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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Vale.Interop.X64.fsti | Vale.Interop.X64.return_val | val return_val (sn: BS.machine_state) : return_val_t sn | val return_val (sn: BS.machine_state) : return_val_t sn | let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 46,
"end_line": 251,
"start_col": 0,
"start_line": 250
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | sn: Vale.X64.Machine_Semantics_s.machine_state -> Vale.Interop.X64.return_val_t sn | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_Semantics_s.machine_state",
"FStar.UInt64.uint_to_t",
"Vale.X64.Machine_Semantics_s.eval_reg_64",
"Vale.X64.Machine_s.rRax",
"Vale.Interop.X64.return_val_t"
] | [] | false | false | false | false | false | let return_val (sn: BS.machine_state) : return_val_t sn =
| UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn) | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.taint_arg_args_b8_mem | val taint_arg_args_b8_mem (args: arg_list) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==> List.memP (taint_arg_b8 a) (args_b8 args)) | val taint_arg_args_b8_mem (args: arg_list) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==> List.memP (taint_arg_b8 a) (args_b8 args)) | let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 32,
"end_line": 184,
"start_col": 0,
"start_line": 178
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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: Vale.Interop.X64.arg_list -> a: Vale.Interop.Base.arg
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP a args /\ Some? (Vale.Interop.X64.taint_of_arg a) ==>
FStar.List.Tot.Base.memP (Vale.Interop.X64.taint_arg_b8 a) (Vale.Interop.Base.args_b8 args)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Vale.Interop.X64.arg_list",
"Vale.Interop.Base.arg",
"Prims.list",
"Vale.Interop.X64.taint_arg_args_b8_mem",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.l_and",
"FStar.List.Tot.Base.memP",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"Vale.Arch.HeapTypes_s.taint",
"Vale.Interop.X64.taint_of_arg",
"Vale.Interop.Types.b8",
"Vale.Interop.X64.taint_arg_b8",
"Vale.Interop.Base.args_b8",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec taint_arg_args_b8_mem (args: arg_list) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==> List.memP (taint_arg_b8 a) (args_b8 args)) =
| match args with
| [] -> ()
| hd :: tl -> taint_arg_args_b8_mem tl a | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig_post | val as_lowstar_sig_post : n: Prims.nat ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
args: Vale.Interop.X64.arg_list ->
h0: Vale.Interop.Base.mem_roots args ->
predict:
Vale.Interop.X64.prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
ret: Vale.Interop.X64.als_ret ->
h1: FStar.Monotonic.HyperStack.mem
-> Prims.logical | let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 42,
"end_line": 349,
"start_col": 0,
"start_line": 324
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
args: Vale.Interop.X64.arg_list ->
h0: Vale.Interop.Base.mem_roots args ->
predict:
Vale.Interop.X64.prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
ret: Vale.Interop.X64.als_ret ->
h1: FStar.Monotonic.HyperStack.mem
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Vale.Interop.X64.arg_list",
"Vale.Interop.Base.mem_roots",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction",
"Vale.Interop.X64.als_ret",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Prims.eq2",
"Vale.Interop.X64.__proj__As_lowstar_sig_ret__item__args",
"Vale.Interop.X64.__proj__As_lowstar_sig_ret__item__n",
"Vale.Interop.Heap_s.hs_of_mem",
"Vale.Interop.X64.prediction_pre",
"FStar.Pervasives.Native.tuple3",
"FStar.UInt64.t",
"Vale.Interop.Heap_s.interop_heap",
"FStar.Pervasives.Native.Mktuple3",
"Vale.Interop.X64.prediction_post",
"FStar.HyperStack.ST.equal_domains",
"Vale.X64.Machine_Semantics_s.machine_state",
"FStar.Pervasives.Native.fst",
"Vale.Interop.X64.create_initial_trusted_state",
"Vale.Interop.X64.__proj__As_lowstar_sig_ret__item__final_mem",
"Vale.Interop.X64.__proj__As_lowstar_sig_ret__item__fuel",
"Vale.Interop.X64.as_lowstar_sig_ret",
"FStar.Ghost.reveal",
"FStar.Pervasives.Native.snd",
"FStar.Ghost.erased",
"Prims.logical"
] | [] | false | false | false | false | true | let as_lowstar_sig_post
(n: nat)
(arg_reg: arg_reg_relation n)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(args: arg_list)
(h0: mem_roots args)
(#pre_rel #post_rel: _)
(predict: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret: als_ret)
(h1: HS.mem)
=
| let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\ n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\ prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1) | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.prediction_post | val prediction_post : n: Prims.nat ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
args: Vale.Interop.X64.arg_list ->
post_rel: Vale.Interop.X64.prediction_post_rel_t c args ->
h0: Vale.Interop.Base.mem_roots args ->
s0: Vale.X64.Machine_Semantics_s.machine_state ->
rax_fuel_mem: ((FStar.UInt64.t * Prims.nat) * Vale.Interop.Heap_s.interop_heap)
-> Prims.logical | let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 295,
"start_col": 0,
"start_line": 274
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
args: Vale.Interop.X64.arg_list ->
post_rel: Vale.Interop.X64.prediction_post_rel_t c args ->
h0: Vale.Interop.Base.mem_roots args ->
s0: Vale.X64.Machine_Semantics_s.machine_state ->
rax_fuel_mem: ((FStar.UInt64.t * Prims.nat) * Vale.Interop.Heap_s.interop_heap)
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Vale.Interop.X64.arg_list",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.Base.mem_roots",
"Vale.X64.Machine_Semantics_s.machine_state",
"FStar.Pervasives.Native.tuple3",
"FStar.UInt64.t",
"Vale.Interop.Heap_s.interop_heap",
"Prims.l_and",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"Vale.X64.Machine_Semantics_s.machine_eval_code",
"FStar.HyperStack.ST.equal_domains",
"LowStar.Monotonic.Buffer.modifies",
"Vale.Interop.Base.loc_modified_args",
"Vale.Interop.Base.mem_roots_p",
"Prims.eq2",
"Vale.Arch.MachineHeap_s.machine_heap",
"Vale.Arch.Heap.heap_create_machine",
"Vale.Interop.Base.mk_mem",
"Vale.Arch.Heap.heap_get",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap",
"Vale.Interop.X64.calling_conventions",
"Vale.Interop.X64.return_val",
"FStar.Monotonic.HyperStack.mem",
"Vale.Interop.Heap_s.hs_of_mem",
"FStar.Pervasives.Native.__proj__Some__item__v",
"Prims.logical"
] | [] | false | false | false | false | true | let prediction_post
(n: nat)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(args: arg_list)
(post_rel: prediction_post_rel_t c args)
(h0: mem_roots args)
(s0: BS.machine_state)
(rax_fuel_mem: (UInt64.t & nat & interop_heap))
=
| let rax, fuel, final_mem = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\
(let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\ B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\ heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\ rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1) | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig_t_weak_stdcall | val as_lowstar_sig_t_weak_stdcall : c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t Vale.Interop.X64.max_stdcall
Vale.Interop.X64.arg_reg_stdcall
Vale.Interop.X64.regs_modified_stdcall
Vale.Interop.X64.xmms_modified_stdcall
c
dom
args
pre_rel
post_rel
-> Type0 | let as_lowstar_sig_t_weak_stdcall = as_lowstar_sig_t_weak' max_stdcall arg_reg_stdcall regs_modified_stdcall xmms_modified_stdcall | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 130,
"end_line": 680,
"start_col": 0,
"start_line": 680
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *)
[@__reduce__]
let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict
val wrap_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:arity_ok n td)
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
let register_of_arg_i (i:reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
let open MS in
if IA.win then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9
//A partial inverse of the above function
[@__reduce__]
let arg_of_register (r:MS.reg_64)
: option (reg_nat (if IA.win then 4 else 6))
= let open MS in
if IA.win then
match r with
| 2 -> Some 0 // rcx
| 3 -> Some 1 // rdx
| 8 -> Some 2 // r8
| 9 -> Some 3 // r9
| _ -> None
else
match r with
| 5 -> Some 0 // rdi
| 4 -> Some 1 // rsi
| 3 -> Some 2 // rdx
| 2 -> Some 3 // rcx
| 8 -> Some 4 // r8
| 9 -> Some 5 // r9
| _ -> None
let max_stdcall : nat = if IA.win then 4 else 6
let arity_ok_stdcall = arity_ok max_stdcall
let arg_reg_stdcall : arg_reg_relation max_stdcall =
Rel arg_of_register register_of_arg_i
let regs_modified_stdcall:MS.reg_64 -> bool = fun (r:MS.reg_64) ->
let open MS in
if IA.win then (
// These registers are callee-saved on Windows
if r = rRbx || r = rRbp || r = rRdi || r = rRsi || r = rRsp || r = rR12 || r = rR13 || r = rR14 || r = rR15 then false
// All the other ones may be modified
else true
) else (
// These registers are callee-saved on Linux
if r = rRbx || r = rRbp || r = rR12 || r = rR13 || r = rR14 || r = rR15 then false
// All the other ones may be modified
else true
)
let xmms_modified_stdcall:MS.reg_xmm -> bool = fun (x:MS.reg_xmm) ->
let open MS in
if IA.win then (
// These xmms are callee-saved on Windows
if x = 6 || x = 7 || x = 8 || x = 9 || x = 10 || x = 11 || x = 12 || x = 13 || x = 14 || x = 15 then false
else true
) else
// No xmm needs to be callee-saved on Linux
true
// For stdcalls, we do not have the arity_ok restriction: We can pass as many arguments as we want, the extra arguments will be passed on the stack | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 |
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t Vale.Interop.X64.max_stdcall
Vale.Interop.X64.arg_reg_stdcall
Vale.Interop.X64.regs_modified_stdcall
Vale.Interop.X64.xmms_modified_stdcall
c
dom
args
pre_rel
post_rel
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.X64.as_lowstar_sig_t_weak'",
"Vale.Interop.X64.max_stdcall",
"Vale.Interop.X64.arg_reg_stdcall",
"Vale.Interop.X64.regs_modified_stdcall",
"Vale.Interop.X64.xmms_modified_stdcall"
] | [] | false | false | false | false | true | let as_lowstar_sig_t_weak_stdcall =
| as_lowstar_sig_t_weak' max_stdcall arg_reg_stdcall regs_modified_stdcall xmms_modified_stdcall | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.mk_taint_equiv | val mk_taint_equiv (args: arg_list_sb{disjoint_or_eq args}) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a)) | val mk_taint_equiv (args: arg_list_sb{disjoint_or_eq args}) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a)) | let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 56,
"end_line": 200,
"start_col": 0,
"start_line": 186
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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: Vale.Interop.X64.arg_list_sb{Vale.Interop.Base.disjoint_or_eq args} ->
a: Vale.Interop.Base.arg
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP a args /\ Some? (Vale.Interop.X64.taint_of_arg a) ==>
Some?.v (Vale.Interop.X64.taint_of_arg a) ==
Vale.Interop.X64.mk_taint args Vale.Interop.X64.init_taint (Vale.Interop.X64.taint_arg_b8 a)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Vale.Interop.X64.arg_list_sb",
"Vale.Interop.Base.disjoint_or_eq",
"Vale.Interop.Base.arg",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.Base.valid_base_type",
"Vale.Arch.HeapTypes_s.base_typ",
"Vale.Interop.Base.buffer_qualifiers",
"FStar.BigOps.big_and'_forall",
"Vale.Interop.Base.disjoint_or_eq_1",
"Prims.unit",
"Vale.Interop.Base.disjoint_or_eq_cons",
"Vale.Interop.X64.mk_taint_equiv",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.l_and",
"FStar.List.Tot.Base.memP",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"Vale.Arch.HeapTypes_s.taint",
"Vale.Interop.X64.taint_of_arg",
"Prims.eq2",
"FStar.Pervasives.Native.__proj__Some__item__v",
"Vale.Interop.X64.mk_taint",
"Vale.Interop.X64.init_taint",
"Vale.Interop.X64.taint_arg_b8",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mk_taint_equiv (args: arg_list_sb{disjoint_or_eq args}) (a: arg)
: Lemma
(List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a)) =
| match args with
| [] -> ()
| hd :: tl ->
mk_taint_equiv tl a;
let (| tag , x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _
| TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.elim_predict_t_cons | val elim_predict_t_cons
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(hd: td)
(tl: list td)
(#args: arg_list{List.length args + List.length tl <= 19})
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c (hd :: tl) args pre_rel post_rel)
(x: td_as_type hd)
: prediction_t n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) | val elim_predict_t_cons
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(hd: td)
(tl: list td)
(#args: arg_list{List.length args + List.length tl <= 19})
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c (hd :: tl) args pre_rel post_rel)
(x: td_as_type hd)
: prediction_t n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) | let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 6,
"end_line": 481,
"start_col": 0,
"start_line": 465
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 |
hd: Vale.Interop.Base.td ->
tl: Prims.list Vale.Interop.Base.td ->
p:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
(hd :: tl)
args
pre_rel
post_rel ->
x: Vale.Interop.Base.td_as_type hd
-> Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(Vale.Interop.X64.elim_rel_gen_t_cons hd tl pre_rel x)
(Vale.Interop.X64.elim_rel_gen_t_cons hd tl post_rel x) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Vale.Interop.Base.td",
"Prims.list",
"Vale.Interop.X64.arg_list",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.Base.arg",
"Vale.Interop.X64.rel_gen_t",
"Prims.Cons",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction_t",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.op_Plus_Plus",
"Vale.Interop.X64.elim_rel_gen_t_cons"
] | [] | false | false | false | false | false | let elim_predict_t_cons
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(hd: td)
(tl: list td)
(#args: arg_list{List.length args + List.length tl <= 19})
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c (hd :: tl) args pre_rel post_rel)
(x: td_as_type hd)
: prediction_t n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) =
| p | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.rel_gen_t | val rel_gen_t : c: Vale.X64.Machine_Semantics_s.code ->
td: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length td <= 20} ->
f: (_: Vale.Interop.X64.arg_list -> Type)
-> Type | let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 32,
"end_line": 409,
"start_col": 0,
"start_line": 400
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 |
c: Vale.X64.Machine_Semantics_s.code ->
td: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length td <= 20} ->
f: (_: Vale.Interop.X64.arg_list -> Type)
-> Type | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_Semantics_s.code",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.X64.arg_list",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.Base.arg",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.X64.op_Plus_Plus"
] | [
"recursion"
] | false | false | false | false | true | let rec rel_gen_t
(c: BS.code)
(td: list td)
(args: arg_list{List.length args + List.length td <= 20})
(f: (arg_list -> Type))
=
| match td with
| [] -> f args
| hd :: tl -> x: td_as_type hd -> rel_gen_t c tl (x ++ args) f | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig | val as_lowstar_sig : c: Vale.X64.Machine_Semantics_s.code -> Type | let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 117,
"end_line": 392,
"start_col": 0,
"start_line": 381
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1)) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | c: Vale.X64.Machine_Semantics_s.code -> Type | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_Semantics_s.code",
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.Interop.X64.arg_list",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction",
"Vale.Interop.X64.als_ret",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Vale.Interop.Base.mem_roots_p",
"Vale.Interop.X64.as_lowstar_sig_post"
] | [] | false | false | false | true | true | let as_lowstar_sig (c: BS.code) =
|
n: nat ->
arg_reg: arg_reg_relation n ->
regs_modified: (MS.reg_64 -> bool) ->
xmms_modified: (MS.reg_xmm -> bool) ->
args: arg_list ->
#pre_rel: _ ->
#post_rel: _ ->
predict: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
-> FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures
fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1) | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.elim_predict_t_nil | val elim_predict_t_nil
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(#args: arg_list)
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel | val elim_predict_t_nil
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(#args: arg_list)
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel | let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 6,
"end_line": 462,
"start_col": 0,
"start_line": 451
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | p: Vale.Interop.X64.prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel
-> Vale.Interop.X64.prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Vale.Interop.X64.arg_list",
"Vale.Interop.X64.rel_gen_t",
"Prims.Nil",
"Vale.Interop.Base.td",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction_t",
"Vale.Interop.X64.prediction"
] | [] | false | false | false | false | false | let elim_predict_t_nil
(#n: nat)
(#arg_reg: arg_reg_relation n)
(#regs_modified: (MS.reg_64 -> bool))
(#xmms_modified: (MS.reg_xmm -> bool))
(#c: BS.code)
(#args: arg_list)
(#pre_rel #post_rel: _)
(p: prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel =
| p | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.register_of_arg_i | val register_of_arg_i (i: reg_nat (if IA.win then 4 else 6)) : MS.reg_64 | val register_of_arg_i (i: reg_nat (if IA.win then 4 else 6)) : MS.reg_64 | let register_of_arg_i (i:reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
let open MS in
if IA.win then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9 | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 624,
"start_col": 0,
"start_line": 609
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *)
[@__reduce__]
let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict
val wrap_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:arity_ok n td)
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 |
i:
Vale.Interop.X64.reg_nat (match Vale.Interop.Assumptions.win with
| true -> 4
| _ -> 6)
-> Vale.X64.Machine_s.reg_64 | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.X64.reg_nat",
"Vale.Interop.Assumptions.win",
"Prims.bool",
"Prims.nat",
"Vale.X64.Machine_s.rRcx",
"Vale.X64.Machine_s.rRdx",
"Vale.X64.Machine_s.rR8",
"Vale.X64.Machine_s.rR9",
"Vale.X64.Machine_s.reg_64",
"Vale.X64.Machine_s.rRdi",
"Vale.X64.Machine_s.rRsi"
] | [] | false | false | false | false | false | let register_of_arg_i (i: reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
| let open MS in
if IA.win
then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9 | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.elim_rel_gen_t_cons | val elim_rel_gen_t_cons (#c hd tl #args #f: _) (p: rel_gen_t c (hd :: tl) args f)
: (x: td_as_type hd -> rel_gen_t c tl (x ++ args) f) | val elim_rel_gen_t_cons (#c hd tl #args #f: _) (p: rel_gen_t c (hd :: tl) args f)
: (x: td_as_type hd -> rel_gen_t c tl (x ++ args) f) | let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 420,
"start_col": 0,
"start_line": 417
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 |
hd: Vale.Interop.Base.td ->
tl: Prims.list Vale.Interop.Base.td ->
p: Vale.Interop.X64.rel_gen_t c (hd :: tl) args f ->
x: Vale.Interop.Base.td_as_type hd
-> Vale.Interop.X64.rel_gen_t c tl (x ++ args) f | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_Semantics_s.code",
"Vale.Interop.Base.td",
"Prims.list",
"Vale.Interop.X64.arg_list",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.Base.arg",
"Prims.Cons",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.op_Plus_Plus"
] | [] | false | false | false | false | false | let elim_rel_gen_t_cons #c hd tl #args #f (p: rel_gen_t c (hd :: tl) args f)
: (x: td_as_type hd -> rel_gen_t c tl (x ++ args) f) =
| p | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.arg_of_register | val arg_of_register (r: MS.reg_64) : option (reg_nat (if IA.win then 4 else 6)) | val arg_of_register (r: MS.reg_64) : option (reg_nat (if IA.win then 4 else 6)) | let arg_of_register (r:MS.reg_64)
: option (reg_nat (if IA.win then 4 else 6))
= let open MS in
if IA.win then
match r with
| 2 -> Some 0 // rcx
| 3 -> Some 1 // rdx
| 8 -> Some 2 // r8
| 9 -> Some 3 // r9
| _ -> None
else
match r with
| 5 -> Some 0 // rdi
| 4 -> Some 1 // rsi
| 3 -> Some 2 // rdx
| 2 -> Some 3 // rcx
| 8 -> Some 4 // r8
| 9 -> Some 5 // r9
| _ -> None | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 646,
"start_col": 0,
"start_line": 628
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *)
[@__reduce__]
let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict
val wrap_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:arity_ok n td)
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
let register_of_arg_i (i:reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
let open MS in
if IA.win then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9
//A partial inverse of the above function | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | r: Vale.X64.Machine_s.reg_64
-> FStar.Pervasives.Native.option (Vale.Interop.X64.reg_nat (match
Vale.Interop.Assumptions.win
with
| true -> 4
| _ -> 6)) | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_s.reg_64",
"Vale.Interop.Assumptions.win",
"FStar.Pervasives.Native.Some",
"Vale.Interop.X64.reg_nat",
"Prims.bool",
"Prims.nat",
"Prims.int",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.option"
] | [] | false | false | false | false | false | let arg_of_register (r: MS.reg_64) : option (reg_nat (if IA.win then 4 else 6)) =
| let open MS in
if IA.win
then
match r with
| 2 -> Some 0
| 3 -> Some 1
| 8 -> Some 2
| 9 -> Some 3
| _ -> None
else
match r with
| 5 -> Some 0
| 4 -> Some 1
| 3 -> Some 2
| 2 -> Some 3
| 8 -> Some 4
| 9 -> Some 5
| _ -> None | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig_t_weak | val as_lowstar_sig_t_weak : n: Prims.nat{n <= 20} ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= n} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 102,
"end_line": 595,
"start_col": 0,
"start_line": 584
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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{n <= 20} ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= n} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.Base.arg",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction_t",
"Vale.Interop.X64.as_lowstar_sig_t_weak'"
] | [] | false | false | false | false | true | let as_lowstar_sig_t_weak
(n: nat{n <= 20})
(arg_reg: arg_reg_relation n)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(dom: list td)
(args: list arg {List.length args + List.length dom <= n})
(pre_rel: rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel: rel_gen_t c dom args (prediction_post_rel_t c))
(predict: prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel)
=
| as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.arg_reg_stdcall | val arg_reg_stdcall:arg_reg_relation max_stdcall | val arg_reg_stdcall:arg_reg_relation max_stdcall | let arg_reg_stdcall : arg_reg_relation max_stdcall =
Rel arg_of_register register_of_arg_i | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 39,
"end_line": 652,
"start_col": 0,
"start_line": 651
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *)
[@__reduce__]
let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict
val wrap_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:arity_ok n td)
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
let register_of_arg_i (i:reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
let open MS in
if IA.win then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9
//A partial inverse of the above function
[@__reduce__]
let arg_of_register (r:MS.reg_64)
: option (reg_nat (if IA.win then 4 else 6))
= let open MS in
if IA.win then
match r with
| 2 -> Some 0 // rcx
| 3 -> Some 1 // rdx
| 8 -> Some 2 // r8
| 9 -> Some 3 // r9
| _ -> None
else
match r with
| 5 -> Some 0 // rdi
| 4 -> Some 1 // rsi
| 3 -> Some 2 // rdx
| 2 -> Some 3 // rcx
| 8 -> Some 4 // r8
| 9 -> Some 5 // r9
| _ -> None
let max_stdcall : nat = if IA.win then 4 else 6
let arity_ok_stdcall = arity_ok max_stdcall | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | v:
Vale.Interop.X64.arg_reg_relation' Vale.Interop.X64.max_stdcall
{ forall (r: Vale.X64.Machine_s.reg_64). {:pattern Rel?.of_reg v r}
Some? (Rel?.of_reg v r) ==> Rel?.of_arg v (Some?.v (Rel?.of_reg v r)) = r } | Prims.Tot | [
"total"
] | [] | [
"Vale.Interop.X64.Rel",
"Vale.Interop.Assumptions.win",
"Prims.bool",
"Prims.nat",
"Vale.Interop.X64.arg_of_register",
"Vale.Interop.X64.register_of_arg_i"
] | [] | false | false | false | false | false | let arg_reg_stdcall:arg_reg_relation max_stdcall =
| Rel arg_of_register register_of_arg_i | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig_t | val as_lowstar_sig_t : n: Prims.nat ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 47,
"end_line": 517,
"start_col": 0,
"start_line": 484
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.X64.arg_list",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.Base.arg",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction_t",
"Prims.unit",
"Vale.Interop.X64.als_ret",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Vale.Interop.Base.mem_roots_p",
"Vale.Interop.X64.elim_rel_gen_t_nil",
"Vale.Interop.X64.as_lowstar_sig_post",
"Vale.Interop.X64.elim_predict_t_nil",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.as_lowstar_sig_t",
"Vale.Interop.X64.op_Plus_Plus",
"Vale.Interop.X64.elim_rel_gen_t_cons",
"Vale.Interop.X64.elim_predict_t_cons"
] | [
"recursion"
] | false | false | false | false | true | let rec as_lowstar_sig_t
(n: nat)
(arg_reg: arg_reg_relation n)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(dom: list td)
(args: arg_list{List.length args + List.length dom <= 20})
(pre_rel: rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel: rel_gen_t c dom args (prediction_post_rel_t c))
(predict: prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel)
=
| match dom with
| [] ->
(unit
-> FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ elim_rel_gen_t_nil pre_rel h0))
(ensures
fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 #pre_rel #post_rel
(elim_predict_t_nil predict) ret h1))
| hd :: tl ->
x: td_as_type hd
-> as_lowstar_sig_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x) (elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x) | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.prediction_t | val prediction_t : n: Prims.nat ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length dom + FStar.List.Tot.Base.length args <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c)
-> Type | let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 48,
"end_line": 448,
"start_col": 0,
"start_line": 422
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Vale.Interop.X64.arg_list
{FStar.List.Tot.Base.length dom + FStar.List.Tot.Base.length args <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c)
-> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.X64.arg_list",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.Base.arg",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.prediction_t",
"Vale.Interop.X64.op_Plus_Plus",
"Vale.Interop.X64.elim_rel_gen_t_cons"
] | [
"recursion"
] | false | false | false | false | true | let rec prediction_t
(n: nat)
(arg_reg: arg_reg_relation n)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(dom: list td)
(args: arg_list{List.length dom + List.length args <= 20})
(pre_rel: rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel: rel_gen_t c dom args (prediction_post_rel_t c))
=
| match dom with
| [] -> prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd :: tl ->
x: td_as_type hd
-> prediction_t n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x) | false |
|
Vale.Interop.X64.fsti | Vale.Interop.X64.regs_modified_stdcall | val regs_modified_stdcall: MS.reg_64 -> bool | val regs_modified_stdcall: MS.reg_64 -> bool | let regs_modified_stdcall:MS.reg_64 -> bool = fun (r:MS.reg_64) ->
let open MS in
if IA.win then (
// These registers are callee-saved on Windows
if r = rRbx || r = rRbp || r = rRdi || r = rRsi || r = rRsp || r = rR12 || r = rR13 || r = rR14 || r = rR15 then false
// All the other ones may be modified
else true
) else (
// These registers are callee-saved on Linux
if r = rRbx || r = rRbp || r = rR12 || r = rR13 || r = rR14 || r = rR15 then false
// All the other ones may be modified
else true
) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 666,
"start_col": 0,
"start_line": 654
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__]
private
let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
(* These two functions are the ones that are available from outside the module. The arity_ok restriction ensures that all arguments are passed in registers for inline assembly *)
[@__reduce__]
let as_lowstar_sig_t_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= n})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel predict
val wrap_weak
(n:nat{n <= 20})
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:arity_ok n td)
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t_weak n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
let register_of_arg_i (i:reg_nat (if IA.win then 4 else 6)) : MS.reg_64 =
let open MS in
if IA.win then
match i with
| 0 -> rRcx
| 1 -> rRdx
| 2 -> rR8
| 3 -> rR9
else
match i with
| 0 -> rRdi
| 1 -> rRsi
| 2 -> rRdx
| 3 -> rRcx
| 4 -> rR8
| 5 -> rR9
//A partial inverse of the above function
[@__reduce__]
let arg_of_register (r:MS.reg_64)
: option (reg_nat (if IA.win then 4 else 6))
= let open MS in
if IA.win then
match r with
| 2 -> Some 0 // rcx
| 3 -> Some 1 // rdx
| 8 -> Some 2 // r8
| 9 -> Some 3 // r9
| _ -> None
else
match r with
| 5 -> Some 0 // rdi
| 4 -> Some 1 // rsi
| 3 -> Some 2 // rdx
| 2 -> Some 3 // rcx
| 8 -> Some 4 // r8
| 9 -> Some 5 // r9
| _ -> None
let max_stdcall : nat = if IA.win then 4 else 6
let arity_ok_stdcall = arity_ok max_stdcall
let arg_reg_stdcall : arg_reg_relation max_stdcall =
Rel arg_of_register register_of_arg_i | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 | r: Vale.X64.Machine_s.reg_64 -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Vale.X64.Machine_s.reg_64",
"Vale.Interop.Assumptions.win",
"Prims.op_BarBar",
"Prims.op_Equality",
"Vale.X64.Machine_s.rRbx",
"Vale.X64.Machine_s.rRbp",
"Vale.X64.Machine_s.rRdi",
"Vale.X64.Machine_s.rRsi",
"Vale.X64.Machine_s.rRsp",
"Vale.X64.Machine_s.rR12",
"Vale.X64.Machine_s.rR13",
"Vale.X64.Machine_s.rR14",
"Vale.X64.Machine_s.rR15",
"Prims.bool"
] | [] | false | false | false | true | false | let regs_modified_stdcall: MS.reg_64 -> bool =
| fun (r: MS.reg_64) ->
let open MS in
if IA.win
then
(if
r = rRbx || r = rRbp || r = rRdi || r = rRsi || r = rRsp || r = rR12 || r = rR13 || r = rR14 ||
r = rR15
then false
else true)
else
(if r = rRbx || r = rRbp || r = rR12 || r = rR13 || r = rR14 || r = rR15 then false else true) | false |
Vale.Interop.X64.fsti | Vale.Interop.X64.as_lowstar_sig_t_weak' | val as_lowstar_sig_t_weak' : n: Prims.nat ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | let rec as_lowstar_sig_t_weak'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:list arg{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t_weak'
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x) | {
"file_name": "vale/specs/interop/Vale.Interop.X64.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 47,
"end_line": 567,
"start_col": 0,
"start_line": 534
} | module Vale.Interop.X64
open FStar.Mul
open Vale.Interop.Base
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
module B = LowStar.Buffer
module BS = Vale.X64.Machine_Semantics_s
module UV = LowStar.BufferView.Up
module DV = LowStar.BufferView.Down
module HS = FStar.HyperStack
module MS = Vale.X64.Machine_s
module IA = Vale.Interop.Assumptions
module List = FStar.List.Tot
////////////////////////////////////////////////////////////////////////////////
//The calling convention w.r.t the register mapping
////////////////////////////////////////////////////////////////////////////////
let calling_conventions
(s0 s1:BS.machine_state)
(regs_modified: MS.reg_64 -> bool)
(xmms_modified: MS.reg_xmm -> bool) =
let s0 = s0 in
let s1 = s1 in
s1.BS.ms_ok /\
s0.BS.ms_regs MS.reg_Rsp == s1.BS.ms_regs MS.reg_Rsp /\
(forall (r:MS.reg). {:pattern (s0.BS.ms_regs r)}
match r with
| MS.Reg 0 r -> not (regs_modified r) ==> s0.BS.ms_regs (MS.Reg 0 r) == s1.BS.ms_regs (MS.Reg 0 r)
| MS.Reg 1 r -> not (xmms_modified r) ==> s0.BS.ms_regs (MS.Reg 1 r) == s1.BS.ms_regs (MS.Reg 1 r)
)
let reg_nat (n:nat) = i:nat{i < n}
let arity_ok n 'a = l:list 'a { List.Tot.length l <= n }
(* We limit the number of args we can pass through the interop wrappers to an arbitrary 20.
This ensures first that the addr_map axiom is sound: Since the length of buffers is limited to 2^32, we can prove that addr_map is inhabited.
for extra arguments + the extra slots needed.
Note that this number can be increased if needed*)
let arg_list = l:list arg{List.Tot.length l <= 20}
let arg_list_sb = l:list arg{List.Tot.length l <= 21}
unfold
let injective f = forall x y.{:pattern f x; f y} f x == f y ==> x == y
noeq
type arg_reg_relation' (n:nat) =
| Rel: of_reg:(MS.reg_64 -> option (reg_nat n)) ->
of_arg:(reg_nat n -> MS.reg_64){
// This function should be injective
injective of_arg /\
// rRsp is not a valid register to store paramters
(forall (i:reg_nat n).{:pattern of_arg i} of_arg i <> MS.rRsp) /\
// of_reg should always return Some when the register corresponds to an of_arg
(forall (i:reg_nat n).{:pattern of_arg i}
Some? (of_reg (of_arg i)) /\ Some?.v (of_reg (of_arg i)) = i)} ->
arg_reg_relation' n
unfold
let arg_reg_relation (n:nat) = (v:arg_reg_relation' n{
// of_reg is a partial inverse of of_arg
forall (r:MS.reg_64).{:pattern v.of_reg r} Some? (v.of_reg r) ==> v.of_arg (Some?.v (v.of_reg r)) = r})
let registers = MS.reg_64 -> MS.nat64
let upd_reg (n:nat) (arg_reg:arg_reg_relation n) (regs:registers) (i:nat) (v:_) : registers =
fun (r:MS.reg_64) ->
match arg_reg.of_reg r with
| Some j ->
if i = j then v
else regs r
| _ -> regs r
[@__reduce__]
let arg_as_nat64 (a:arg) : GTot MS.nat64 =
let (| tag, x |) = a in
match tag with
| TD_Base TUInt8 ->
UInt8.v x
| TD_Base TUInt16 ->
UInt16.v x
| TD_Base TUInt32 ->
UInt32.v x
| TD_Base TUInt64 ->
UInt64.v x
| TD_Buffer src _ _ ->
let b:b8 = Buffer true (x <: B.buffer (base_typ_as_type src)) in
global_addrs_map b
| TD_ImmBuffer src _ _ -> global_addrs_map (imm_to_b8 src x)
[@__reduce__]
let update_regs (n:nat)
(arg_reg:arg_reg_relation n)
(x:arg)
(i:reg_nat n)
(regs:registers)
: GTot registers
= upd_reg n arg_reg regs i (arg_as_nat64 x)
[@__reduce__]
let rec register_of_args (max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(n:nat)
(args:arg_list{List.Tot.length args = n})
(regs:registers) : GTot (regs':registers{regs MS.rRsp == regs' MS.rRsp}) =
match args with
| [] -> regs
| hd::tl ->
if n > max_arity then
// This arguments will be passed on the stack
register_of_args max_arity arg_reg (n-1) tl regs
else
update_regs max_arity arg_reg hd (n - 1) (register_of_args max_arity arg_reg (n - 1) tl regs)
// Pass extra arguments on the stack. The arity_ok condition on inline wrappers ensures that
// this only happens for stdcalls
[@__reduce__]
let rec stack_of_args (max_arity:nat)
(n:nat)
(rsp:int)
(args:arg_list{List.Tot.length args = n})
(st:Map.t int Vale.Def.Words_s.nat8)
: GTot (Map.t int Vale.Def.Words_s.nat8) =
match args with
| [] -> st
| hd::tl ->
if n <= max_arity then st // We can pass the remaining args in registers
else
let ptr = ((n - max_arity) - 1) * 8 // Arguments on the stack are pushed from right to left
+ (if IA.win then 32 else 0) // The shadow space on Windows comes next
+ 8 // The return address is then pushed on the stack
+ rsp // And we then have all the extra slots required for the Vale procedure
in
let st1 = stack_of_args max_arity (n-1) rsp tl st in
let v = arg_as_nat64 hd in // We will store the arg hd
BS.update_heap64 ptr v st1
////////////////////////////////////////////////////////////////////////////////
let taint_map = b8 -> GTot taint
let upd_taint_map_b8 (tm:taint_map) (x:b8) (tnt:taint) : taint_map =
fun (y:b8) ->
if StrongExcludedMiddle.strong_excluded_middle ((x <: b8) == y) then
tnt
else tm y
[@__reduce__]
let upd_taint_map_arg (a:arg) (tm:taint_map) : GTot taint_map =
match a with
| (| TD_Buffer _ _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (Buffer true x) tnt
| (| TD_ImmBuffer src _ {taint=tnt}, x |) ->
upd_taint_map_b8 tm (imm_to_b8 src x) tnt
| (| TD_Base _, _ |) ->
tm
let init_taint : taint_map = fun r -> Public
[@__reduce__]
let mk_taint (args:arg_list_sb) (tm:taint_map) : GTot taint_map =
List.fold_right_gtot args upd_taint_map_arg init_taint
let taint_of_arg (a:arg) =
let (| tag, x |) = a in
match tag with
| TD_ImmBuffer _ TUInt64 {taint=tnt}
| TD_ImmBuffer _ TUInt128 {taint=tnt}
| TD_Buffer _ TUInt64 {taint=tnt}
| TD_Buffer _ TUInt128 {taint=tnt} -> Some tnt
| _ -> None
let taint_arg_b8 (a:arg{Some? (taint_of_arg a)}) : GTot b8 =
let (| tag, x |) = a in
match tag with
| TD_Buffer src _ _ -> Buffer true (x <: B.buffer (base_typ_as_type src))
| TD_ImmBuffer src _ _ -> imm_to_b8 src x
let rec taint_arg_args_b8_mem (args:arg_list) (a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
List.memP (taint_arg_b8 a) (args_b8 args))
= match args with
| [] -> ()
| hd::tl ->
taint_arg_args_b8_mem tl a
let rec mk_taint_equiv
(args:arg_list_sb{disjoint_or_eq args})
(a:arg)
: Lemma (List.memP a args /\ Some? (taint_of_arg a) ==>
Some?.v (taint_of_arg a) == (mk_taint args init_taint) (taint_arg_b8 a))
= match args with
| [] -> ()
| hd::tl ->
mk_taint_equiv tl a;
let (| tag, x |) = hd in
match tag with
| TD_Base _ -> ()
| TD_Buffer _ _ _ | TD_ImmBuffer _ _ _ ->
disjoint_or_eq_cons hd tl;
BigOps.big_and'_forall (disjoint_or_eq_1 hd) tl
////////////////////////////////////////////////////////////////////////////////
let state_builder_t (max_arity:nat) (args:arg_list) (codom:Type) =
h0:HS.mem{mem_roots_p h0 args} ->
GTot codom
// Splitting the construction of the initial state into two functions
// one that creates the initial trusted state (i.e., part of our TCB)
// and another that just creates the vale state, a view upon the trusted one
let create_initial_trusted_state
(max_arity:nat)
(arg_reg:arg_reg_relation max_arity)
(args:arg_list)
: state_builder_t max_arity args (BS.machine_state & interop_heap) =
fun h0 ->
let open MS in
let regs_64 = register_of_args max_arity arg_reg (List.Tot.length args) args IA.init_regs in
let xmms = IA.init_xmms in
let flags = FunctionalExtensionality.on flag IA.init_flags in
let init_rsp = regs_64 rRsp in
let regs = FunctionalExtensionality.on_dom reg #t_reg (fun r ->
match r with
| Reg 0 r -> regs_64 r
| Reg 1 r -> xmms r)
in
// Create an initial empty stack
let stack = Map.const_on Set.empty 0 in
// Spill additional arguments on the stack
let stack = stack_of_args max_arity (List.Tot.length args) init_rsp args stack in
let mem:interop_heap = mk_mem args h0 in
let memTaint = create_memtaint mem (args_b8 args) (mk_taint args init_taint) in
let (s0:BS.machine_state) = {
BS.ms_ok = true;
BS.ms_regs = regs;
BS.ms_flags = flags;
BS.ms_heap = heap_create_impl mem memTaint;
BS.ms_stack = BS.Machine_stack init_rsp stack;
BS.ms_stackTaint = Map.const Public;
BS.ms_trace = [];
} in
(s0, mem)
////////////////////////////////////////////////////////////////////////////////
let prediction_pre_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
prop
let return_val_t (sn:BS.machine_state) = r:UInt64.t{UInt64.v r == BS.eval_reg_64 MS.rRax sn}
let return_val (sn:BS.machine_state) : return_val_t sn =
UInt64.uint_to_t (BS.eval_reg_64 MS.rRax sn)
let prediction_post_rel_t (c:BS.code) (args:arg_list) =
h0:mem_roots args ->
s0:BS.machine_state ->
(UInt64.t & nat & interop_heap) ->
sn:BS.machine_state ->
prop
[@__reduce__]
let prediction_pre
(n:nat)
(arg_reg:arg_reg_relation n)
(c:BS.code)
(args:arg_list)
(pre_rel: prediction_pre_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
=
pre_rel h0 /\
s0 == fst (create_initial_trusted_state n arg_reg args h0)
[@__reduce__]
let prediction_post
(n:nat)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(post_rel: prediction_post_rel_t c args)
(h0:mem_roots args)
(s0:BS.machine_state)
(rax_fuel_mem:(UInt64.t & nat & interop_heap)) =
let (rax, fuel, final_mem) = rax_fuel_mem in
Some? (BS.machine_eval_code c fuel s0) /\ (
let s1 = Some?.v (BS.machine_eval_code c fuel s0) in
let h1 = hs_of_mem final_mem in
FStar.HyperStack.ST.equal_domains h0 h1 /\
B.modifies (loc_modified_args args) h0 h1 /\
mem_roots_p h1 args /\
heap_create_machine (mk_mem args h1) == heap_get s1.BS.ms_heap /\
calling_conventions s0 s1 regs_modified xmms_modified /\
rax == return_val s1 /\
post_rel h0 s0 rax_fuel_mem s1
)
let prediction
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(pre_rel:prediction_pre_rel_t c args)
(post_rel:prediction_post_rel_t c args) =
h0:mem_roots args{pre_rel h0} ->
s0:BS.machine_state ->
Ghost (UInt64.t & nat & interop_heap)
(requires prediction_pre n arg_reg c args pre_rel h0 s0)
(ensures prediction_post n regs_modified xmms_modified c args post_rel h0 s0)
noeq
type as_lowstar_sig_ret =
| As_lowstar_sig_ret :
n:nat ->
args:arg_list ->
fuel:nat ->
final_mem:interop_heap ->
as_lowstar_sig_ret
let als_ret = UInt64.t & Ghost.erased as_lowstar_sig_ret
[@__reduce__]
let as_lowstar_sig_post
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
h1 == hs_of_mem final_mem /\
prediction_pre n arg_reg c args pre_rel h0 s0 /\
(rax, fuel, final_mem) == predict h0 s0 /\
prediction_post n regs_modified xmms_modified c args post_rel h0 s0 (rax, fuel, final_mem) /\
FStar.HyperStack.ST.equal_domains h0 h1)
[@__reduce__]
let as_lowstar_sig_post_weak
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(args:arg_list)
(h0:mem_roots args)
(#pre_rel:_)
(#post_rel: _)
(predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel)
(ret:als_ret)
(h1:HS.mem) =
(* write it this way to be reduction friendly *)
let rax = fst ret in
let ret = Ghost.reveal (snd ret) in
args == As_lowstar_sig_ret?.args ret /\
n == As_lowstar_sig_ret?.n ret /\
(let fuel = As_lowstar_sig_ret?.fuel ret in
let final_mem = As_lowstar_sig_ret?.final_mem ret in
let s0 = fst (create_initial_trusted_state n arg_reg args h0) in
(exists fuel
final_mem
s1.
h1 == hs_of_mem final_mem /\
rax == return_val s1 /\
post_rel h0 s0 (return_val s1, fuel, final_mem) s1))
[@__reduce__]
let as_lowstar_sig (c:BS.code) =
n:nat ->
arg_reg:arg_reg_relation n ->
regs_modified:(MS.reg_64 -> bool) ->
xmms_modified:(MS.reg_xmm -> bool) ->
args:arg_list ->
#pre_rel:_ ->
#post_rel:_ ->
predict:prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ pre_rel h0))
(ensures fun h0 ret h1 -> as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0 predict ret h1)
val wrap_variadic (c:BS.code) : as_lowstar_sig c
[@__reduce__]
let (++) (#t:td) (x:td_as_type t) (args:list arg) = (| t, x |) :: args
[@__reduce__]
let rec rel_gen_t
(c:BS.code)
(td:list td)
(args:arg_list{List.length args + List.length td <= 20})
(f: arg_list -> Type) =
match td with
| [] -> f args
| hd::tl ->
x:td_as_type hd ->
rel_gen_t c tl (x++args) f
[@__reduce__]
let elim_rel_gen_t_nil #c #args #f (x:rel_gen_t c [] args f)
: f args
= x
[@__reduce__]
let elim_rel_gen_t_cons #c hd tl #args #f (p:rel_gen_t c (hd::tl) args f)
: (x:td_as_type hd ->
rel_gen_t c tl (x++args) f)
= p
let rec prediction_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length dom + List.length args <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
=
match dom with
| [] ->
prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
| hd::tl ->
x:td_as_type hd ->
prediction_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
[@__reduce__]
let elim_predict_t_nil
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(#args:arg_list)
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c [] args pre_rel post_rel)
: prediction n arg_reg regs_modified xmms_modified c args pre_rel post_rel
= p
[@__reduce__]
let elim_predict_t_cons
(#n:nat)
(#arg_reg:arg_reg_relation n)
(#regs_modified:MS.reg_64 -> bool)
(#xmms_modified:MS.reg_xmm -> bool)
(#c:BS.code)
(hd:td)
(tl:list td)
(#args:arg_list{List.length args + List.length tl <= 19})
(#pre_rel:_)
(#post_rel:_)
(p:prediction_t n arg_reg regs_modified xmms_modified c (hd::tl) args pre_rel post_rel)
: x:td_as_type hd ->
prediction_t n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
= p
[@__reduce__]
let rec as_lowstar_sig_t
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td)
(args:arg_list{List.length args + List.length dom <= 20})
(pre_rel:rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel:rel_gen_t c dom args (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel) =
match dom with
| [] ->
(unit ->
FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 ->
mem_roots_p h0 args /\
elim_rel_gen_t_nil pre_rel h0))
(ensures fun h0 ret h1 ->
as_lowstar_sig_post n arg_reg regs_modified xmms_modified c args h0
#pre_rel #post_rel (elim_predict_t_nil predict) ret h1))
| hd::tl ->
x:td_as_type hd ->
as_lowstar_sig_t
n
arg_reg
regs_modified
xmms_modified
c
tl
(x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x)
(elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x)
private
val wrap'
(n:nat)
(arg_reg:arg_reg_relation n)
(regs_modified:MS.reg_64 -> bool)
(xmms_modified:MS.reg_xmm -> bool)
(c:BS.code)
(dom:list td{List.length dom <= 20})
(#pre_rel:rel_gen_t c dom [] (prediction_pre_rel_t c))
(#post_rel:rel_gen_t c dom [] (prediction_post_rel_t c))
(predict:prediction_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel)
: as_lowstar_sig_t n arg_reg regs_modified xmms_modified c dom [] pre_rel post_rel predict
[@__reduce__] | {
"checked_file": "/",
"dependencies": [
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.Interop.Base.fst.checked",
"Vale.Interop.Assumptions.fst.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"LowStar.BufferView.Up.fsti.checked",
"LowStar.BufferView.Down.fsti.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.BigOps.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.Interop.X64.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "List"
},
{
"abbrev": true,
"full_module": "Vale.Interop.Assumptions",
"short_module": "IA"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_s",
"short_module": "MS"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": "BS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop",
"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 ->
arg_reg: Vale.Interop.X64.arg_reg_relation n ->
regs_modified: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) ->
xmms_modified: (_: Vale.X64.Machine_s.reg_xmm -> Prims.bool) ->
c: Vale.X64.Machine_Semantics_s.code ->
dom: Prims.list Vale.Interop.Base.td ->
args:
Prims.list Vale.Interop.Base.arg
{FStar.List.Tot.Base.length args + FStar.List.Tot.Base.length dom <= 20} ->
pre_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_pre_rel_t c) ->
post_rel: Vale.Interop.X64.rel_gen_t c dom args (Vale.Interop.X64.prediction_post_rel_t c) ->
predict:
Vale.Interop.X64.prediction_t n
arg_reg
regs_modified
xmms_modified
c
dom
args
pre_rel
post_rel
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Vale.Interop.X64.arg_reg_relation",
"Vale.X64.Machine_s.reg_64",
"Prims.bool",
"Vale.X64.Machine_s.reg_xmm",
"Vale.X64.Machine_Semantics_s.code",
"Prims.list",
"Vale.Interop.Base.td",
"Vale.Interop.Base.arg",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.List.Tot.Base.length",
"Vale.Interop.X64.rel_gen_t",
"Vale.Interop.X64.prediction_pre_rel_t",
"Vale.Interop.X64.prediction_post_rel_t",
"Vale.Interop.X64.prediction_t",
"Prims.unit",
"Vale.Interop.X64.als_ret",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Vale.Interop.Base.mem_roots_p",
"Vale.Interop.X64.elim_rel_gen_t_nil",
"Vale.Interop.X64.as_lowstar_sig_post_weak",
"Vale.Interop.X64.elim_predict_t_nil",
"Vale.Interop.Base.td_as_type",
"Vale.Interop.X64.as_lowstar_sig_t_weak'",
"Vale.Interop.X64.op_Plus_Plus",
"Vale.Interop.X64.elim_rel_gen_t_cons",
"Vale.Interop.X64.elim_predict_t_cons"
] | [
"recursion"
] | false | false | false | false | true | let rec as_lowstar_sig_t_weak'
(n: nat)
(arg_reg: arg_reg_relation n)
(regs_modified: (MS.reg_64 -> bool))
(xmms_modified: (MS.reg_xmm -> bool))
(c: BS.code)
(dom: list td)
(args: list arg {List.length args + List.length dom <= 20})
(pre_rel: rel_gen_t c dom args (prediction_pre_rel_t c))
(post_rel: rel_gen_t c dom args (prediction_post_rel_t c))
(predict: prediction_t n arg_reg regs_modified xmms_modified c dom args pre_rel post_rel)
=
| match dom with
| [] ->
(unit
-> FStar.HyperStack.ST.Stack als_ret
(requires (fun h0 -> mem_roots_p h0 args /\ elim_rel_gen_t_nil pre_rel h0))
(ensures
fun h0 ret h1 ->
as_lowstar_sig_post_weak n arg_reg regs_modified xmms_modified c args h0 #pre_rel
#post_rel (elim_predict_t_nil predict) ret h1))
| hd :: tl ->
x: td_as_type hd
-> as_lowstar_sig_t_weak' n arg_reg regs_modified xmms_modified c tl (x ++ args)
(elim_rel_gen_t_cons hd tl pre_rel x) (elim_rel_gen_t_cons hd tl post_rel x)
(elim_predict_t_cons hd tl predict x) | false |
|
Hacl.Impl.MultiExponentiation.fst | Hacl.Impl.MultiExponentiation.lexp_four_fw_loop | val lexp_four_fw_loop:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> table_inv3:table_inv_t a_t len table_len
-> table_inv4:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2
-> pow_a_to_small_b3:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3
-> pow_a_to_small_b4:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4 ->
lexp_four_fw_loop_st a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4 | val lexp_four_fw_loop:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> table_inv3:table_inv_t a_t len table_len
-> table_inv4:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2
-> pow_a_to_small_b3:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3
-> pow_a_to_small_b4:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4 ->
lexp_four_fw_loop_st a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4 | let lexp_four_fw_loop #a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
pow_a_to_small_b1 pow_a_to_small_b2 pow_a_to_small_b3 pow_a_to_small_b4
ctx a1 bLen bBits b1 a2 b2 a3 b3 a4 b4 table1 table2 table3 table4 acc
=
push_frame ();
let tmp = create len (uint #a_t #SEC 0) in
let h0 = ST.get () in
[@ inline_let]
let refl1 i : GTot k.to.a_spec = k.to.refl (as_seq h0 acc) in
[@inline_let]
let spec (h:mem) = S.exp_four_fw_f k.to.comm_monoid
(k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2)
(k.to.refl (as_seq h0 a3)) (BD.bn_v h0 b3)
(k.to.refl (as_seq h0 a4)) (BD.bn_v h0 b4) (v l) in
[@inline_let]
let inv h (i:nat{i <= v bBits / v l}) =
modifies (loc acc |+| loc tmp) h0 h /\
k.to.linv (as_seq h acc) /\
k.to.refl (as_seq h acc) == Loops.repeati i (spec h0) (refl1 0) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2) /\
table_inv3 (as_seq h a3) (as_seq h table3) /\
table_inv4 (as_seq h a4) (as_seq h table4) in
Loops.eq_repeati0 (v bBits / v l) (spec h0) (refl1 0);
Lib.Loops.for 0ul (bBits /. l) inv
(fun i ->
Loops.unfold_repeati (v bBits / v l) (spec h0) (refl1 0) (v i);
lexp_four_fw_f len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
pow_a_to_small_b1 pow_a_to_small_b2 pow_a_to_small_b3 pow_a_to_small_b4
ctx a1 bLen bBits b1 a2 b2 a3 b3 a4 b4 table1 table2 table3 table4 i acc tmp
);
pop_frame () | {
"file_name": "code/bignum/Hacl.Impl.MultiExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 13,
"end_line": 629,
"start_col": 0,
"start_line": 594
} | module Hacl.Impl.MultiExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module Loops = Lib.LoopCombinators
module S = Lib.Exponentiation
module BD = Hacl.Bignum.Definitions
module PT = Hacl.Impl.PrecompTable
friend Hacl.Impl.Exponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
inline_for_extraction noextract
let lexp_double_fw_f_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len) =
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{(v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> i:size_t{v i < v bBits / v l}
-> acc:lbuffer (uint_t a_t SEC) len
-> tmp:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h ->
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h acc /\ live h ctx /\ live h table1 /\ live h table2 /\
live h tmp /\ disjoint tmp a1 /\ disjoint tmp a2 /\
disjoint tmp b1 /\ disjoint tmp b2 /\ disjoint tmp acc /\
disjoint tmp ctx /\ disjoint tmp table1 /\ disjoint tmp table2 /\
eq_or_disjoint a1 a2 /\ disjoint a1 acc /\ disjoint a1 ctx /\
disjoint b1 acc /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint b2 acc /\ disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
k.to.linv (as_seq h acc) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2))
(ensures fun h0 _ h1 -> modifies (loc acc |+| loc tmp) h0 h1 /\
k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
S.exp_double_fw_f #k.to.a_spec k.to.comm_monoid
(k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2) (v l) (v i) (k.to.refl (as_seq h0 acc)))
inline_for_extraction noextract
val lexp_double_fw_f:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2 ->
lexp_double_fw_f_st a_t len ctx_len k l table_len table_inv1 table_inv2
let lexp_double_fw_f #a_t len ctx_len k l table_len table_inv1 table_inv2
pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 i acc tmp =
lexp_fw_f len ctx_len k l table_len table_inv2 pow_a_to_small_b2 ctx a2 bLen bBits b2 table2 i acc tmp;
lmul_acc_pow_a_bits_l len ctx_len k l table_len table_inv1 pow_a_to_small_b1 ctx a1 bLen bBits b1 table1 i acc tmp
inline_for_extraction noextract
let lexp_double_fw_acc0_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len) =
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{0 < v bBits /\ (v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> acc:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h -> v bBits % v l <> 0 /\
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h acc /\ live h ctx /\ live h table1 /\ live h table2 /\
eq_or_disjoint a1 a2 /\ disjoint a1 acc /\ disjoint a1 ctx /\
disjoint b1 acc /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint b2 acc /\ disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2))
(ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\
k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
S.exp_double_fw_acc0 #k.to.a_spec k.to.comm_monoid
(k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2) (v l))
inline_for_extraction noextract
val lexp_double_fw_acc0:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2 ->
lexp_double_fw_acc0_st a_t len ctx_len k l table_len table_inv1 table_inv2
let lexp_double_fw_acc0 #a_t len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 acc =
push_frame ();
let tmp = create len (uint #a_t #SEC 0) in
lexp_fw_acc0 len ctx_len k l table_len table_inv1 pow_a_to_small_b1 ctx a1 bLen bBits b1 table1 acc;
lexp_fw_acc0 len ctx_len k l table_len table_inv2 pow_a_to_small_b2 ctx a2 bLen bBits b2 table2 tmp;
k.lmul ctx acc tmp acc;
pop_frame ()
inline_for_extraction noextract
let lexp_double_fw_loop_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len) =
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{(v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> acc:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h ->
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h acc /\ live h ctx /\ live h table1 /\ live h table2 /\
eq_or_disjoint a1 a2 /\ disjoint a1 acc /\ disjoint a1 ctx /\
disjoint b1 acc /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint b2 acc /\ disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
k.to.linv (as_seq h acc) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2))
(ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\ k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
Loops.repeati (v bBits / v l) (S.exp_double_fw_f k.to.comm_monoid (k.to.refl (as_seq h0 a1))
(v bBits) (BD.bn_v h0 b1) (k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2) (v l)) (k.to.refl (as_seq h0 acc)))
inline_for_extraction noextract
val lexp_double_fw_loop:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2 ->
lexp_double_fw_loop_st a_t len ctx_len k l table_len table_inv1 table_inv2
let lexp_double_fw_loop #a_t len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 acc =
push_frame ();
let tmp = create len (uint #a_t #SEC 0) in
let h0 = ST.get () in
[@ inline_let]
let refl1 i : GTot k.to.a_spec = k.to.refl (as_seq h0 acc) in
[@inline_let]
let spec (h:mem) = S.exp_double_fw_f k.to.comm_monoid (k.to.refl (as_seq h0 a1))
(v bBits) (BD.bn_v h0 b1) (k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2) (v l) in
[@inline_let]
let inv h (i:nat{i <= v bBits / v l}) =
modifies (loc acc |+| loc tmp) h0 h /\
k.to.linv (as_seq h acc) /\
k.to.refl (as_seq h acc) == Loops.repeati i (spec h0) (refl1 0) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2) in
Loops.eq_repeati0 (v bBits / v l) (spec h0) (refl1 0);
Lib.Loops.for 0ul (bBits /. l) inv
(fun i ->
Loops.unfold_repeati (v bBits / v l) (spec h0) (refl1 0) (v i);
lexp_double_fw_f len ctx_len k l table_len table_inv1 table_inv2
pow_a_to_small_b1 pow_a_to_small_b2
ctx a1 bLen bBits b1 a2 b2 table1 table2 i acc tmp
);
pop_frame ()
let mk_lexp_double_fw_tables #a_t len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 res =
assert (v (bBits %. l) == v bBits % v l);
if bBits %. l <> 0ul then
lexp_double_fw_acc0 len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 res
else k.lone ctx res;
lexp_double_fw_loop #a_t len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 res
//-----------------------------------
inline_for_extraction noextract
val lexp_double_fw_gen:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len{1 < v table_len /\ v table_len == pow2 (v l)}
-> lprecomp_get:pow_a_to_small_b_st a_t len ctx_len k l table_len
(table_inv_precomp len ctx_len k l table_len) ->
lexp_double_fw_st a_t len ctx_len k l
#push-options "--z3rlimit 150"
let lexp_double_fw_gen #a_t len ctx_len k l table_len lprecomp_get ctx a1 bLen bBits b1 a2 b2 acc =
push_frame ();
lemma_pow2_is_divisible_by_2 (v l);
let table1 = create (table_len *! len) (uint #a_t #SEC 0) in
PT.lprecomp_table #a_t len ctx_len k ctx a1 table_len table1;
let h0 = ST.get () in
assert (table_inv_precomp len ctx_len k l table_len (as_seq h0 a1) (as_seq h0 table1));
let table2 = create (table_len *! len) (uint #a_t #SEC 0) in
PT.lprecomp_table #a_t len ctx_len k ctx a2 table_len table2;
let h1 = ST.get () in
assert (table_inv_precomp len ctx_len k l table_len (as_seq h1 a1) (as_seq h1 table1));
assert (table_inv_precomp len ctx_len k l table_len (as_seq h1 a2) (as_seq h1 table2));
mk_lexp_double_fw_tables len ctx_len k l table_len
(table_inv_precomp len ctx_len k l table_len)
(table_inv_precomp len ctx_len k l table_len)
lprecomp_get lprecomp_get ctx a1 bLen bBits b1 a2 b2 (to_const table1) (to_const table2) acc;
pop_frame ()
#pop-options
let lexp_double_fw_vartime #a_t len ctx_len k l ctx a1 bLen bBits b1 a2 b2 acc =
[@inline_let]
let table_len = 1ul <<. l in
assert (v table_len == pow2 (v l));
Math.Lemmas.pow2_le_compat (v l) 1;
assert (1 < v table_len /\ v table_len * v len <= max_size_t);
lexp_double_fw_gen len ctx_len k l table_len
(lprecomp_get_vartime len ctx_len k l table_len)
ctx a1 bLen bBits b1 a2 b2 acc
let lexp_double_fw_consttime #a_t len ctx_len k l ctx a1 bLen bBits b1 a2 b2 acc =
[@inline_let]
let table_len = 1ul <<. l in
assert (v table_len == pow2 (v l));
Math.Lemmas.pow2_le_compat (v l) 1;
assert (1 < v table_len /\ v table_len * v len <= max_size_t);
lexp_double_fw_gen len ctx_len k l table_len
(lprecomp_get_consttime len ctx_len k l table_len)
ctx a1 bLen bBits b1 a2 b2 acc
//--------------------------------------------------
inline_for_extraction noextract
let lexp_four_fw_f_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len)
(table_inv3:table_inv_t a_t len table_len)
(table_inv4:table_inv_t a_t len table_len)
=
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{(v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> a3:lbuffer (uint_t a_t SEC) len
-> b3:lbuffer (uint_t a_t SEC) bLen
-> a4:lbuffer (uint_t a_t SEC) len
-> b4:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table3:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table4:clbuffer (uint_t a_t SEC) (table_len *! len)
-> i:size_t{v i < v bBits / v l}
-> acc:lbuffer (uint_t a_t SEC) len
-> tmp:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h ->
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h a3 /\ live h b3 /\ live h a4 /\ live h b4 /\
live h acc /\ live h ctx /\
live h table1 /\ live h table2 /\ live h table3 /\ live h table4 /\
live h tmp /\ disjoint tmp a1 /\ disjoint tmp a2 /\
disjoint tmp a3 /\ disjoint tmp a4 /\ disjoint tmp b1 /\
disjoint tmp b2 /\ disjoint tmp b3 /\ disjoint tmp b4 /\
disjoint tmp acc /\ disjoint tmp ctx /\ disjoint tmp table1 /\
disjoint tmp table2 /\ disjoint tmp table3 /\ disjoint tmp table4 /\
eq_or_disjoint a1 a2 /\ eq_or_disjoint a1 a3 /\ eq_or_disjoint a1 a4 /\
eq_or_disjoint a2 a3 /\ eq_or_disjoint a2 a4 /\ eq_or_disjoint a3 a4 /\
disjoint a1 acc /\ disjoint a1 ctx /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint a3 acc /\ disjoint a3 ctx /\ disjoint a4 acc /\ disjoint a4 ctx /\
disjoint b1 acc /\ disjoint b2 acc /\ disjoint b3 acc /\ disjoint b4 acc /\
disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
disjoint acc table3 /\ disjoint acc table4 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
BD.bn_v h b3 < pow2 (v bBits) /\
BD.bn_v h b4 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
k.to.linv (as_seq h a3) /\ k.to.linv (as_seq h a4) /\
k.to.linv (as_seq h acc) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2) /\
table_inv3 (as_seq h a3) (as_seq h table3) /\
table_inv4 (as_seq h a4) (as_seq h table4))
(ensures fun h0 _ h1 -> modifies (loc acc |+| loc tmp) h0 h1 /\
k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
S.exp_four_fw_f #k.to.a_spec k.to.comm_monoid
(k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2)
(k.to.refl (as_seq h0 a3)) (BD.bn_v h0 b3)
(k.to.refl (as_seq h0 a4)) (BD.bn_v h0 b4) (v l) (v i) (k.to.refl (as_seq h0 acc)))
inline_for_extraction noextract
val lexp_four_fw_f:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> table_inv3:table_inv_t a_t len table_len
-> table_inv4:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2
-> pow_a_to_small_b3:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3
-> pow_a_to_small_b4:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4 ->
lexp_four_fw_f_st a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
let lexp_four_fw_f #a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
pow_a_to_small_b1 pow_a_to_small_b2 pow_a_to_small_b3 pow_a_to_small_b4
ctx a1 bLen bBits b1 a2 b2 a3 b3 a4 b4 table1 table2 table3 table4 i acc tmp
=
lexp_double_fw_f len ctx_len k l table_len table_inv3 table_inv4
pow_a_to_small_b3 pow_a_to_small_b4 ctx a3 bLen bBits b3 a4 b4 table3 table4 i acc tmp;
lmul_acc_pow_a_bits_l len ctx_len k l table_len table_inv2 pow_a_to_small_b2 ctx a2 bLen bBits b2 table2 i acc tmp;
lmul_acc_pow_a_bits_l len ctx_len k l table_len table_inv1 pow_a_to_small_b1 ctx a1 bLen bBits b1 table1 i acc tmp
inline_for_extraction noextract
let lexp_four_fw_acc0_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len)
(table_inv3:table_inv_t a_t len table_len)
(table_inv4:table_inv_t a_t len table_len)
=
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{0 < v bBits /\ (v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> a3:lbuffer (uint_t a_t SEC) len
-> b3:lbuffer (uint_t a_t SEC) bLen
-> a4:lbuffer (uint_t a_t SEC) len
-> b4:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table3:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table4:clbuffer (uint_t a_t SEC) (table_len *! len)
-> acc:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h -> v bBits % v l <> 0 /\
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h a3 /\ live h b3 /\ live h a4 /\ live h b4 /\
live h acc /\ live h ctx /\
live h table1 /\ live h table2 /\ live h table3 /\ live h table4 /\
eq_or_disjoint a1 a2 /\ eq_or_disjoint a1 a3 /\ eq_or_disjoint a1 a4 /\
eq_or_disjoint a2 a3 /\ eq_or_disjoint a2 a4 /\ eq_or_disjoint a3 a4 /\
disjoint a1 acc /\ disjoint a1 ctx /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint a3 acc /\ disjoint a3 ctx /\ disjoint a4 acc /\ disjoint a4 ctx /\
disjoint b1 acc /\ disjoint b2 acc /\ disjoint b3 acc /\ disjoint b4 acc /\
disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
disjoint acc table3 /\ disjoint acc table4 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
BD.bn_v h b3 < pow2 (v bBits) /\
BD.bn_v h b4 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
k.to.linv (as_seq h a3) /\ k.to.linv (as_seq h a4) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2) /\
table_inv3 (as_seq h a3) (as_seq h table3) /\
table_inv4 (as_seq h a4) (as_seq h table4))
(ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\
k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
S.exp_four_fw_acc0 #k.to.a_spec k.to.comm_monoid
(k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2)
(k.to.refl (as_seq h0 a3)) (BD.bn_v h0 b3)
(k.to.refl (as_seq h0 a4)) (BD.bn_v h0 b4) (v l))
inline_for_extraction noextract
val lexp_four_fw_acc0:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> table_inv3:table_inv_t a_t len table_len
-> table_inv4:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2
-> pow_a_to_small_b3:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3
-> pow_a_to_small_b4:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4 ->
lexp_four_fw_acc0_st a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
let lexp_four_fw_acc0 #a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
pow_a_to_small_b1 pow_a_to_small_b2 pow_a_to_small_b3 pow_a_to_small_b4
ctx a1 bLen bBits b1 a2 b2 a3 b3 a4 b4 table1 table2 table3 table4 acc
=
push_frame ();
let tmp = create len (uint #a_t #SEC 0) in
lexp_double_fw_acc0 len ctx_len k l table_len table_inv1 table_inv2 pow_a_to_small_b1 pow_a_to_small_b2 ctx a1 bLen bBits b1 a2 b2 table1 table2 acc;
lexp_double_fw_acc0 len ctx_len k l table_len table_inv3 table_inv4 pow_a_to_small_b3 pow_a_to_small_b4 ctx a3 bLen bBits b3 a4 b4 table3 table4 tmp;
k.lmul ctx acc tmp acc;
pop_frame ()
inline_for_extraction noextract
let lexp_four_fw_loop_st
(a_t:inttype_a)
(len:size_t{v len > 0})
(ctx_len:size_t)
(k:concrete_ops a_t len ctx_len)
(l:size_window_t a_t len)
(table_len:table_len_t len)
(table_inv1:table_inv_t a_t len table_len)
(table_inv2:table_inv_t a_t len table_len)
(table_inv3:table_inv_t a_t len table_len)
(table_inv4:table_inv_t a_t len table_len)
=
ctx:lbuffer (uint_t a_t SEC) ctx_len
-> a1:lbuffer (uint_t a_t SEC) len
-> bLen:size_t
-> bBits:size_t{(v bBits - 1) / bits a_t < v bLen}
-> b1:lbuffer (uint_t a_t SEC) bLen
-> a2:lbuffer (uint_t a_t SEC) len
-> b2:lbuffer (uint_t a_t SEC) bLen
-> a3:lbuffer (uint_t a_t SEC) len
-> b3:lbuffer (uint_t a_t SEC) bLen
-> a4:lbuffer (uint_t a_t SEC) len
-> b4:lbuffer (uint_t a_t SEC) bLen
-> table1:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table2:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table3:clbuffer (uint_t a_t SEC) (table_len *! len)
-> table4:clbuffer (uint_t a_t SEC) (table_len *! len)
-> acc:lbuffer (uint_t a_t SEC) len ->
Stack unit
(requires fun h ->
live h a1 /\ live h b1 /\ live h a2 /\ live h b2 /\
live h a3 /\ live h b3 /\ live h a4 /\ live h b4 /\
live h acc /\ live h ctx /\
live h table1 /\ live h table2 /\ live h table3 /\ live h table4 /\
eq_or_disjoint a1 a2 /\ eq_or_disjoint a1 a3 /\ eq_or_disjoint a1 a4 /\
eq_or_disjoint a2 a3 /\ eq_or_disjoint a2 a4 /\ eq_or_disjoint a3 a4 /\
disjoint a1 acc /\ disjoint a1 ctx /\ disjoint a2 acc /\ disjoint a2 ctx /\
disjoint a3 acc /\ disjoint a3 ctx /\ disjoint a4 acc /\ disjoint a4 ctx /\
disjoint b1 acc /\ disjoint b2 acc /\ disjoint b3 acc /\ disjoint b4 acc /\
disjoint acc ctx /\ disjoint acc table1 /\ disjoint acc table2 /\
disjoint acc table3 /\ disjoint acc table4 /\
BD.bn_v h b1 < pow2 (v bBits) /\
BD.bn_v h b2 < pow2 (v bBits) /\
BD.bn_v h b3 < pow2 (v bBits) /\
BD.bn_v h b4 < pow2 (v bBits) /\
k.to.linv_ctx (as_seq h ctx) /\
k.to.linv (as_seq h a1) /\ k.to.linv (as_seq h a2) /\
k.to.linv (as_seq h a3) /\ k.to.linv (as_seq h a4) /\
k.to.linv (as_seq h acc) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\
table_inv2 (as_seq h a2) (as_seq h table2) /\
table_inv3 (as_seq h a3) (as_seq h table3) /\
table_inv4 (as_seq h a4) (as_seq h table4))
(ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\
k.to.linv (as_seq h1 acc) /\
k.to.refl (as_seq h1 acc) ==
Loops.repeati (v bBits / v l)
(S.exp_four_fw_f k.to.comm_monoid (k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2)
(k.to.refl (as_seq h0 a3)) (BD.bn_v h0 b3)
(k.to.refl (as_seq h0 a4)) (BD.bn_v h0 b4) (v l)) (k.to.refl (as_seq h0 acc)))
inline_for_extraction noextract
val lexp_four_fw_loop:
#a_t:inttype_a
-> len:size_t{v len > 0}
-> ctx_len:size_t
-> k:concrete_ops a_t len ctx_len
-> l:size_window_t a_t len
-> table_len:table_len_t len
-> table_inv1:table_inv_t a_t len table_len
-> table_inv2:table_inv_t a_t len table_len
-> table_inv3:table_inv_t a_t len table_len
-> table_inv4:table_inv_t a_t len table_len
-> pow_a_to_small_b1:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1
-> pow_a_to_small_b2:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2
-> pow_a_to_small_b3:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3
-> pow_a_to_small_b4:pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4 ->
lexp_four_fw_loop_st a_t len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.PrecompTable.fsti.checked",
"Hacl.Impl.Exponentiation.fst.checked",
"Hacl.Bignum.Definitions.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": true,
"source_file": "Hacl.Impl.MultiExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.PrecompTable",
"short_module": "PT"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"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.Impl.Exponentiation",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "S"
},
{
"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.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"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 |
len: Lib.IntTypes.size_t{Lib.IntTypes.v len > 0} ->
ctx_len: Lib.IntTypes.size_t ->
k: Hacl.Impl.Exponentiation.Definitions.concrete_ops a_t len ctx_len ->
l: Hacl.Impl.Exponentiation.size_window_t a_t len ->
table_len: Hacl.Impl.Exponentiation.table_len_t len ->
table_inv1: Hacl.Impl.Exponentiation.table_inv_t a_t len table_len ->
table_inv2: Hacl.Impl.Exponentiation.table_inv_t a_t len table_len ->
table_inv3: Hacl.Impl.Exponentiation.table_inv_t a_t len table_len ->
table_inv4: Hacl.Impl.Exponentiation.table_inv_t a_t len table_len ->
pow_a_to_small_b1:
Hacl.Impl.Exponentiation.pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv1 ->
pow_a_to_small_b2:
Hacl.Impl.Exponentiation.pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv2 ->
pow_a_to_small_b3:
Hacl.Impl.Exponentiation.pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv3 ->
pow_a_to_small_b4:
Hacl.Impl.Exponentiation.pow_a_to_small_b_st a_t len ctx_len k l table_len table_inv4
-> Hacl.Impl.MultiExponentiation.lexp_four_fw_loop_st a_t len ctx_len k l table_len table_inv1
table_inv2 table_inv3 table_inv4 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Impl.Exponentiation.Definitions.inttype_a",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_GreaterThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Exponentiation.Definitions.concrete_ops",
"Hacl.Impl.Exponentiation.size_window_t",
"Hacl.Impl.Exponentiation.table_len_t",
"Hacl.Impl.Exponentiation.table_inv_t",
"Hacl.Impl.Exponentiation.pow_a_to_small_b_st",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint_t",
"Lib.IntTypes.SEC",
"Prims.op_LessThan",
"Prims.op_Division",
"Prims.op_Subtraction",
"Lib.IntTypes.bits",
"Lib.Buffer.clbuffer",
"Lib.IntTypes.op_Star_Bang",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.Loops.for",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.op_Slash_Dot",
"Prims.l_and",
"Prims.op_LessThanOrEqual",
"Hacl.Impl.MultiExponentiation.lexp_four_fw_f",
"Lib.LoopCombinators.unfold_repeati",
"Hacl.Impl.Exponentiation.Definitions.__proj__Mkto_comm_monoid__item__a_spec",
"FStar.Ghost.reveal",
"Hacl.Impl.Exponentiation.Definitions.to_comm_monoid",
"Hacl.Impl.Exponentiation.Definitions.__proj__Mkconcrete_ops__item__to",
"Lib.LoopCombinators.eq_repeati0",
"FStar.Monotonic.HyperStack.mem",
"Prims.nat",
"Prims.logical",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Hacl.Impl.Exponentiation.Definitions.__proj__Mkto_comm_monoid__item__linv",
"Lib.Buffer.as_seq",
"Prims.eq2",
"Hacl.Impl.Exponentiation.Definitions.__proj__Mkto_comm_monoid__item__refl",
"Lib.LoopCombinators.repeati",
"Lib.Buffer.CONST",
"Lib.Exponentiation.exp_four_fw_f",
"Hacl.Impl.Exponentiation.Definitions.__proj__Mkto_comm_monoid__item__comm_monoid",
"Hacl.Bignum.Definitions.bn_v",
"Prims.int",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let lexp_four_fw_loop
#a_t
len
ctx_len
k
l
table_len
table_inv1
table_inv2
table_inv3
table_inv4
pow_a_to_small_b1
pow_a_to_small_b2
pow_a_to_small_b3
pow_a_to_small_b4
ctx
a1
bLen
bBits
b1
a2
b2
a3
b3
a4
b4
table1
table2
table3
table4
acc
=
| push_frame ();
let tmp = create len (uint #a_t #SEC 0) in
let h0 = ST.get () in
[@@ inline_let ]let refl1 i : GTot k.to.a_spec = k.to.refl (as_seq h0 acc) in
[@@ inline_let ]let spec (h: mem) =
S.exp_four_fw_f k.to.comm_monoid (k.to.refl (as_seq h0 a1)) (v bBits) (BD.bn_v h0 b1)
(k.to.refl (as_seq h0 a2)) (BD.bn_v h0 b2) (k.to.refl (as_seq h0 a3)) (BD.bn_v h0 b3)
(k.to.refl (as_seq h0 a4)) (BD.bn_v h0 b4) (v l)
in
[@@ inline_let ]let inv h (i: nat{i <= v bBits / v l}) =
modifies (loc acc |+| loc tmp) h0 h /\ k.to.linv (as_seq h acc) /\
k.to.refl (as_seq h acc) == Loops.repeati i (spec h0) (refl1 0) /\
table_inv1 (as_seq h a1) (as_seq h table1) /\ table_inv2 (as_seq h a2) (as_seq h table2) /\
table_inv3 (as_seq h a3) (as_seq h table3) /\ table_inv4 (as_seq h a4) (as_seq h table4)
in
Loops.eq_repeati0 (v bBits / v l) (spec h0) (refl1 0);
Lib.Loops.for 0ul
(bBits /. l)
inv
(fun i ->
Loops.unfold_repeati (v bBits / v l) (spec h0) (refl1 0) (v i);
lexp_four_fw_f len ctx_len k l table_len table_inv1 table_inv2 table_inv3 table_inv4
pow_a_to_small_b1 pow_a_to_small_b2 pow_a_to_small_b3 pow_a_to_small_b4 ctx a1 bLen bBits b1
a2 b2 a3 b3 a4 b4 table1 table2 table3 table4 i acc tmp);
pop_frame () | false |
Intro.fst | Intro.max_list | val max_list (l1 l2: list int) : Tac (list int) | val max_list (l1 l2: list int) : Tac (list int) | let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2 | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 66,
"end_line": 51,
"start_col": 0,
"start_line": 50
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"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 | l1: Prims.list Prims.int -> l2: Prims.list Prims.int
-> FStar.Tactics.Effect.Tac (Prims.list Prims.int) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Prims.int",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.bool"
] | [] | false | true | false | false | false | let max_list (l1 l2: list int) : Tac (list int) =
| if FStar.List.length l1 > FStar.List.length l2 then l1 else l2 | false |
Intro.fst | Intro.looping_tac | val looping_tac: Prims.unit -> Tac unit | val looping_tac: Prims.unit -> Tac unit | let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac () | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 18,
"end_line": 60,
"start_col": 0,
"start_line": 58
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"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",
"Intro.looping_tac"
] | [
"recursion"
] | false | true | false | false | false | let rec looping_tac () : Tac unit =
| looping_tac () | false |
Intro.fst | Intro.blowup | val blowup: Prims.unit -> Tac unit | val blowup: Prims.unit -> Tac unit | let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> () | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 232,
"start_col": 0,
"start_line": 228
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.NamedView.term",
"FStar.Tactics.V2.Derived.iseq",
"Prims.Cons",
"Intro.blowup",
"Prims.Nil",
"FStar.Tactics.V2.Logic.split",
"FStar.Tactics.NamedView.binding",
"FStar.Tactics.V2.Logic.implies_intro",
"FStar.Reflection.V2.Formula.formula",
"FStar.Tactics.V2.Logic.cur_formula"
] | [
"recursion"
] | false | true | false | false | false | let rec blowup () : Tac unit =
| match cur_formula () with
| And _ _ ->
split ();
iseq [blowup; blowup]
| Implies _ _ ->
let _ = implies_intro () in
blowup ()
| _ -> () | false |
Intro.fst | Intro.int42 | val int42:int | val int42:int | let int42 : int = _ by (exact (`42)) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 249,
"start_col": 0,
"start_line": 249
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.int | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let int42:int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ -> (exact (`42))) | false |
Intro.fst | Intro.recursive_tac | val recursive_tac (n: nat) : Tac unit | val recursive_tac (n: nat) : Tac unit | let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 56,
"start_col": 0,
"start_line": 53
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"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: Prims.nat -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.nat",
"Prims.op_Equality",
"Prims.int",
"Prims.unit",
"Prims.bool",
"Intro.recursive_tac",
"Prims.op_Subtraction"
] | [
"recursion"
] | false | true | false | false | false | let rec recursive_tac (n: nat) : Tac unit =
| if n = 0 then () else recursive_tac (n - 1) | false |
Intro.fst | Intro.suc | val suc : x: Prims.int -> Prims.int | let suc (x:int) = x + 1 | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 23,
"end_line": 260,
"start_col": 0,
"start_line": 260
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | x: Prims.int -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.op_Addition"
] | [] | false | false | false | true | false | let suc (x: int) =
| x + 1 | false |
|
Intro.fst | Intro.triang_aux | val triang_aux (n: pos) : Lemma (n + ((n - 1) * n) / 2 == (n * (n + 1)) / 2) | val triang_aux (n: pos) : Lemma (n + ((n - 1) * n) / 2 == (n * (n + 1)) / 2) | let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
} | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 144,
"start_col": 0,
"start_line": 136
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.pos -> FStar.Pervasives.Lemma (ensures n + (n - 1) * n / 2 == n * (n + 1) / 2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.eq2",
"Prims.op_Addition",
"Prims.op_Division",
"FStar.Mul.op_Star",
"Prims.op_Subtraction",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"FStar.Math.Lemmas.lemma_div_plus",
"Prims.squash",
"FStar.Math.Lemmas.distributivity_add_right",
"FStar.Math.Lemmas.swap_mul",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let triang_aux (n: pos) : Lemma (n + ((n - 1) * n) / 2 == (n * (n + 1)) / 2) =
| calc ( == ) {
n + ((n - 1) * n) / 2;
( == ) { FStar.Math.Lemmas.lemma_div_plus ((n - 1) * n) 2 n }
((n - 1) * n + n * 2) / 2;
( == ) { (FStar.Math.Lemmas.swap_mul n (n - 1);
FStar.Math.Lemmas.distributivity_add_right n (n - 1) 2) }
(n * (n + 1)) / 2;
} | false |
Intro.fst | Intro.triang | val triang (n: nat) : Tot nat | val triang (n: nat) : Tot nat | let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 133,
"start_col": 0,
"start_line": 132
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.nat -> Prims.nat | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"Prims.op_Addition",
"Intro.triang",
"Prims.op_Subtraction"
] | [
"recursion"
] | false | false | false | true | false | let rec triang (n: nat) : Tot nat =
| if n = 0 then 0 else n + triang (n - 1) | false |
Intro.fst | Intro.constr | val constr (a b: prop) : Lemma (a ==> b ==> b /\ a) | val constr (a b: prop) : Lemma (a ==> b ==> b /\ a) | let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ()) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 17,
"end_line": 115,
"start_col": 0,
"start_line": 108
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"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 | a: Prims.prop -> b: Prims.prop -> FStar.Pervasives.Lemma (ensures a ==> b ==> b /\ a) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.prop",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.l_imp",
"Prims.l_and",
"Prims.unit",
"FStar.Tactics.V2.Derived.qed",
"FStar.Tactics.V2.Logic.hyp",
"FStar.Tactics.V2.SyntaxCoercions.binding_to_namedv",
"FStar.Tactics.V2.Logic.split",
"FStar.Tactics.NamedView.binding",
"FStar.Tactics.V2.Logic.implies_intro",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let constr (a b: prop) : Lemma (a ==> b ==> b /\ a) =
| FStar.Tactics.Effect.assert_by_tactic (a ==> b ==> b /\ a)
(fun _ ->
();
(let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())) | false |
Intro.fst | Intro.test_good | val test_good (a: nat) : Lemma (triang a + triang a == a * (a + 1)) | val test_good (a: nat) : Lemma (triang a + triang a == a * (a + 1)) | let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
() | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 179,
"start_col": 0,
"start_line": 174
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | a: Prims.nat -> FStar.Pervasives.Lemma (ensures Intro.triang a + Intro.triang a == a * (a + 1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.unit",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"FStar.Tactics.V2.Derived.qed",
"FStar.Tactics.V2.Derived.apply_lemma",
"Intro.triang",
"Prims.op_Division",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let test_good (a: nat) : Lemma (triang a + triang a == a * (a + 1)) =
| FStar.Tactics.Effect.assert_by_tactic (triang a == (a * (a + 1) / 2))
(fun _ ->
();
(apply_lemma (`gauss);
qed ()));
FStar.Tactics.Effect.assert_by_tactic ((a * (a + 1)) % 2 == 0)
(fun _ ->
();
(apply_lemma (`prod_even);
qed ()));
() | false |
Intro.fst | Intro.ten | val ten:int | val ten:int | let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0)) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 70,
"end_line": 267,
"start_col": 0,
"start_line": 267
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.int | Prims.Tot | [
"total"
] | [] | [
"Intro.suc"
] | [] | false | false | false | true | false | let ten:int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ ->
(repeatN 10 (fun () -> apply (`suc));
exact (`0))) | false |
Intro.fst | Intro.gauss | val gauss (n: nat) : Lemma (triang n == (n * (n + 1)) / 2) | val gauss (n: nat) : Lemma (triang n == (n * (n + 1)) / 2) | let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 150,
"start_col": 0,
"start_line": 146
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.nat -> FStar.Pervasives.Lemma (ensures Intro.triang n == n * (n + 1) / 2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.op_disEquality",
"Prims.int",
"Intro.triang_aux",
"Prims.unit",
"Intro.gauss",
"Prims.op_Subtraction",
"Prims.bool",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Intro.triang",
"Prims.op_Division",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec gauss (n: nat) : Lemma (triang n == (n * (n + 1)) / 2) =
| if n <> 0
then
(gauss (n - 1);
triang_aux n) | false |
Intro.fst | Intro.big | val big:int | val big:int | let big : int = _ by (sums 4) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 280,
"start_col": 0,
"start_line": 280
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.int | Prims.Tot | [
"total"
] | [] | [
"Prims.op_Addition"
] | [] | false | false | false | true | false | let big:int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ -> (sums 4)) | false |
Intro.fst | Intro.prod_even | val prod_even (n: int) : Lemma ((n * (n + 1)) % 2 == 0) | val prod_even (n: int) : Lemma ((n * (n + 1)) % 2 == 0) | let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2 | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 51,
"end_line": 154,
"start_col": 0,
"start_line": 152
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.int -> FStar.Pervasives.Lemma (ensures n * (n + 1) % 2 == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.int",
"FStar.Math.Lemmas.lemma_mod_mul_distr_l",
"Prims.op_Addition",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let prod_even (n: int) : Lemma ((n * (n + 1)) % 2 == 0) =
| FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n + 1) 2 | false |
Intro.fst | Intro.add_2 | val add_2 (a b: int) : Tot int | val add_2 (a b: int) : Tot int | let add_2 : a:int -> b:int -> Tot int = _ by (make_add ()) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 304,
"start_col": 0,
"start_line": 304
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run [] | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | a: Prims.int -> b: Prims.int -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.op_Addition"
] | [] | false | false | false | true | false | let add_2: a: int -> b: int -> Tot int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ -> (make_add ())) | false |
Intro.fst | Intro.make_add | val make_add: Prims.unit -> Tac unit | val make_add: Prims.unit -> Tac unit | let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run [] | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 10,
"end_line": 302,
"start_col": 0,
"start_line": 291
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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",
"Prims.Nil",
"FStar.Tactics.NamedView.binding",
"Prims.list",
"FStar.Tactics.NamedView.binder",
"FStar.Tactics.NamedView.comp",
"Prims.Cons",
"FStar.Stubs.Reflection.V2.Data.binding",
"FStar.Stubs.Tactics.V2.Builtins.intro",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Tactics.V2.Derived.exact",
"FStar.Tactics.Util.iter",
"FStar.Tactics.V2.SyntaxCoercions.binding_to_term",
"FStar.Tactics.V2.Derived.apply",
"FStar.Tactics.NamedView.inspect",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.V2.Derived.cur_goal",
"FStar.Stubs.Reflection.Types.typ"
] | [] | false | true | false | false | false | let make_add () : Tac unit =
| let rec __run (bs: list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b :: bs)
| _ ->
iter (fun b ->
apply (`( + ));
exact (binding_to_term b))
bs;
exact (`0)
in
__run [] | false |
Intro.fst | Intro.add_3 | val add_3 (a b c: int) : Tot int | val add_3 (a b c: int) : Tot int | let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ()) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 67,
"end_line": 305,
"start_col": 0,
"start_line": 305
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run [] | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | a: Prims.int -> b: Prims.int -> c: Prims.int -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.op_Addition"
] | [] | false | false | false | true | false | let add_3: a: int -> b: int -> c: int -> Tot int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ -> (make_add ())) | false |
Intro.fst | Intro.test_blowup | val test_blowup : _: Prims.unit -> Prims.unit | let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state") | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 236,
"start_col": 0,
"start_line": 234
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.l_and",
"Prims.l_True",
"Prims.l_imp",
"Prims.l_False",
"FStar.Stubs.Tactics.V2.Builtins.dump",
"Intro.blowup"
] | [] | false | false | false | true | false | let test_blowup () =
| FStar.Tactics.Effect.assert_by_tactic (True /\ (False ==> (True /\ False)))
(fun _ ->
();
(blowup ();
dump "Final state")) | false |
|
Intro.fst | Intro.add_4 | val add_4 (a b c d: int) : Tot int | val add_4 (a b c d: int) : Tot int | let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ()) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 76,
"end_line": 306,
"start_col": 0,
"start_line": 306
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ()) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | a: Prims.int -> b: Prims.int -> c: Prims.int -> d: Prims.int -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.op_Addition"
] | [] | false | false | false | true | false | let add_4: a: int -> b: int -> c: int -> d: int -> Tot int =
| FStar.Tactics.Effect.synth_by_tactic (fun _ -> (make_add ())) | false |
Intro.fst | Intro.repeatN | val repeatN (n: nat) (t: (unit -> Tac unit)) : Tac unit | val repeatN (n: nat) (t: (unit -> Tac unit)) : Tac unit | let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 265,
"start_col": 0,
"start_line": 262
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.nat -> t: (_: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit)
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.nat",
"Prims.unit",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"Intro.repeatN",
"Prims.op_Subtraction"
] | [
"recursion"
] | false | true | false | false | false | let rec repeatN (n: nat) (t: (unit -> Tac unit)) : Tac unit =
| if n = 0
then ()
else
(t ();
repeatN (n - 1) t) | false |
Intro.fst | Intro.diag | val diag (x: int) (#[exact (quote x)] y: int) : tuple2 int int | val diag (x: int) (#[exact (quote x)] y: int) : tuple2 int int | let diag (x : int) (#[exact (quote x)] y : int) : tuple2 int int = (x, y) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 73,
"end_line": 341,
"start_col": 0,
"start_line": 341
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*)
let id (#a:Type) (x:a) = x
let call_id () = id 5
#push-options "--print_implicits"
(*
Run C-c C-s C-p call_id here, the implicit argument was resolved to
`int` since `5` has type `int`.
*)
#pop-options
(*
In other cases, there is perhaps no single correct solution via
unification, but there might however be some strategy to solve them.
Meta-F* provides a way to solve implicits via tactics, which allows
programmers to write their own strategies. As a simple example, let's
write a "diagonal" function whose second arguments is inferred to be the
same as the first one, unless explicitly provided.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | x: Prims.int -> Prims.int * Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Pervasives.Native.tuple2"
] | [] | false | false | false | true | false | let diag (x: int) (#[exact (quote x)] y: int) : tuple2 int int =
| (x, y) | false |
Intro.fst | Intro.call_id | val call_id : _: Prims.unit -> Prims.int | let call_id () = id 5 | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 21,
"end_line": 321,
"start_col": 0,
"start_line": 321
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*)
let id (#a:Type) (x:a) = x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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.int | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"Intro.id",
"Prims.int"
] | [] | false | false | false | true | false | let call_id () =
| id 5 | false |
|
Intro.fst | Intro.id | val id : x: a -> a | let id (#a:Type) (x:a) = x | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 319,
"start_col": 0,
"start_line": 319
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | x: a -> a | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let id (#a: Type) (x: a) =
| x | false |
|
Intro.fst | Intro.add3 | val add3 (#a: Type) {| _: add a |} (x y z: a) : a | val add3 (#a: Type) {| _: add a |} (x y z: a) : a | let add3 (#a:Type) {|add a|} (x y z : a) : a = sum x (sum y z) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 62,
"end_line": 363,
"start_col": 0,
"start_line": 363
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*)
let id (#a:Type) (x:a) = x
let call_id () = id 5
#push-options "--print_implicits"
(*
Run C-c C-s C-p call_id here, the implicit argument was resolved to
`int` since `5` has type `int`.
*)
#pop-options
(*
In other cases, there is perhaps no single correct solution via
unification, but there might however be some strategy to solve them.
Meta-F* provides a way to solve implicits via tactics, which allows
programmers to write their own strategies. As a simple example, let's
write a "diagonal" function whose second arguments is inferred to be the
same as the first one, unless explicitly provided.
*)
let diag (x : int) (#[exact (quote x)] y : int) : tuple2 int int = (x, y)
(* When the implicit is not given, this is simply a pair of an element
with itself. Note how in this case, there is no information that fully
defines the implicit from the types, and any integer is a correct
solution. *)
let _ = assert (diag 5 == (5, 5))
(* But one can override it *)
let _ = assert (diag 5 #23 == (5, 23))
(* As a more interesting example, this is exactly how typeclasses are
implemented in F*. See the type of add3 below , it contains an implicit
argument of type `add a`, which is to be resolved by the `tcresolve`
tactic. *)
open FStar.Tactics.Typeclasses
class add 'a = {
sum : 'a -> 'a -> 'a
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.Typeclasses",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | {| _: Intro.add a |} -> x: a -> y: a -> z: a -> a | Prims.Tot | [
"total"
] | [] | [
"Intro.add",
"Intro.sum"
] | [] | false | false | false | true | false | let add3 (#a: Type) {| _: add a |} (x y z: a) : a =
| sum x (sum y z) | false |
Intro.fst | Intro.add_int | [@@ FStar.Tactics.Typeclasses.tcinstance]
val add_int:add int | [@@ FStar.Tactics.Typeclasses.tcinstance]
val add_int:add int | instance add_int : add int = { sum = (+) } | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 367,
"start_col": 0,
"start_line": 367
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*)
let id (#a:Type) (x:a) = x
let call_id () = id 5
#push-options "--print_implicits"
(*
Run C-c C-s C-p call_id here, the implicit argument was resolved to
`int` since `5` has type `int`.
*)
#pop-options
(*
In other cases, there is perhaps no single correct solution via
unification, but there might however be some strategy to solve them.
Meta-F* provides a way to solve implicits via tactics, which allows
programmers to write their own strategies. As a simple example, let's
write a "diagonal" function whose second arguments is inferred to be the
same as the first one, unless explicitly provided.
*)
let diag (x : int) (#[exact (quote x)] y : int) : tuple2 int int = (x, y)
(* When the implicit is not given, this is simply a pair of an element
with itself. Note how in this case, there is no information that fully
defines the implicit from the types, and any integer is a correct
solution. *)
let _ = assert (diag 5 == (5, 5))
(* But one can override it *)
let _ = assert (diag 5 #23 == (5, 23))
(* As a more interesting example, this is exactly how typeclasses are
implemented in F*. See the type of add3 below , it contains an implicit
argument of type `add a`, which is to be resolved by the `tcresolve`
tactic. *)
open FStar.Tactics.Typeclasses
class add 'a = {
sum : 'a -> 'a -> 'a
}
(* {|add a|} is just sugar for `(#[tcresolve ()] _ : add a)` *)
let add3 (#a:Type) {|add a|} (x y z : a) : a = sum x (sum y z)
(* Instances are just values with a `tcinstance` attribute, which is | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.Typeclasses",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"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 | Intro.add Prims.int | Prims.Tot | [
"total"
] | [] | [
"Intro.Mkadd",
"Prims.int",
"Prims.op_Addition"
] | [] | false | false | false | true | false | [@@ FStar.Tactics.Typeclasses.tcinstance]
let add_int:add int =
| { sum = ( + ) } | false |
Intro.fst | Intro.sums | val sums (n: nat) : Tac unit | val sums (n: nat) : Tac unit | let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 278,
"start_col": 0,
"start_line": 271
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"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: Prims.nat -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.nat",
"Prims.op_Equality",
"Prims.int",
"FStar.Tactics.V2.Derived.exact",
"Prims.unit",
"Prims.bool",
"Intro.sums",
"Prims.op_Subtraction",
"FStar.Tactics.V2.Derived.apply"
] | [
"recursion"
] | false | true | false | false | false | let rec sums (n: nat) : Tac unit =
| if n = 0
then exact (`1)
else
(apply (`( + ));
sums (n - 1);
sums (n - 1)) | false |
Intro.fst | Intro.test_add | val test_add : Prims.unit | let test_add = assert (add3 1 2 3 == 6) | {
"file_name": "examples/tactics/eci19/Intro.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 376,
"start_col": 0,
"start_line": 376
} | module Intro
(* This file is meant to skim through the features of Meta-F*, and not
really to teach you how to use it. That's what the next files are for! *)
(* We always need this module to use Meta-F* *)
open FStar.Tactics.V2
(*
Meta-F* has many purposes. It can be used to discharge proof obligations
(replacing the SMT solver), to simply preprocess obligations
(complementing the SMT solver), for metaprogramming (i.e. the automatic
generation of terms/programs), solving implicit arguments (which can be
seen as a variant of metaprogramming), and more.
We (somewhat loosely) refer to the setting of using Meta-F* for
proofs as "tactics", and to the generation of terms/programs as
"metaprogramming".
(For experts: If you are familiar with other dependent languages such
as Coq/Agda/Lean, you might think these are the same thing. This is not
exactly so in F*, read ahead for details!)
At its core, Meta-F* is a language for describing correct (i.e. sound)
proof state transformations. A proof state is a set of "goals" that need
to be solved by providing a solution or proof for them. For instance,
a goal can be to prove `forall x. x + 1 == 1 + x`, or to construct a
term of type `int -> int -> Tot int`. The language allows to break down,
tweak, and solve these goals incrementally, while ensuring that the
solutions are correct (valid proofs / well-typed terms).
Meta-F* is in fact a subset of F* itself, and not a separate language
(as Coq and Ltac are). Making good use of F*'s effect system, Meta-F* is
embedded via an effect, named `Tac`, which enjoys all good properties of
F* effects (specifications by WPs, call-by-value, direct-style syntax,
type checking/inference, etc). So, let us present the most simple
metaprogram:
*)
let idtac () : Tac unit = ()
(*
We can also make use of all existing code in the PURE and DIV effects,
since they can be lifted into Tac.
*)
let max_list (l1 l2 : list int) : Tac (list int) =
if FStar.List.length l1 > FStar.List.length l2 then l1 else l2
let rec recursive_tac (n:nat) : Tac unit =
if n = 0
then ()
else recursive_tac (n - 1)
let rec looping_tac () : Tac unit =
// Metaprograms need not terminate!
looping_tac ()
(*
Of course, these programs are not so interesting as they do not have
any effect on the (implicit) proof state that they will manipulate.
Interesting metaprograms can (and must!) be built from *primitives*
(found in FStar.Tactic.Builtins) which can inspect and transform the
proof state. For instance, the `dump` primitive will, when run, output
the proof state in a pretty-printed format to the user, along with a
user-defined string.
*)
(* Exercise: try `C-c` `C-s` `C-d` and query the type of `dump` *)
(*
We can use `run_tactic` to test it. The `run_tactic` "hook" takes a term
of type `unit -> Tac unit` and runs it on a trivial empty proof state.
*)
let _ = run_tactic (fun () -> dump "Hello world!")
(*
You should see a "Hello world!" in the *fstar: goals* buffer of Emacs.
*)
(*
Exercise: Try writing and running a metaprogram that will call `dump` to
count down from 10 to 0. The `string_of_int` function from `Prims` might
be useful. You can navigate the dumps in the *fstar: goals* buffer with
PgUp and PgDown.
*)
// let rec countdown (n:nat) : Tac unit = ???
//
// let _ = run_tactic (fun () -> countdown 10)
(*
Now that we have seen some trivial metaprograms, we can start writing
more interesting ones that actually manipulate the proof state. We will
begin by writing some tactics, i.e. metaprograms for proving, and using
them to solve assertions within code (as is common in F* ).
For instance, let us prove the following tautology with an explicit
proof:
*)
let constr (a b : prop) : Lemma (a ==> b ==> b /\ a) =
assert (a ==> b ==> b /\ a)
by (let ha = implies_intro () in
let hb = implies_intro () in
split ();
hyp (binding_to_namedv hb);
hyp (binding_to_namedv ha);
qed ())
(* Exercise: inspect the intermediate proof states to understand the
proof. *)
(*
In the previous example, our proof was completed internally. However, F*
allows for a mixed style of proof where we can leverage the SMT solver
and use it when it works well, without having to fully spell out proofs
or write complex tactics.
For instance, consider the following fragment:
*)
open FStar.Mul
// computes 1 + 2 + .. + n
let rec triang (n : nat) : Tot nat =
if n = 0 then 0 else n + triang (n - 1)
(* Z3 needs a bit of help to prove this reliably... *)
let triang_aux (n : pos) : Lemma (n + ((n-1) * n) / 2 == (n*(n+1)) / 2) =
calc (==) {
n + ((n-1) * n) / 2;
== { FStar.Math.Lemmas.lemma_div_plus ((n-1)*n) 2 n }
((n-1) * n + n * 2) / 2;
== { FStar.Math.Lemmas.swap_mul n (n-1);
FStar.Math.Lemmas.distributivity_add_right n (n-1) 2 }
(n * (n+1)) / 2;
}
let rec gauss (n : nat) : Lemma (triang n == (n * (n + 1)) / 2) =
if n <> 0 then (
gauss (n-1);
triang_aux n
)
let prod_even (n : int) : Lemma ((n * (n + 1)) % 2 == 0) =
(* z3 needs some help *)
FStar.Math.Lemmas.lemma_mod_mul_distr_l n (n+1) 2
[@expect_failure] // this means the next definition is checked to *fail*!
let test (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
//assert (triang a == (a * (a + 1) / 2));
//assert ((a * (a + 1)) % 2 == 0);
()
(*
The test above fails since the SMT cannot find a proof for the .
assertions We did not call the lemmas, and there are no patterns either.
Exercise: check that both assertions indeed fail by changing them to
`assume`s one at a time. Also make the proof work by using explicit
lemma calls.
Instead of doing either of those things, we can use tactics to guide the
proof in a more local fashion, using each lemma where needed.
*)
let test_good (a : nat) : Lemma (triang a + triang a == a * (a + 1)) =
assert (triang a == (a * (a + 1) / 2))
by (apply_lemma (`gauss); qed ());
assert ((a * (a + 1)) % 2 == 0)
by (apply_lemma (`prod_even); qed ());
()
(*
In this case, the postcondition is being proved by the SMT solver,
while each assertion is completely proven by the tactics.
A couple of things are going here. When using the `assert..by` syntax,
the assertion is turned into a proof state with a single goal, and
the associated tactic is run over it. The backtick marks a "static
quotation"; which we won't explain just yet.
Exercise: print and inspect the proof state before and after applying
the lemmas on each assertion.
These proof states single out the chosen assertion from the full VC of
the term, so one can write tactics for specific subobligations instead
of dealing with an (usually unpleasant) VC. In this case, we use the
`apply_lemma` primitive to solve each one.
*)
(*
Metaprograms can also inspect the goals they are invoked on, which
allows a single metaprogram to be useful in many different scenarios.
We call this ability "reflection", and it is mostly provided by the
FStar.Reflection module and the `inspect` tactic primitive. The
FStar.Reflection.Formula library provides a convenient way to use it for
the case of logical formulae.
*)
let _ =
assert (True /\ True)
by (let f = cur_formula () in
match f with
| And _ _ -> dump "It's a conjunction!"
| _ -> fail "Uh-oh!")
let _ =
assert (False ==> True)
by (let f = cur_formula () in
match f with
| Implies _ _ -> dump "It's an implication!"
| _ -> fail "Uh-oh!")
(*
By using these inspection capabilities, we can construct more
automated tactics. For instance, the following one that will split all
conjunctions and introduce all implications:
*)
let rec blowup () : Tac unit =
match cur_formula () with
| And _ _ -> split (); iseq [blowup; blowup]
| Implies _ _ -> let _ = implies_intro () in blowup ()
| _ -> ()
let test_blowup () =
assert (True /\ (False ==> (True /\ False)))
by (blowup (); dump "Final state")
(* Note how the final state has 3 separate goals *)
(*
Besides using Meta-F* for proving, we can use it do metaprogramming.
Using Meta-F* to construct terms follows the same principles as for
proving: there is a set of goals that need to be solved, and primitives
can be used to do so. The most simple way to solve a goal is to provide
a term via `exact`. The way to invoke Meta-F* for term generation is `_
by` syntax shown below:
*)
let int42 : int = _ by (exact (`42))
(* Exercise: print the definition of `int42` *)
(*
Besides providing exact solutions, terms can be built algorithmically by
calling the primitives. For instance, the `apply` primitive takes a term
`f` and solves the current goal with an application of `f` to as many
arguments as needed.
*)
let suc (x:int) = x + 1
let rec repeatN (n:nat) (t : unit -> Tac unit) : Tac unit =
if n = 0
then ()
else (t (); repeatN (n-1) t)
let ten : int = _ by (repeatN 10 (fun () -> apply (`suc)); exact (`0))
(* Exercise: print the definition of `ten` (it's not 10!) *)
let rec sums (n:nat) : Tac unit =
if n = 0
then exact (`1)
else begin
apply (`(+));
sums (n-1);
sums (n-1)
end
let big : int = _ by (sums 4)
(* Exercise: print the definition of `big` *)
(*
We can also use the reflection capabilities of F* to inspect the
goals we need to solve and act accordingly. For instance, here is a
metaprogram that constructs an addition function for any amount of
arguments.
*)
let make_add () : Tac unit =
let rec __run (bs:list binding) : Tac unit =
match inspect (cur_goal ()) with
| Tv_Arrow _ _ ->
let b = intro () in
__run (b::bs)
| _ ->
iter (fun b -> apply (`(+));
exact (binding_to_term b)) bs;
exact (`0)
in
__run []
let add_2 : a:int -> b:int -> Tot int = _ by (make_add ())
let add_3 : a:int -> b:int -> c:int -> Tot int = _ by (make_add ())
let add_4 : a:int -> b:int -> c:int -> d:int -> Tot int = _ by (make_add ())
let _ = assert (add_2 3 4 == 7)
let _ = assert (add_3 3 4 5 == 12)
let _ = assert (add_4 3 4 5 6 == 18)
(*
Another usage of metaprogramming is resolving implicit arguments.
Usually, in dependently typed languages, they are solved via
unification, which works fine when there is a single correct solution
implied by the well-typing of the term.
*)
let id (#a:Type) (x:a) = x
let call_id () = id 5
#push-options "--print_implicits"
(*
Run C-c C-s C-p call_id here, the implicit argument was resolved to
`int` since `5` has type `int`.
*)
#pop-options
(*
In other cases, there is perhaps no single correct solution via
unification, but there might however be some strategy to solve them.
Meta-F* provides a way to solve implicits via tactics, which allows
programmers to write their own strategies. As a simple example, let's
write a "diagonal" function whose second arguments is inferred to be the
same as the first one, unless explicitly provided.
*)
let diag (x : int) (#[exact (quote x)] y : int) : tuple2 int int = (x, y)
(* When the implicit is not given, this is simply a pair of an element
with itself. Note how in this case, there is no information that fully
defines the implicit from the types, and any integer is a correct
solution. *)
let _ = assert (diag 5 == (5, 5))
(* But one can override it *)
let _ = assert (diag 5 #23 == (5, 23))
(* As a more interesting example, this is exactly how typeclasses are
implemented in F*. See the type of add3 below , it contains an implicit
argument of type `add a`, which is to be resolved by the `tcresolve`
tactic. *)
open FStar.Tactics.Typeclasses
class add 'a = {
sum : 'a -> 'a -> 'a
}
(* {|add a|} is just sugar for `(#[tcresolve ()] _ : add a)` *)
let add3 (#a:Type) {|add a|} (x y z : a) : a = sum x (sum y z)
(* Instances are just values with a `tcinstance` attribute, which is
what `tcresolve` looks for. *)
instance add_int : add int = { sum = (+) }
(* Same as
[@tcinstance] let add_int : add int = ...
*)
(* When called over a concrete type with an instance, such as `int`, the
`tcresolve` tactic solves the implicit to the instance. *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Typeclasses.fsti.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.List.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Intro.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.Typeclasses",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"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"
] | [] | [
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Intro.add3",
"Intro.add_int"
] | [] | false | false | false | true | false | let test_add =
| assert (add3 1 2 3 == 6) | false |
|
Pulse.Lib.Fixpoints.fst | Pulse.Lib.Fixpoints.fix_stt_ghost_1 | val fix_stt_ghost_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (x:a -> (y:a{y << x} -> stt_ghost (b y) (pre y) (post y)) -> stt_ghost (b x) (pre x) (post x)))
: x:a -> stt_ghost (b x) (pre x) (post x) | val fix_stt_ghost_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (x:a -> (y:a{y << x} -> stt_ghost (b y) (pre y) (post y)) -> stt_ghost (b x) (pre x) (post x)))
: x:a -> stt_ghost (b x) (pre x) (post x) | let fix_stt_ghost_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (x:a -> (y:a{y << x} -> stt_ghost (b y) (pre y) (post y)) -> stt_ghost (b x) (pre x) (post x)))
: x:a -> stt_ghost (b x) (pre x) (post x)
= fix_1 #a #(fun x -> stt_ghost (b x) (pre x) (post x)) ff | {
"file_name": "share/steel/examples/pulse/lib/Pulse.Lib.Fixpoints.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 60,
"end_line": 34,
"start_col": 0,
"start_line": 31
} | (*
Copyright 2023 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 Pulse.Lib.Fixpoints
open Pulse.Lib.Core
let rec fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x)
= fun x -> ff x (fix_1 ff)
let rec fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x)
= fun x -> ff x (fix_ghost_1 ff) | {
"checked_file": "/",
"dependencies": [
"Pulse.Lib.Core.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Pulse.Lib.Fixpoints.fst"
} | [
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ff:
(x: a -> _: (y: a{y << x} -> Pulse.Lib.Core.stt_ghost (b y) (pre y) (post y))
-> Pulse.Lib.Core.stt_ghost (b x) (pre x) (post x)) ->
x: a
-> Pulse.Lib.Core.stt_ghost (b x) (pre x) (post x) | Prims.Tot | [
"total"
] | [] | [
"Pulse.Lib.Core.vprop",
"Prims.precedes",
"Pulse.Lib.Core.stt_ghost",
"Pulse.Lib.Fixpoints.fix_1"
] | [] | false | false | false | false | false | let fix_stt_ghost_1
(#a: Type)
(#b: (a -> Type))
(#pre: (a -> vprop))
(#post: (x: a -> b x -> vprop))
(ff:
(x: a -> (y: a{y << x} -> stt_ghost (b y) (pre y) (post y))
-> stt_ghost (b x) (pre x) (post x)))
(x: a) : stt_ghost (b x) (pre x) (post x) =
| fix_1 #a #(fun x -> stt_ghost (b x) (pre x) (post x)) ff | false |
Pulse.Lib.Fixpoints.fst | Pulse.Lib.Fixpoints.fix_1 | val fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x) | val fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x) | let rec fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x)
= fun x -> ff x (fix_1 ff) | {
"file_name": "share/steel/examples/pulse/lib/Pulse.Lib.Fixpoints.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 28,
"end_line": 24,
"start_col": 0,
"start_line": 21
} | (*
Copyright 2023 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 Pulse.Lib.Fixpoints
open Pulse.Lib.Core | {
"checked_file": "/",
"dependencies": [
"Pulse.Lib.Core.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Pulse.Lib.Fixpoints.fst"
} | [
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | ff: (x: a -> _: (y: a{y << x} -> b y) -> b x) -> x: a -> b x | Prims.Tot | [
"total"
] | [] | [
"Prims.precedes",
"Pulse.Lib.Fixpoints.fix_1"
] | [
"recursion"
] | false | false | false | false | false | let rec fix_1 (#a: Type) (#b: (a -> Type)) (ff: (x: a -> (y: a{y << x} -> Tot (b y)) -> Tot (b x)))
(x: a) : Tot (b x) =
| fun x -> ff x (fix_1 ff) | false |
Pulse.Lib.Fixpoints.fst | Pulse.Lib.Fixpoints.fix_ghost_1 | val fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x) | val fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x) | let rec fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x)
= fun x -> ff x (fix_ghost_1 ff) | {
"file_name": "share/steel/examples/pulse/lib/Pulse.Lib.Fixpoints.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 34,
"end_line": 29,
"start_col": 0,
"start_line": 26
} | (*
Copyright 2023 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 Pulse.Lib.Fixpoints
open Pulse.Lib.Core
let rec fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x)
= fun x -> ff x (fix_1 ff) | {
"checked_file": "/",
"dependencies": [
"Pulse.Lib.Core.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Pulse.Lib.Fixpoints.fst"
} | [
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | ff: (x: a -> _: (y: a{y << x} -> Prims.GTot (b y)) -> Prims.GTot (b x)) -> x: a -> Prims.GTot (b x) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.precedes",
"Pulse.Lib.Fixpoints.fix_ghost_1"
] | [
"recursion"
] | false | false | false | false | false | let rec fix_ghost_1
(#a: Type0)
(#b: (a -> Type0))
(ff: (x: a -> (y: a{y << x} -> GTot (b y)) -> GTot (b x)))
(x: a) : GTot (b x) =
| fun x -> ff x (fix_ghost_1 ff) | false |
Pulse.Lib.Fixpoints.fst | Pulse.Lib.Fixpoints.fix_stt_1_div | val fix_stt_1_div:
#a: Type ->
#b: (a -> Type) ->
#pre: (a -> vprop) ->
#post: (x: a -> b x -> vprop) ->
kk:
((y: a -> unit -> Dv (stt (b y) (pre y) (post y))) -> x: a -> unit
-> Dv (stt (b x) (pre x) (post x))) ->
x: a ->
unit
-> Dv (stt (b x) (pre x) (post x)) | val fix_stt_1_div:
#a: Type ->
#b: (a -> Type) ->
#pre: (a -> vprop) ->
#post: (x: a -> b x -> vprop) ->
kk:
((y: a -> unit -> Dv (stt (b y) (pre y) (post y))) -> x: a -> unit
-> Dv (stt (b x) (pre x) (post x))) ->
x: a ->
unit
-> Dv (stt (b x) (pre x) (post x)) | let fix_stt_1_div
(#a : Type)
(#b : a -> Type)
(#pre : a -> vprop)
(#post : (x:a -> b x -> vprop))
(kk : ((y:a -> unit -> Dv (stt (b y) (pre y) (post y))) ->
x:a -> unit -> Dv (stt (b x) (pre x) (post x))))
: x:a -> unit -> Dv (stt (b x) (pre x) (post x))
= let rec f (x:a) (_:unit) : Dv (stt (b x) (pre x) (post x)) =
kk (fun y () -> f y ()) x ()
in
f | {
"file_name": "share/steel/examples/pulse/lib/Pulse.Lib.Fixpoints.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 4,
"end_line": 49,
"start_col": 0,
"start_line": 38
} | (*
Copyright 2023 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 Pulse.Lib.Fixpoints
open Pulse.Lib.Core
let rec fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x)
= fun x -> ff x (fix_1 ff)
let rec fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x)
= fun x -> ff x (fix_ghost_1 ff)
let fix_stt_ghost_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (x:a -> (y:a{y << x} -> stt_ghost (b y) (pre y) (post y)) -> stt_ghost (b x) (pre x) (post x)))
: x:a -> stt_ghost (b x) (pre x) (post x)
= fix_1 #a #(fun x -> stt_ghost (b x) (pre x) (post x)) ff
(* No termination check needed by dropping into STT *) | {
"checked_file": "/",
"dependencies": [
"Pulse.Lib.Core.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Pulse.Lib.Fixpoints.fst"
} | [
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"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 |
kk:
(
_:
(y: a -> _: Prims.unit
-> FStar.Pervasives.Dv (Pulse.Lib.Core.stt (b y) (pre y) (post y))) ->
x: a ->
_: Prims.unit
-> FStar.Pervasives.Dv (Pulse.Lib.Core.stt (b x) (pre x) (post x))) ->
x: a ->
_: Prims.unit
-> FStar.Pervasives.Dv (Pulse.Lib.Core.stt (b x) (pre x) (post x)) | FStar.Pervasives.Dv | [] | [] | [
"Pulse.Lib.Core.vprop",
"Prims.unit",
"Pulse.Lib.Core.stt"
] | [] | false | true | false | false | false | let fix_stt_1_div
(#a: Type)
(#b: (a -> Type))
(#pre: (a -> vprop))
(#post: (x: a -> b x -> vprop))
(kk:
((y: a -> unit -> Dv (stt (b y) (pre y) (post y))) -> x: a -> unit
-> Dv (stt (b x) (pre x) (post x))))
: x: a -> unit -> Dv (stt (b x) (pre x) (post x)) =
| let rec f (x: a) (_: unit) : Dv (stt (b x) (pre x) (post x)) = kk (fun y () -> f y ()) x () in
f | false |
Pulse.Lib.Fixpoints.fst | Pulse.Lib.Fixpoints.fix_stt_1 | val fix_stt_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (y:a -> stt (b y) (pre y) (post y)) -> (x:a -> stt (b x) (pre x) (post x)))
: x:a -> stt (b x) (pre x) (post x) | val fix_stt_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (y:a -> stt (b y) (pre y) (post y)) -> (x:a -> stt (b x) (pre x) (post x)))
: x:a -> stt (b x) (pre x) (post x) | let fix_stt_1
(#a : Type)
(#b : a -> Type)
(#pre : a -> vprop)
(#post : (x:a -> b x -> vprop))
(kk : ((y:a -> stt (b y) (pre y) (post y)) -> x:a -> stt (b x) (pre x) (post x)))
: x:a -> stt (b x) (pre x) (post x)
= fun x ->
hide_div (fix_stt_1_div #a #b #pre #post (fun f x () -> kk (fun y -> hide_div (f y)) x) x) | {
"file_name": "share/steel/examples/pulse/lib/Pulse.Lib.Fixpoints.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 94,
"end_line": 59,
"start_col": 0,
"start_line": 51
} | (*
Copyright 2023 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 Pulse.Lib.Fixpoints
open Pulse.Lib.Core
let rec fix_1 (#a : Type) (#b : a -> Type)
(ff : (x:a -> (y:a{y << x} -> Tot (b y)) -> Tot (b x)))
: x:a -> Tot (b x)
= fun x -> ff x (fix_1 ff)
let rec fix_ghost_1 (#a : Type0) (#b : a -> Type0)
(ff : (x:a -> (y:a{y << x} -> GTot (b y)) -> GTot (b x)))
: x:a -> GTot (b x)
= fun x -> ff x (fix_ghost_1 ff)
let fix_stt_ghost_1 (#a : Type) (#b : a -> Type) (#pre : a -> vprop) (#post : (x:a -> b x -> vprop))
(ff : (x:a -> (y:a{y << x} -> stt_ghost (b y) (pre y) (post y)) -> stt_ghost (b x) (pre x) (post x)))
: x:a -> stt_ghost (b x) (pre x) (post x)
= fix_1 #a #(fun x -> stt_ghost (b x) (pre x) (post x)) ff
(* No termination check needed by dropping into STT *)
let fix_stt_1_div
(#a : Type)
(#b : a -> Type)
(#pre : a -> vprop)
(#post : (x:a -> b x -> vprop))
(kk : ((y:a -> unit -> Dv (stt (b y) (pre y) (post y))) ->
x:a -> unit -> Dv (stt (b x) (pre x) (post x))))
: x:a -> unit -> Dv (stt (b x) (pre x) (post x))
= let rec f (x:a) (_:unit) : Dv (stt (b x) (pre x) (post x)) =
kk (fun y () -> f y ()) x ()
in
f | {
"checked_file": "/",
"dependencies": [
"Pulse.Lib.Core.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Pulse.Lib.Fixpoints.fst"
} | [
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Lib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ff:
(_: (y: a -> Pulse.Lib.Core.stt (b y) (pre y) (post y)) -> x: a
-> Pulse.Lib.Core.stt (b x) (pre x) (post x)) ->
x: a
-> Pulse.Lib.Core.stt (b x) (pre x) (post x) | Prims.Tot | [
"total"
] | [] | [
"Pulse.Lib.Core.vprop",
"Pulse.Lib.Core.stt",
"Pulse.Lib.Core.hide_div",
"Pulse.Lib.Fixpoints.fix_stt_1_div",
"Prims.unit"
] | [] | false | false | false | false | false | let fix_stt_1
(#a: Type)
(#b: (a -> Type))
(#pre: (a -> vprop))
(#post: (x: a -> b x -> vprop))
(kk: ((y: a -> stt (b y) (pre y) (post y)) -> x: a -> stt (b x) (pre x) (post x)))
(x: a) : stt (b x) (pre x) (post x) =
| fun x -> hide_div (fix_stt_1_div #a #b #pre #post (fun f x () -> kk (fun y -> hide_div (f y)) x) x) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.g_is_null | val g_is_null (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot bool | val g_is_null (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot bool | let g_is_null #_ #_ #_ b = Null? b | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 94,
"start_col": 0,
"start_line": 94
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Prims.GTot Prims.bool | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.uu___is_Null",
"Prims.bool"
] | [] | false | false | false | false | false | let g_is_null #_ #_ #_ b =
| Null? b | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.compatible_sub_preorder | val compatible_sub_preorder : len: Prims.nat ->
rel: LowStar.Monotonic.Buffer.srel a ->
i: Prims.nat ->
j: Prims.nat{i <= j /\ j <= len} ->
sub_rel: LowStar.Monotonic.Buffer.srel a
-> Prims.logical | let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 36,
"start_col": 0,
"start_line": 34
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
len: Prims.nat ->
rel: LowStar.Monotonic.Buffer.srel a ->
i: Prims.nat ->
j: Prims.nat{i <= j /\ j <= len} ->
sub_rel: LowStar.Monotonic.Buffer.srel a
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowStar.Monotonic.Buffer.srel",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowStar.Monotonic.Buffer.compatible_subseq_preorder",
"Prims.logical"
] | [] | false | false | false | false | true | let compatible_sub_preorder
(#a: Type0)
(len: nat)
(rel: srel a)
(i: nat)
(j: nat{i <= j /\ j <= len})
(sub_rel: srel a)
=
| compatible_subseq_preorder len rel i j sub_rel | false |
|
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.mnull | val mnull (#a:Type0) (#rrel #rel:srel a) :Tot (b:mbuffer a rrel rel {g_is_null b}) | val mnull (#a:Type0) (#rrel #rel:srel a) :Tot (b:mbuffer a rrel rel {g_is_null b}) | let mnull #_ #_ #_ = Null | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 96,
"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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel {LowStar.Monotonic.Buffer.g_is_null b} | Prims.Tot | [
"total"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.Null",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.b2t",
"LowStar.Monotonic.Buffer.g_is_null"
] | [] | false | false | false | false | false | let mnull #_ #_ #_ =
| Null | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive | val lemma_seq_sub_compatilibity_is_reflexive (#a: Type0) (len: nat) (rel: srel a)
: Lemma (compatible_sub_preorder len rel 0 len rel) | val lemma_seq_sub_compatilibity_is_reflexive (#a: Type0) (len: nat) (rel: srel a)
: Lemma (compatible_sub_preorder len rel 0 len rel) | let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 91,
"end_line": 44,
"start_col": 0,
"start_line": 41
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | len: Prims.nat -> rel: LowStar.Monotonic.Buffer.srel a
-> FStar.Pervasives.Lemma
(ensures LowStar.Monotonic.Buffer.compatible_sub_preorder len rel 0 len rel) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowStar.Monotonic.Buffer.srel",
"Prims._assert",
"Prims.l_Forall",
"FStar.Seq.Base.seq",
"Prims.l_imp",
"Prims.eq2",
"FStar.Seq.Base.length",
"FStar.Seq.Base.equal",
"FStar.Seq.Properties.replace_subseq",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"LowStar.Monotonic.Buffer.compatible_sub_preorder",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lemma_seq_sub_compatilibity_is_reflexive (#a: Type0) (len: nat) (rel: srel a)
: Lemma (compatible_sub_preorder len rel 0 len rel) =
| assert (forall (s1: Seq.seq a) (s2: Seq.seq a).
Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.srel_to_lsrel | val srel_to_lsrel (#a: Type0) (len: nat) (pre: srel a) : P.preorder (Seq.lseq a len) | val srel_to_lsrel (#a: Type0) (len: nat) (pre: srel a) : P.preorder (Seq.lseq a len) | let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 94,
"end_line": 26,
"start_col": 8,
"start_line": 26
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | len: Prims.nat -> pre: LowStar.Monotonic.Buffer.srel a
-> FStar.Preorder.preorder (FStar.Seq.Properties.lseq a len) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowStar.Monotonic.Buffer.srel",
"FStar.Preorder.preorder",
"FStar.Seq.Properties.lseq"
] | [] | false | false | false | false | false | let srel_to_lsrel (#a: Type0) (len: nat) (pre: srel a) : P.preorder (Seq.lseq a len) =
| pre | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.frameOf | val frameOf (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :Tot HS.rid | val frameOf (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :Tot HS.rid | let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 84,
"end_line": 125,
"start_col": 0,
"start_line": 125
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> FStar.Monotonic.HyperHeap.rid | Prims.Tot | [
"total"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.uu___is_Null",
"FStar.Monotonic.HyperHeap.root",
"Prims.bool",
"FStar.Monotonic.HyperStack.frameOf",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__content",
"FStar.Monotonic.HyperHeap.rid"
] | [] | false | false | false | false | false | let frameOf #_ #_ #_ b =
| if Null? b then HS.root else HS.frameOf (Buffer?.content b) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.unused_in | val unused_in (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :GTot Type0 | val unused_in (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :GTot Type0 | let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 52,
"end_line": 103,
"start_col": 0,
"start_line": 100
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> h: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_False",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"FStar.Monotonic.HyperStack.unused_in"
] | [] | false | false | false | false | true | let unused_in #_ #_ #_ b h =
| match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.live | val live (#a:Type0) (#rrel #rel:srel a) (h:HS.mem) (b:mbuffer a rrel rel) :GTot Type0 | val live (#a:Type0) (#rrel #rel:srel a) (h:HS.mem) (b:mbuffer a rrel rel) :GTot Type0 | let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 117,
"start_col": 0,
"start_line": 112
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h: FStar.Monotonic.HyperStack.mem -> b: LowStar.Monotonic.Buffer.mbuffer a rrel rel
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_True",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"Prims.l_and",
"FStar.Monotonic.HyperStack.contains",
"LowStar.Monotonic.Buffer.buffer_compatible"
] | [] | false | false | false | false | true | let live #_ #rrel #rel h b =
| match b with
| Null -> True
| Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.buffer_compatible | val buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 | val buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 | let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 110,
"start_col": 0,
"start_line": 105
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer t rrel rel -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_True",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"LowStar.Monotonic.Buffer.compatible_sub_preorder"
] | [] | false | false | false | false | true | let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
| match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.unused_in_equiv | val unused_in_equiv (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem)
:Lemma (unused_in b h <==>
(HS.live_region h (frameOf b) ==> as_addr b `Heap.addr_unused_in` (Map.sel (HS.get_hmap h) (frameOf b)))) | val unused_in_equiv (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem)
:Lemma (unused_in b h <==>
(HS.live_region h (frameOf b) ==> as_addr b `Heap.addr_unused_in` (Map.sel (HS.get_hmap h) (frameOf b)))) | let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 95,
"end_line": 130,
"start_col": 0,
"start_line": 129
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.unused_in b h <==>
FStar.Monotonic.HyperStack.live_region h (LowStar.Monotonic.Buffer.frameOf b) ==>
FStar.Monotonic.Heap.addr_unused_in (LowStar.Monotonic.Buffer.as_addr b)
(FStar.Map.sel (FStar.Monotonic.HyperStack.get_hmap h) (LowStar.Monotonic.Buffer.frameOf b))
) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.g_is_null",
"FStar.Monotonic.Heap.not_addr_unused_in_nullptr",
"FStar.Map.sel",
"FStar.Monotonic.HyperHeap.rid",
"FStar.Monotonic.Heap.heap",
"FStar.Monotonic.HyperStack.get_hmap",
"FStar.Monotonic.HyperHeap.root",
"Prims.bool",
"Prims.unit"
] | [] | false | false | true | false | false | let unused_in_equiv #_ #_ #_ b h =
| if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.as_seq | val as_seq (#a:Type0) (#rrel #rel:srel a) (h:HS.mem) (b:mbuffer a rrel rel) :GTot (Seq.seq a) | val as_seq (#a:Type0) (#rrel #rel:srel a) (h:HS.mem) (b:mbuffer a rrel rel) :GTot (Seq.seq a) | let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 68,
"end_line": 145,
"start_col": 0,
"start_line": 141
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h: FStar.Monotonic.HyperStack.mem -> b: LowStar.Monotonic.Buffer.mbuffer a rrel rel
-> Prims.GTot (FStar.Seq.Base.seq a) | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Seq.Base.empty",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"FStar.Seq.Base.slice",
"FStar.Monotonic.HyperStack.sel",
"FStar.Seq.Base.seq"
] | [] | false | false | false | false | false | let as_seq #_ #_ #_ h b =
| match b with
| Null -> Seq.empty
| Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.len | val len (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot U32.t | val len (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot U32.t | let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 137,
"start_col": 0,
"start_line": 134
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Prims.GTot FStar.UInt32.t | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal"
] | [] | false | false | false | false | false | let len #_ #_ #_ b =
| match b with
| Null -> 0ul
| Buffer _ _ _ len -> len | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.as_addr | val as_addr (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot nat | val as_addr (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot nat | let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 83,
"end_line": 127,
"start_col": 0,
"start_line": 127
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Prims.GTot Prims.nat | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.g_is_null",
"Prims.bool",
"FStar.Monotonic.HyperStack.as_addr",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__content",
"Prims.nat"
] | [] | false | false | false | false | false | let as_addr #_ #_ #_ b =
| if g_is_null b then 0 else HS.as_addr (Buffer?.content b) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.mgsub | val mgsub (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a)
(b:mbuffer a rrel rel) (i:U32.t) (len:U32.t)
:Ghost (mbuffer a rrel sub_rel)
(requires (U32.v i + U32.v len <= length b))
(ensures (fun _ -> True)) | val mgsub (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a)
(b:mbuffer a rrel rel) (i:U32.t) (len:U32.t)
:Ghost (mbuffer a rrel sub_rel)
(requires (U32.v i + U32.v len <= length b))
(ensures (fun _ -> True)) | let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 59,
"end_line": 155,
"start_col": 0,
"start_line": 151
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sub_rel: LowStar.Monotonic.Buffer.srel a ->
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
i: FStar.UInt32.t ->
len: FStar.UInt32.t
-> Prims.Ghost (LowStar.Monotonic.Buffer.mbuffer a rrel sub_rel) | Prims.Ghost | [] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.Null",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"LowStar.Monotonic.Buffer.Buffer",
"FStar.UInt32.add",
"FStar.Ghost.hide"
] | [] | false | false | false | false | false | let mgsub #a #rrel #rel sub_rel b i len =
| match b with
| Null -> Null
| Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.s_lemma_equal_instances_implies_equal_types | val s_lemma_equal_instances_implies_equal_types: unit
-> Lemma
(forall (a: Type) (b: Type) (s1: Seq.seq a) (s2: Seq.seq b).
{:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b))}
s1 === s2 ==> a == b) | val s_lemma_equal_instances_implies_equal_types: unit
-> Lemma
(forall (a: Type) (b: Type) (s1: Seq.seq a) (s2: Seq.seq b).
{:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b))}
s1 === s2 ==> a == b) | let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types() | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 51,
"end_line": 214,
"start_col": 0,
"start_line": 209
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit
-> FStar.Pervasives.Lemma
(ensures
forall (a: Type) (b: Type) (s1: FStar.Seq.Base.seq a) (s2: FStar.Seq.Base.seq b).
{:pattern
Prims.has_type s1 (FStar.Seq.Base.seq a); Prims.has_type s2 (FStar.Seq.Base.seq b)}
s1 === s2 ==> a == b) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.unit",
"FStar.Seq.Base.lemma_equal_instances_implies_equal_types",
"Prims.l_True",
"Prims.squash",
"Prims.l_Forall",
"FStar.Seq.Base.seq",
"Prims.l_imp",
"Prims.op_Equals_Equals_Equals",
"Prims.eq2",
"Prims.has_type",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let s_lemma_equal_instances_implies_equal_types (_: unit)
: Lemma
(forall (a: Type) (b: Type) (s1: Seq.seq a) (s2: Seq.seq b).
{:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b))}
s1 === s2 ==> a == b) =
| Seq.lemma_equal_instances_implies_equal_types () | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.lemma_seq_sub_compatibility_is_transitive | val lemma_seq_sub_compatibility_is_transitive
(#a: Type0)
(len: nat)
(rel: srel a)
(i1 j1: nat)
(rel1: srel a)
(i2 j2: nat)
(rel2: srel a)
: Lemma
(requires
(i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) | val lemma_seq_sub_compatibility_is_transitive
(#a: Type0)
(len: nat)
(rel: srel a)
(i1 j1: nat)
(rel1: srel a)
(i2 j2: nat)
(rel2: srel a)
: Lemma
(requires
(i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) | let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 64,
"end_line": 83,
"start_col": 0,
"start_line": 51
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
len: Prims.nat ->
rel: LowStar.Monotonic.Buffer.srel a ->
i1: Prims.nat ->
j1: Prims.nat ->
rel1: LowStar.Monotonic.Buffer.srel a ->
i2: Prims.nat ->
j2: Prims.nat ->
rel2: LowStar.Monotonic.Buffer.srel a
-> FStar.Pervasives.Lemma
(requires
i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
LowStar.Monotonic.Buffer.compatible_sub_preorder len rel i1 j1 rel1 /\
LowStar.Monotonic.Buffer.compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)
(ensures LowStar.Monotonic.Buffer.compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowStar.Monotonic.Buffer.srel",
"FStar.Classical.forall_intro_2",
"FStar.Seq.Base.seq",
"Prims.l_imp",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.Classical.arrow_to_impl",
"Prims._assert",
"FStar.Seq.Base.equal",
"FStar.Seq.Properties.replace_subseq",
"FStar.Seq.Base.slice",
"Prims.op_Addition",
"Prims.logical",
"Prims.l_and",
"Prims.eq2",
"FStar.Seq.Base.length",
"Prims.int",
"Prims.op_Subtraction",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowStar.Monotonic.Buffer.compatible_sub_preorder"
] | [] | false | false | true | false | false | let lemma_seq_sub_compatibility_is_transitive
(#a: Type0)
(len: nat)
(rel: srel a)
(i1 j1: nat)
(rel1: srel a)
(i2 j2: nat)
(rel2: srel a)
: Lemma
(requires
(i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) =
| let t1 (s1 s2: Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2: Seq.seq a) =
t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2))
in
let aux0 (s1 s2: Seq.seq a) : Lemma (t1 s1 s2 ==> t2 s1 s2) =
Classical.arrow_to_impl #(t1 s1 s2)
#(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))
)
in
let t1 (s s2: Seq.seq a) =
Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2
in
let t2 (s s2: Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2: Seq.seq a) : Lemma (t1 s s2 ==> t2 s s2) =
Classical.arrow_to_impl #(t1 s s2)
#(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
);
assert (Seq.equal (Seq.replace_subseq s
i1
j1
(Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0;
Classical.forall_intro_2 aux1 | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.gsub_zero_length | val gsub_zero_length (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel)
:Lemma (compatible_sub b 0ul (len b) rel /\ b == mgsub rel b 0ul (len b)) | val gsub_zero_length (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel)
:Lemma (compatible_sub b 0ul (len b) rel /\ b == mgsub rel b 0ul (len b)) | let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 91,
"end_line": 196,
"start_col": 0,
"start_line": 196
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.compatible_sub b 0ul (LowStar.Monotonic.Buffer.len b) rel /\
b == LowStar.Monotonic.Buffer.mgsub rel b 0ul (LowStar.Monotonic.Buffer.len b)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive",
"LowStar.Monotonic.Buffer.length",
"Prims.unit"
] | [] | true | false | true | false | false | let gsub_zero_length #_ #_ #rel b =
| lemma_seq_sub_compatilibity_is_reflexive (length b) rel | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.live_gsub | val live_gsub (#a:Type0) (#rrel #rel:srel a)
(h:HS.mem) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a)
:Lemma (requires (U32.v i + U32.v len <= length b /\ compatible_sub b i len sub_rel))
(ensures (live h b <==> (live h (mgsub sub_rel b i len) /\ (exists h0 . {:pattern (live h0 b)} live h0 b))))
[SMTPatOr [
[SMTPat (live h (mgsub sub_rel b i len))];
[SMTPat (live h b); SMTPat (mgsub sub_rel b i len);]
]] | val live_gsub (#a:Type0) (#rrel #rel:srel a)
(h:HS.mem) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a)
:Lemma (requires (U32.v i + U32.v len <= length b /\ compatible_sub b i len sub_rel))
(ensures (live h b <==> (live h (mgsub sub_rel b i len) /\ (exists h0 . {:pattern (live h0 b)} live h0 b))))
[SMTPatOr [
[SMTPat (live h (mgsub sub_rel b i len))];
[SMTPat (live h b); SMTPat (mgsub sub_rel b i len);]
]] | let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf () | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 169,
"start_col": 0,
"start_line": 157
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
i: FStar.UInt32.t ->
len: FStar.UInt32.t ->
sub_rel: LowStar.Monotonic.Buffer.srel a
-> FStar.Pervasives.Lemma
(requires
FStar.UInt32.v i + FStar.UInt32.v len <= LowStar.Monotonic.Buffer.length b /\
LowStar.Monotonic.Buffer.compatible_sub b i len sub_rel)
(ensures
LowStar.Monotonic.Buffer.live h b <==>
LowStar.Monotonic.Buffer.live h (LowStar.Monotonic.Buffer.mgsub sub_rel b i len) /\
(exists (h0: FStar.Monotonic.HyperStack.mem). {:pattern LowStar.Monotonic.Buffer.live h0 b}
LowStar.Monotonic.Buffer.live h0 b))
[
SMTPatOr [
[
SMTPat (LowStar.Monotonic.Buffer.live h
(LowStar.Monotonic.Buffer.mgsub sub_rel b i len))
];
[
SMTPat (LowStar.Monotonic.Buffer.live h b);
SMTPat (LowStar.Monotonic.Buffer.mgsub sub_rel b i len)
]
]
] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"FStar.Classical.move_requires",
"Prims.unit",
"LowStar.Monotonic.Buffer.buffer_compatible",
"LowStar.Monotonic.Buffer.mgsub",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowStar.Monotonic.Buffer.lemma_seq_sub_compatibility_is_transitive"
] | [] | false | false | true | false | false | let live_gsub #_ #rrel #rel _ b i len sub_rel =
| match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf ()
: Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len)
rrel
(U32.v idx)
(U32.v idx + U32.v length)
rel
(U32.v i)
(U32.v i + U32.v len)
sub_rel
in
Classical.move_requires prf () | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer' | val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 | val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 | let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 87,
"end_line": 259,
"start_col": 0,
"start_line": 259
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | region: FStar.Monotonic.HyperHeap.rid -> addr: Prims.nat -> Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer_",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_offset",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_length",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_max_length"
] | [] | false | false | false | true | true | let ubuffer' region addr =
| (x: ubuffer_{x.b_offset + x.b_length <= x.b_max_length}) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer | val ubuffer (region: HS.rid) (addr: nat) : Tot Type0 | val ubuffer (region: HS.rid) (addr: nat) : Tot Type0 | let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 86,
"end_line": 261,
"start_col": 0,
"start_line": 261
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | region: FStar.Monotonic.HyperHeap.rid -> addr: Prims.nat -> Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"FStar.Ghost.erased",
"LowStar.Monotonic.Buffer.ubuffer'"
] | [] | false | false | false | true | true | let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 =
| G.erased (ubuffer' region addr) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_preserved | val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 | val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 | let ubuffer_preserved = ubuffer_preserved' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 302,
"start_col": 0,
"start_line": 302
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.ubuffer r a ->
h: FStar.Monotonic.HyperStack.mem ->
h': FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.ubuffer_preserved'"
] | [] | false | false | false | false | true | let ubuffer_preserved =
| ubuffer_preserved' | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.as_seq_gsub | val as_seq_gsub (#a:Type0) (#rrel #rel:srel a)
(h:HS.mem) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a)
:Lemma (requires (U32.v i + U32.v len <= length b))
(ensures (as_seq h (mgsub sub_rel b i len) == Seq.slice (as_seq h b) (U32.v i) (U32.v i + U32.v len)))
[SMTPat (as_seq h (mgsub sub_rel b i len))] | val as_seq_gsub (#a:Type0) (#rrel #rel:srel a)
(h:HS.mem) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a)
:Lemma (requires (U32.v i + U32.v len <= length b))
(ensures (as_seq h (mgsub sub_rel b i len) == Seq.slice (as_seq h b) (U32.v i) (U32.v i + U32.v len)))
[SMTPat (as_seq h (mgsub sub_rel b i len))] | let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 107,
"end_line": 202,
"start_col": 0,
"start_line": 198
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
i: FStar.UInt32.t ->
len: FStar.UInt32.t ->
sub_rel: LowStar.Monotonic.Buffer.srel a
-> FStar.Pervasives.Lemma
(requires FStar.UInt32.v i + FStar.UInt32.v len <= LowStar.Monotonic.Buffer.length b)
(ensures
LowStar.Monotonic.Buffer.as_seq h (LowStar.Monotonic.Buffer.mgsub sub_rel b i len) ==
FStar.Seq.Base.slice (LowStar.Monotonic.Buffer.as_seq h b)
(FStar.UInt32.v i)
(FStar.UInt32.v i + FStar.UInt32.v len))
[SMTPat (LowStar.Monotonic.Buffer.as_seq h (LowStar.Monotonic.Buffer.mgsub sub_rel b i len))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"FStar.Seq.Properties.slice_slice",
"FStar.Monotonic.HyperStack.sel",
"Prims.unit"
] | [] | false | false | true | false | false | let as_seq_gsub #_ #_ #_ h b i len _ =
| match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content)
(U32.v idx)
(U32.v idx + U32.v len0)
(U32.v i)
(U32.v i + U32.v len) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_includes0 | val ubuffer_includes0 : larger: LowStar.Monotonic.Buffer.ubuffer r1 a1 -> smaller: LowStar.Monotonic.Buffer.ubuffer r2 a2
-> Prims.logical | let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 80,
"end_line": 472,
"start_col": 0,
"start_line": 471
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | larger: LowStar.Monotonic.Buffer.ubuffer r1 a1 -> smaller: LowStar.Monotonic.Buffer.ubuffer r2 a2
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"Prims.l_and",
"Prims.eq2",
"LowStar.Monotonic.Buffer.ubuffer_includes'",
"FStar.Ghost.reveal",
"LowStar.Monotonic.Buffer.ubuffer'",
"Prims.logical"
] | [] | false | false | false | false | true | let ubuffer_includes0
(#r1 #r2: HS.rid)
(#a1 #a2: nat)
(larger: ubuffer r1 a1)
(smaller: ubuffer r2 a2)
=
| r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) | false |
|
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_of_buffer | val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) | val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) | let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 419,
"start_col": 0,
"start_line": 419
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel
-> LowStar.Monotonic.Buffer.ubuffer (LowStar.Monotonic.Buffer.frameOf b)
(LowStar.Monotonic.Buffer.as_addr b) | Prims.Tot | [
"total"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.ubuffer_of_buffer'",
"LowStar.Monotonic.Buffer.ubuffer",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr"
] | [] | false | false | false | false | false | let ubuffer_of_buffer #_ #_ #_ b =
| ubuffer_of_buffer' b | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_includes' | val ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 | val ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 | let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 74,
"end_line": 468,
"start_col": 0,
"start_line": 464
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | larger: LowStar.Monotonic.Buffer.ubuffer_ -> smaller: LowStar.Monotonic.Buffer.ubuffer_
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.ubuffer_",
"Prims.l_and",
"Prims.eq2",
"Prims.bool",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_is_mm",
"Prims.nat",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_max_length",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_offset",
"Prims.op_Addition",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_length"
] | [] | false | false | false | false | true | let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
| larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_disjoint0 | val ubuffer_disjoint0 : b1: LowStar.Monotonic.Buffer.ubuffer r1 a1 -> b2: LowStar.Monotonic.Buffer.ubuffer r2 a2
-> Prims.logical | let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 522,
"start_col": 0,
"start_line": 520
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b1: LowStar.Monotonic.Buffer.ubuffer r1 a1 -> b2: LowStar.Monotonic.Buffer.ubuffer r2 a2
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"Prims.l_and",
"Prims.eq2",
"LowStar.Monotonic.Buffer.ubuffer_disjoint'",
"FStar.Ghost.reveal",
"LowStar.Monotonic.Buffer.ubuffer'",
"Prims.logical"
] | [] | false | false | false | false | true | let ubuffer_disjoint0 (#r1 #r2: HS.rid) (#a1 #a2: nat) (b1: ubuffer r1 a1) (b2: ubuffer r2 a2) =
| r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) | false |
|
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.lemma_equal_instances_implies_equal_types | val lemma_equal_instances_implies_equal_types (a b: Type) (s1: Seq.seq a) (s2: Seq.seq b)
: Lemma (requires s1 === s2) (ensures a == b) | val lemma_equal_instances_implies_equal_types (a b: Type) (s1: Seq.seq a) (s2: Seq.seq b)
: Lemma (requires s1 === s2) (ensures a == b) | let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types () | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 52,
"end_line": 207,
"start_col": 0,
"start_line": 204
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type -> b: Type -> s1: FStar.Seq.Base.seq a -> s2: FStar.Seq.Base.seq b
-> FStar.Pervasives.Lemma (requires s1 === s2) (ensures a == b) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.Seq.Base.seq",
"FStar.Seq.Base.lemma_equal_instances_implies_equal_types",
"Prims.unit",
"Prims.op_Equals_Equals_Equals",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let lemma_equal_instances_implies_equal_types (a b: Type) (s1: Seq.seq a) (s2: Seq.seq b)
: Lemma (requires s1 === s2) (ensures a == b) =
| Seq.lemma_equal_instances_implies_equal_types () | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.live_same_addresses_equal_types_and_preorders | val live_same_addresses_equal_types_and_preorders
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) ==> (a1 == a2 /\ rrel1 == rrel2)) | val live_same_addresses_equal_types_and_preorders
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) ==> (a1 == a2 /\ rrel1 == rrel2)) | let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 82,
"end_line": 243,
"start_col": 0,
"start_line": 241
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1' | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b1: LowStar.Monotonic.Buffer.mbuffer a1 rrel1 rel1 ->
b2: LowStar.Monotonic.Buffer.mbuffer a2 rrel2 rel2 ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.frameOf b1 == LowStar.Monotonic.Buffer.frameOf b2 /\
LowStar.Monotonic.Buffer.as_addr b1 == LowStar.Monotonic.Buffer.as_addr b2 /\
LowStar.Monotonic.Buffer.live h b1 /\ LowStar.Monotonic.Buffer.live h b2 /\
~(LowStar.Monotonic.Buffer.g_is_null b1 /\ LowStar.Monotonic.Buffer.g_is_null b2) ==>
a1 == a2 /\ rrel1 == rrel2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"FStar.Classical.move_requires",
"Prims.l_and",
"Prims.eq2",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Monotonic.Buffer.frameOf",
"Prims.nat",
"LowStar.Monotonic.Buffer.as_addr",
"LowStar.Monotonic.Buffer.live",
"Prims.l_not",
"Prims.b2t",
"LowStar.Monotonic.Buffer.g_is_null",
"FStar.Preorder.relation",
"FStar.Seq.Base.seq",
"Prims.l_or",
"FStar.Preorder.preorder_rel",
"LowStar.Monotonic.Buffer.live_same_addresses_equal_types_and_preorders'",
"Prims.unit"
] | [] | false | false | true | false | false | let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h =
| Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_includes | val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 | val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 | let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 76,
"end_line": 476,
"start_col": 0,
"start_line": 476
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | larger: LowStar.Monotonic.Buffer.ubuffer r a -> smaller: LowStar.Monotonic.Buffer.ubuffer r a
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"LowStar.Monotonic.Buffer.ubuffer_includes0"
] | [] | false | false | false | false | true | let ubuffer_includes #r #a larger smaller =
| ubuffer_includes0 larger smaller | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_disjoint' | val ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 | val ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 | let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset)) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 517,
"start_col": 0,
"start_line": 511
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x1: LowStar.Monotonic.Buffer.ubuffer_ -> x2: LowStar.Monotonic.Buffer.ubuffer_ -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.ubuffer_",
"Prims.op_BarBar",
"Prims.op_Equality",
"Prims.int",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_length",
"Prims.l_True",
"Prims.bool",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_max_length",
"Prims.l_or",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"LowStar.Monotonic.Buffer.__proj__Mkubuffer___item__b_offset"
] | [] | false | false | false | false | true | let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
| if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_disjoint | val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 | val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 | let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 525,
"start_col": 0,
"start_line": 525
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b1: LowStar.Monotonic.Buffer.ubuffer r a -> b2: LowStar.Monotonic.Buffer.ubuffer r a
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"LowStar.Monotonic.Buffer.ubuffer_disjoint0"
] | [] | false | false | false | false | true | let ubuffer_disjoint #r #a b1 b2 =
| ubuffer_disjoint0 b1 b2 | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_0_preserves_mreferences | val modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 | val modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 | let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 75,
"end_line": 558,
"start_col": 0,
"start_line": 556
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"FStar.Preorder.preorder",
"FStar.Monotonic.HyperStack.mreference",
"Prims.l_imp",
"FStar.Monotonic.HyperStack.contains",
"Prims.l_and",
"Prims.eq2",
"FStar.Monotonic.HyperStack.sel"
] | [] | false | false | false | false | true | let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
| forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre).
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.live_same_addresses_equal_types_and_preorders' | val live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\
(~(g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) | val live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\
(~(g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) | let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 239,
"start_col": 0,
"start_line": 216
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b1: LowStar.Monotonic.Buffer.mbuffer a1 rrel1 rel1 ->
b2: LowStar.Monotonic.Buffer.mbuffer a2 rrel2 rel2 ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Monotonic.Buffer.frameOf b1 == LowStar.Monotonic.Buffer.frameOf b2 /\
LowStar.Monotonic.Buffer.as_addr b1 == LowStar.Monotonic.Buffer.as_addr b2 /\
LowStar.Monotonic.Buffer.live h b1 /\ LowStar.Monotonic.Buffer.live h b2 /\
~(LowStar.Monotonic.Buffer.g_is_null b1 /\ LowStar.Monotonic.Buffer.g_is_null b2))
(ensures a1 == a2 /\ rrel1 == rrel2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.lemma_equal_instances_implies_equal_types",
"Prims.unit",
"Prims._assert",
"Prims.op_Equals_Equals_Equals",
"FStar.Seq.Base.seq",
"FStar.Pervasives.coerce_eq",
"Prims.eq2",
"LowStar.Monotonic.Buffer.as_seq",
"FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_mm",
"FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_preorders",
"Prims.l_and",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Monotonic.Buffer.frameOf",
"Prims.nat",
"LowStar.Monotonic.Buffer.as_addr",
"LowStar.Monotonic.Buffer.live",
"Prims.l_not",
"Prims.b2t",
"LowStar.Monotonic.Buffer.g_is_null",
"Prims.squash",
"FStar.Preorder.relation",
"Prims.l_or",
"FStar.Preorder.preorder_rel",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\
(~(g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) =
| Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1:Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1':Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1' | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_of_buffer' | val ubuffer_of_buffer' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel)
: Tot (ubuffer (frameOf b) (as_addr b)) | val ubuffer_of_buffer' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel)
: Tot (ubuffer (frameOf b) (as_addr b)) | let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
}) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 6,
"end_line": 279,
"start_col": 0,
"start_line": 263
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: LowStar.Monotonic.Buffer.mbuffer a rrel rel
-> LowStar.Monotonic.Buffer.ubuffer (LowStar.Monotonic.Buffer.frameOf b)
(LowStar.Monotonic.Buffer.as_addr b) | Prims.Tot | [
"total"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.uu___is_Null",
"FStar.Ghost.hide",
"LowStar.Monotonic.Buffer.ubuffer'",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr",
"LowStar.Monotonic.Buffer.Mkubuffer_",
"Prims.bool",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__idx",
"FStar.Ghost.reveal",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__length",
"FStar.Monotonic.HyperStack.is_mm",
"FStar.Seq.Properties.lseq",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__content",
"LowStar.Monotonic.Buffer.ubuffer"
] | [] | false | false | false | false | false | let ubuffer_of_buffer' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel)
: Tot (ubuffer (frameOf b) (as_addr b)) =
| if Null? b
then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false })
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b)
}) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_0_preserves_not_unused_in | val modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 | val modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 | let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 569,
"start_col": 0,
"start_line": 563
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"Prims.l_imp",
"Prims.l_and",
"Prims.b2t",
"FStar.Monotonic.HyperStack.live_region",
"FStar.Monotonic.Heap.addr_unused_in",
"FStar.Map.sel",
"FStar.Monotonic.Heap.heap",
"FStar.Monotonic.HyperStack.get_hmap"
] | [] | false | false | false | false | true | let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
| forall (r: HS.rid) (n: nat).
(HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` ((HS.get_hmap h2) `Map.sel` r)) ==>
(n `Heap.addr_unused_in` ((HS.get_hmap h1) `Map.sel` r)) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_0_preserves_regions | val modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 | val modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 | let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 66,
"end_line": 561,
"start_col": 0,
"start_line": 560
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"FStar.Monotonic.HyperHeap.rid",
"Prims.l_imp",
"Prims.b2t",
"FStar.Monotonic.HyperStack.live_region"
] | [] | false | false | false | false | true | let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
| forall (r: HS.rid). HS.live_region h1 r ==> HS.live_region h2 r | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_0' | val modifies_0' (h1 h2: HS.mem) : GTot Type0 | val modifies_0' (h1 h2: HS.mem) : GTot Type0 | let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 574,
"start_col": 0,
"start_line": 571
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"LowStar.Monotonic.Buffer.modifies_0_preserves_mreferences",
"LowStar.Monotonic.Buffer.modifies_0_preserves_regions",
"LowStar.Monotonic.Buffer.modifies_0_preserves_not_unused_in"
] | [] | false | false | false | false | true | let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
| modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2 | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.ubuffer_preserved' | val ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h': HS.mem) : GTot Type0 | val ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h': HS.mem) : GTot Type0 | let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
))))) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 7,
"end_line": 298,
"start_col": 0,
"start_line": 281
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
}) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.ubuffer r a ->
h: FStar.Monotonic.HyperStack.mem ->
h': FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_imp",
"Prims.l_and",
"Prims.eq2",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr",
"LowStar.Monotonic.Buffer.live",
"Prims.b2t",
"LowStar.Monotonic.Buffer.uu___is_Buffer",
"Prims.bool",
"FStar.UInt32.t",
"FStar.HyperStack.ST.mreference",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"FStar.Ghost.erased",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"FStar.Ghost.reveal",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.Seq.Base.equal",
"FStar.Seq.Base.slice",
"LowStar.Monotonic.Buffer.as_seq",
"Prims.op_Subtraction",
"Prims.logical",
"LowStar.Monotonic.Buffer.ubuffer'"
] | [] | false | false | false | false | true | let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h': HS.mem) : GTot Type0 =
| forall (t': Type0) (rrel: srel t') (rel: srel t') (b': mbuffer t' rrel rel).
((frameOf b' == r /\ as_addr b' == a) ==>
((live h b' ==> live h' b') /\
(((live h b' /\ live h' b' /\ Buffer? b') ==>
(let { b_max_length = bmax ; b_offset = boff ; b_length = blen } = Ghost.reveal b in
let Buffer max _ idx len = b' in
(U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen))
(Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))))))) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.gsub_gsub | val gsub_gsub (#a:Type0) (#rrel #rel:srel a)
(b:mbuffer a rrel rel)
(i1:U32.t) (len1:U32.t) (sub_rel1:srel a)
(i2: U32.t) (len2: U32.t) (sub_rel2:srel a)
:Lemma (requires (U32.v i1 + U32.v len1 <= length b /\
U32.v i2 + U32.v len2 <= U32.v len1))
(ensures (((compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2) ==> compatible_sub b (U32.add i1 i2) len2 sub_rel2) /\
mgsub sub_rel2 (mgsub sub_rel1 b i1 len1) i2 len2 == mgsub sub_rel2 b (U32.add i1 i2) len2))
[SMTPat (mgsub sub_rel2 (mgsub sub_rel1 b i1 len1) i2 len2)] | val gsub_gsub (#a:Type0) (#rrel #rel:srel a)
(b:mbuffer a rrel rel)
(i1:U32.t) (len1:U32.t) (sub_rel1:srel a)
(i2: U32.t) (len2: U32.t) (sub_rel2:srel a)
:Lemma (requires (U32.v i1 + U32.v len1 <= length b /\
U32.v i2 + U32.v len2 <= U32.v len1))
(ensures (((compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2) ==> compatible_sub b (U32.add i1 i2) len2 sub_rel2) /\
mgsub sub_rel2 (mgsub sub_rel1 b i1 len1) i2 len2 == mgsub sub_rel2 b (U32.add i1 i2) len2))
[SMTPat (mgsub sub_rel2 (mgsub sub_rel1 b i1 len1) i2 len2)] | let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf () | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 190,
"start_col": 0,
"start_line": 182
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 20,
"z3rlimit_factor": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
i1: FStar.UInt32.t ->
len1: FStar.UInt32.t ->
sub_rel1: LowStar.Monotonic.Buffer.srel a ->
i2: FStar.UInt32.t ->
len2: FStar.UInt32.t ->
sub_rel2: LowStar.Monotonic.Buffer.srel a
-> FStar.Pervasives.Lemma
(requires
FStar.UInt32.v i1 + FStar.UInt32.v len1 <= LowStar.Monotonic.Buffer.length b /\
FStar.UInt32.v i2 + FStar.UInt32.v len2 <= FStar.UInt32.v len1)
(ensures
(LowStar.Monotonic.Buffer.compatible_sub b i1 len1 sub_rel1 /\
LowStar.Monotonic.Buffer.compatible_sub (LowStar.Monotonic.Buffer.mgsub sub_rel1 b i1 len1
)
i2
len2
sub_rel2 ==>
LowStar.Monotonic.Buffer.compatible_sub b (FStar.UInt32.add i1 i2) len2 sub_rel2) /\
LowStar.Monotonic.Buffer.mgsub sub_rel2
(LowStar.Monotonic.Buffer.mgsub sub_rel1 b i1 len1)
i2
len2 ==
LowStar.Monotonic.Buffer.mgsub sub_rel2 b (FStar.UInt32.add i1 i2) len2)
[
SMTPat (LowStar.Monotonic.Buffer.mgsub sub_rel2
(LowStar.Monotonic.Buffer.mgsub sub_rel1 b i1 len1)
i2
len2)
] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.t",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.l_and",
"LowStar.Monotonic.Buffer.compatible_sub",
"LowStar.Monotonic.Buffer.mgsub",
"FStar.UInt32.add",
"Prims.l_Forall",
"FStar.Seq.Base.seq",
"Prims.l_imp",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"LowStar.Monotonic.Buffer.length",
"FStar.Seq.Base.slice",
"FStar.UInt32.v",
"Prims.op_Addition",
"Prims.int",
"Prims.op_Subtraction",
"FStar.Seq.Properties.replace_subseq",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowStar.Monotonic.Buffer.lemma_seq_sub_compatibility_is_transitive"
] | [] | false | false | true | false | false | let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
| let prf ()
: Lemma
(requires
(compatible_sub b i1 len1 sub_rel1 /\
compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) =
lemma_seq_sub_compatibility_is_transitive (length b)
rel
(U32.v i1)
(U32.v i1 + U32.v len1)
sub_rel1
(U32.v i2)
(U32.v i2 + U32.v len2)
sub_rel2
in
Classical.move_requires prf () | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.unused_in_ubuffer_preserved | val unused_in_ubuffer_preserved
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h h': HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) | val unused_in_ubuffer_preserved
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h h': HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) | let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 89,
"end_line": 462,
"start_col": 0,
"start_line": 454
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
h: FStar.Monotonic.HyperStack.mem ->
h': FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.unused_in b h)
(ensures
LowStar.Monotonic.Buffer.ubuffer_preserved (LowStar.Monotonic.Buffer.ubuffer_of_buffer b)
h
h') | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.addr_unused_in_ubuffer_preserved",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr",
"LowStar.Monotonic.Buffer.ubuffer_of_buffer",
"Prims.unit",
"LowStar.Monotonic.Buffer.unused_in_equiv",
"LowStar.Monotonic.Buffer.null_unique",
"LowStar.Monotonic.Buffer.live_null",
"FStar.Classical.move_requires",
"Prims.l_and",
"LowStar.Monotonic.Buffer.live",
"LowStar.Monotonic.Buffer.unused_in",
"Prims.l_False",
"LowStar.Monotonic.Buffer.live_not_unused_in",
"Prims.squash",
"LowStar.Monotonic.Buffer.ubuffer_preserved",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let unused_in_ubuffer_preserved
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h h': HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) =
| Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.same_mreference_ubuffer_preserved | val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2) | val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2) | let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
) | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 404,
"start_col": 0,
"start_line": 391
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.ubuffer r a ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem ->
f:
(
a': Type0 ->
pre: FStar.Preorder.preorder a' ->
r': FStar.Monotonic.HyperStack.mreference a' pre
-> FStar.Pervasives.Lemma
(requires
FStar.Monotonic.HyperStack.contains h1 r' /\
r == FStar.Monotonic.HyperStack.frameOf r' /\
a == FStar.Monotonic.HyperStack.as_addr r')
(ensures
FStar.Monotonic.HyperStack.contains h2 r' /\
FStar.Monotonic.HyperStack.sel h1 r' == FStar.Monotonic.HyperStack.sel h2 r'))
-> FStar.Pervasives.Lemma (ensures LowStar.Monotonic.Buffer.ubuffer_preserved b h1 h2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.Monotonic.HyperHeap.rid",
"Prims.nat",
"LowStar.Monotonic.Buffer.ubuffer",
"FStar.Monotonic.HyperStack.mem",
"FStar.Preorder.preorder",
"FStar.Monotonic.HyperStack.mreference",
"Prims.unit",
"Prims.l_and",
"FStar.Monotonic.HyperStack.contains",
"Prims.eq2",
"FStar.Monotonic.HyperStack.frameOf",
"Prims.int",
"Prims.l_or",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.op_GreaterThanOrEqual",
"FStar.Monotonic.HyperStack.as_addr",
"Prims.squash",
"FStar.Monotonic.HyperStack.sel",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowStar.Monotonic.Buffer.ubuffer_preserved_intro",
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Monotonic.Buffer.uu___is_Null",
"Prims.bool",
"FStar.Seq.Properties.lseq",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length",
"LowStar.Monotonic.Buffer.srel_to_lsrel",
"LowStar.Monotonic.Buffer.__proj__Buffer__item__content"
] | [] | false | false | true | false | false | let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
| ubuffer_preserved_intro b
h1
h2
(fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b'))
(fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b')) | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_0 | val modifies_0 (h1 h2: HS.mem) : GTot Type0 | val modifies_0 (h1 h2: HS.mem) : GTot Type0 | let modifies_0 = modifies_0' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 578,
"start_col": 0,
"start_line": 578
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.modifies_0'"
] | [] | false | false | false | false | true | let modifies_0 =
| modifies_0' | false |
Hacl.K256.PrecompTable.fst | Hacl.K256.PrecompTable.precomp_g_pow2_192_table_lemma_w4 | val precomp_g_pow2_192_table_lemma_w4: unit ->
Lemma (forall (i:nat{i < 16}). precomp_table_acc_inv g_pow2_192 16 precomp_g_pow2_192_table_lseq_w4 i) | val precomp_g_pow2_192_table_lemma_w4: unit ->
Lemma (forall (i:nat{i < 16}). precomp_table_acc_inv g_pow2_192 16 precomp_g_pow2_192_table_lseq_w4 i) | let precomp_g_pow2_192_table_lemma_w4 () =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15);
SPT.precomp_base_table_lemma mk_k256_precomp_base_table
proj_g_pow2_192 16 precomp_g_pow2_64_table_lseq_w4;
proj_g_pow2_192_lemma () | {
"file_name": "code/k256/Hacl.K256.PrecompTable.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 26,
"end_line": 272,
"start_col": 0,
"start_line": 268
} | module Hacl.K256.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.K256.PrecompTable
module S = Spec.K256
module SL = Spec.K256.Lemmas
open Hacl.Impl.K256.Point
include Hacl.Impl.K256.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 = 0x46ec0aa60b0b98c37b29371784676ad967b7beb1a941ddb6fbbff95b44cb788b in
[@inline_let]
let rY : S.felem = 0x6b946755bbc6b677576579c990a1ccf14a710545251a1428fabbf02f40268e63 in
[@inline_let]
let rZ : S.felem = 0x3c114b2ac17c199ec9eba9f7cc64dc459ca2e53f5bbead2b4e618b318ffcc00e in
(rX, rY, rZ)
val lemma_proj_g_pow2_64_eval : unit ->
Lemma (SE.exp_pow2 S.mk_k256_concrete_ops S.g 64 == proj_g_pow2_64)
let lemma_proj_g_pow2_64_eval () =
SPT256.exp_pow2_rec_is_exp_pow2 S.mk_k256_concrete_ops S.g 64;
let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops S.g 64) in
normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops S.g 64);
let rX : S.felem = 0x46ec0aa60b0b98c37b29371784676ad967b7beb1a941ddb6fbbff95b44cb788b in
let rY : S.felem = 0x6b946755bbc6b677576579c990a1ccf14a710545251a1428fabbf02f40268e63 in
let rZ : S.felem = 0x3c114b2ac17c199ec9eba9f7cc64dc459ca2e53f5bbead2b4e618b318ffcc00e 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 = 0x98299efbc8e459915404ae015b48cac3b929e0158665f3c7fa5489fbd25c4297 in
[@inline_let]
let rY : S.felem = 0xf1e5cbc9579e7d11a31681e947c2959ae0298a006d1c06ab1ad93d6716ea50cc in
[@inline_let]
let rZ : S.felem = 0x5c53ffe15001674a2eacb60c9327a8b0ddbd93a0fa6d90309de6cc124133938b in
(rX, rY, rZ)
val lemma_proj_g_pow2_128_eval : unit ->
Lemma (SE.exp_pow2 S.mk_k256_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_k256_concrete_ops proj_g_pow2_64 64;
let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64) in
normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64);
let rX : S.felem = 0x98299efbc8e459915404ae015b48cac3b929e0158665f3c7fa5489fbd25c4297 in
let rY : S.felem = 0xf1e5cbc9579e7d11a31681e947c2959ae0298a006d1c06ab1ad93d6716ea50cc in
let rZ : S.felem = 0x5c53ffe15001674a2eacb60c9327a8b0ddbd93a0fa6d90309de6cc124133938b 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 = 0xbd382b67d20492b1480ca58a6d7d617ba413a9bc7c2f1cff51301ef960fb245c in
[@inline_let]
let rY : S.felem = 0x0b232afcf692673aa714357c524c07867a64ea3b9dfab53f0e74622159e86b0d in
[@inline_let]
let rZ : S.felem = 0x028a1380449aede5df8219420b458d464a6a4773ac91e8305237878cef1cffa6 in
(rX, rY, rZ)
val lemma_proj_g_pow2_192_eval : unit ->
Lemma (SE.exp_pow2 S.mk_k256_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_k256_concrete_ops proj_g_pow2_128 64;
let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_128 64) in
normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_128 64);
let rX : S.felem = 0xbd382b67d20492b1480ca58a6d7d617ba413a9bc7c2f1cff51301ef960fb245c in
let rY : S.felem = 0x0b232afcf692673aa714357c524c07867a64ea3b9dfab53f0e74622159e86b0d in
let rZ : S.felem = 0x028a1380449aede5df8219420b458d464a6a4773ac91e8305237878cef1cffa6 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_k256_concrete_ops S.g 64)
// let proj_g_pow2_128 : S.proj_point =
// normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64)
// let proj_g_pow2_192 : S.proj_point =
// normalize_term (SPT256.exp_pow2_rec S.mk_k256_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 15 =
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 15 =
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 15 =
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_k256_concrete_ops S.g
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_k256_concrete_ops S.g
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_k256_concrete_ops S.g
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 = 240} =
normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15)
let precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 240 =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15);
Seq.seq_of_list precomp_basepoint_table_list_w4
let precomp_basepoint_table_lemma_w4 () =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15);
SPT.precomp_base_table_lemma mk_k256_precomp_base_table S.g 16 precomp_basepoint_table_lseq_w4
let precomp_basepoint_table_w4:
x:glbuffer uint64 240ul{witnessed x precomp_basepoint_table_lseq_w4 /\ recallable x} =
createL_global precomp_basepoint_table_list_w4
/// window size = 4; precomputed table = [[0]([pow2 64]G), [1]([pow2 64]G), ..., [15]([pow2 64]G)]
inline_for_extraction noextract
let precomp_g_pow2_64_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} =
normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15)
let precomp_g_pow2_64_table_lseq_w4 : LSeq.lseq uint64 240 =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15);
Seq.seq_of_list precomp_g_pow2_64_table_list_w4
let precomp_g_pow2_64_table_lemma_w4 () =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15);
SPT.precomp_base_table_lemma mk_k256_precomp_base_table
proj_g_pow2_64 16 precomp_g_pow2_64_table_lseq_w4;
proj_g_pow2_64_lemma ()
let precomp_g_pow2_64_table_w4:
x:glbuffer uint64 240ul{witnessed x precomp_g_pow2_64_table_lseq_w4 /\ recallable x} =
createL_global precomp_g_pow2_64_table_list_w4
/// window size = 4; precomputed table = [[0]([pow2 128]G), [1]([pow2 128]G),...,[15]([pow2 128]G)]
inline_for_extraction noextract
let precomp_g_pow2_128_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} =
normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15)
let precomp_g_pow2_128_table_lseq_w4 : LSeq.lseq uint64 240 =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15);
Seq.seq_of_list precomp_g_pow2_128_table_list_w4
let precomp_g_pow2_128_table_lemma_w4 () =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15);
SPT.precomp_base_table_lemma mk_k256_precomp_base_table
proj_g_pow2_128 16 precomp_g_pow2_64_table_lseq_w4;
proj_g_pow2_128_lemma ()
let precomp_g_pow2_128_table_w4:
x:glbuffer uint64 240ul{witnessed x precomp_g_pow2_128_table_lseq_w4 /\ recallable x} =
createL_global precomp_g_pow2_128_table_list_w4
/// window size = 4; precomputed table = [[0]([pow2 192]G), [1]([pow2 192]G),...,[15]([pow2 192]G)]
inline_for_extraction noextract
let precomp_g_pow2_192_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} =
normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15)
let precomp_g_pow2_192_table_lseq_w4 : LSeq.lseq uint64 240 =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15);
Seq.seq_of_list precomp_g_pow2_192_table_list_w4 | {
"checked_file": "/",
"dependencies": [
"Spec.K256.Lemmas.fsti.checked",
"Spec.K256.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.K256.PrecompTable.fsti.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"Hacl.Impl.K256.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.K256.PrecompTable.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.K256.Lemmas",
"short_module": "SL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.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.K256.Group",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.K256.Point",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.K256",
"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.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit
-> FStar.Pervasives.Lemma
(ensures
forall (i: Prims.nat{i < 16}).
Hacl.K256.PrecompTable.precomp_table_acc_inv Hacl.K256.PrecompTable.g_pow2_192
16
Hacl.K256.PrecompTable.precomp_g_pow2_192_table_lseq_w4
i) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.unit",
"Hacl.K256.PrecompTable.proj_g_pow2_192_lemma",
"Hacl.Spec.PrecompBaseTable.precomp_base_table_lemma",
"Spec.K256.PointOps.proj_point",
"Lib.IntTypes.U64",
"FStar.UInt32.uint_to_t",
"Hacl.K256.PrecompTable.mk_k256_precomp_base_table",
"Hacl.K256.PrecompTable.proj_g_pow2_192",
"Hacl.K256.PrecompTable.precomp_g_pow2_64_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_g_pow2_192_table_lemma_w4 () =
| normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15);
SPT.precomp_base_table_lemma mk_k256_precomp_base_table
proj_g_pow2_192
16
precomp_g_pow2_64_table_lseq_w4;
proj_g_pow2_192_lemma () | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_1 | val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 | val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 | let modifies_1 = modifies_1' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 646,
"start_col": 0,
"start_line": 646
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0
let modifies_0 = modifies_0'
val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma
(requires (modifies_0 h1 h2 /\ HS.live_region h1 r))
(ensures (HS.live_region h2 r))
let modifies_0_live_region h1 h2 r = ()
val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma
(requires (modifies_0 h1 h2 /\ h1 `HS.contains` r))
(ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r))
let modifies_0_mreference #a #pre h1 h2 r = ()
let modifies_0_ubuffer
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (modifies_0 h1 h2))
(ensures (ubuffer_preserved b h1 h2))
= same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r')
val modifies_0_unused_in
(h1 h2: HS.mem)
(r: HS.rid)
(n: nat)
: Lemma
(requires (
modifies_0 h1 h2 /\
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
))
(ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)))
let modifies_0_unused_in h1 h2 r n = ()
let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
:GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre).
((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==>
(h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r')
let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2
let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2
let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r'
let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= modifies_0_preserves_regions h1 h2 /\
modifies_1_preserves_mreferences b h1 h2 /\
modifies_1_preserves_livenesses b h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2 /\
modifies_1_preserves_ubuffers b h1 h2
val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.modifies_1'"
] | [] | false | false | false | false | true | let modifies_1 =
| modifies_1' | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_1_preserves_ubuffers | val modifies_1_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 | val modifies_1_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 | let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 132,
"end_line": 625,
"start_col": 0,
"start_line": 622
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0
let modifies_0 = modifies_0'
val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma
(requires (modifies_0 h1 h2 /\ HS.live_region h1 r))
(ensures (HS.live_region h2 r))
let modifies_0_live_region h1 h2 r = ()
val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma
(requires (modifies_0 h1 h2 /\ h1 `HS.contains` r))
(ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r))
let modifies_0_mreference #a #pre h1 h2 r = ()
let modifies_0_ubuffer
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (modifies_0 h1 h2))
(ensures (ubuffer_preserved b h1 h2))
= same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r')
val modifies_0_unused_in
(h1 h2: HS.mem)
(r: HS.rid)
(n: nat)
: Lemma
(requires (
modifies_0 h1 h2 /\
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
))
(ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)))
let modifies_0_unused_in h1 h2 r n = ()
let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
:GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre).
((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==>
(h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"LowStar.Monotonic.Buffer.ubuffer",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr",
"Prims.l_imp",
"LowStar.Monotonic.Buffer.ubuffer_disjoint",
"LowStar.Monotonic.Buffer.ubuffer_of_buffer",
"LowStar.Monotonic.Buffer.ubuffer_preserved"
] | [] | false | false | false | false | true | let modifies_1_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 =
| forall (b': ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==>
ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_1_preserves_livenesses | val modifies_1_preserves_livenesses
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 | val modifies_1_preserves_livenesses
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 | let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 119,
"end_line": 634,
"start_col": 0,
"start_line": 632
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0
let modifies_0 = modifies_0'
val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma
(requires (modifies_0 h1 h2 /\ HS.live_region h1 r))
(ensures (HS.live_region h2 r))
let modifies_0_live_region h1 h2 r = ()
val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma
(requires (modifies_0 h1 h2 /\ h1 `HS.contains` r))
(ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r))
let modifies_0_mreference #a #pre h1 h2 r = ()
let modifies_0_ubuffer
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (modifies_0 h1 h2))
(ensures (ubuffer_preserved b h1 h2))
= same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r')
val modifies_0_unused_in
(h1 h2: HS.mem)
(r: HS.rid)
(n: nat)
: Lemma
(requires (
modifies_0 h1 h2 /\
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
))
(ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)))
let modifies_0_unused_in h1 h2 r n = ()
let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
:GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre).
((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==>
(h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r')
let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2
let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"FStar.Preorder.preorder",
"FStar.Monotonic.HyperStack.mreference",
"Prims.l_imp",
"FStar.Monotonic.HyperStack.contains"
] | [] | false | false | false | false | true | let modifies_1_preserves_livenesses
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(h1 h2: HS.mem)
: GTot Type0 =
| forall (a': Type) (pre: Preorder.preorder a') (r': HS.mreference a' pre).
h1 `HS.contains` r' ==> h2 `HS.contains` r' | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_1' | val modifies_1' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (h1 h2: HS.mem)
: GTot Type0 | val modifies_1' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (h1 h2: HS.mem)
: GTot Type0 | let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= modifies_0_preserves_regions h1 h2 /\
modifies_1_preserves_mreferences b h1 h2 /\
modifies_1_preserves_livenesses b h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2 /\
modifies_1_preserves_ubuffers b h1 h2 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 642,
"start_col": 0,
"start_line": 636
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0
let modifies_0 = modifies_0'
val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma
(requires (modifies_0 h1 h2 /\ HS.live_region h1 r))
(ensures (HS.live_region h2 r))
let modifies_0_live_region h1 h2 r = ()
val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma
(requires (modifies_0 h1 h2 /\ h1 `HS.contains` r))
(ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r))
let modifies_0_mreference #a #pre h1 h2 r = ()
let modifies_0_ubuffer
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (modifies_0 h1 h2))
(ensures (ubuffer_preserved b h1 h2))
= same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r')
val modifies_0_unused_in
(h1 h2: HS.mem)
(r: HS.rid)
(n: nat)
: Lemma
(requires (
modifies_0 h1 h2 /\
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
))
(ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)))
let modifies_0_unused_in h1 h2 r n = ()
let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
:GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre).
((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==>
(h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r')
let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2
let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2
let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"LowStar.Monotonic.Buffer.modifies_0_preserves_regions",
"LowStar.Monotonic.Buffer.modifies_1_preserves_mreferences",
"LowStar.Monotonic.Buffer.modifies_1_preserves_livenesses",
"LowStar.Monotonic.Buffer.modifies_0_preserves_not_unused_in",
"LowStar.Monotonic.Buffer.modifies_1_preserves_ubuffers"
] | [] | false | false | false | false | true | let modifies_1' (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (h1 h2: HS.mem)
: GTot Type0 =
| modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\
modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\
modifies_1_preserves_ubuffers b h1 h2 | false |
LowStar.Monotonic.Buffer.fst | LowStar.Monotonic.Buffer.modifies_1_from_to_preserves_ubuffers | val modifies_1_from_to_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(from to: U32.t)
(h1 h2: HS.mem)
: GTot Type0 | val modifies_1_from_to_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(from to: U32.t)
(h1 h2: HS.mem)
: GTot Type0 | let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | {
"file_name": "ulib/LowStar.Monotonic.Buffer.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 148,
"end_line": 630,
"start_col": 0,
"start_line": 627
} | (*
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 LowStar.Monotonic.Buffer
module P = FStar.Preorder
module G = FStar.Ghost
module U32 = FStar.UInt32
module Seq = FStar.Seq
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre
(*
* Counterpart of compatible_sub from the fsti but using sequences
*
* The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise
* The cost is that we have to additional asserts as triggers
*)
let compatible_sub_preorder (#a:Type0)
(len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a)
= compatible_subseq_preorder len rel i j sub_rel
(*
* Reflexivity of the compatibility relation
*)
let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a)
:Lemma (compatible_sub_preorder len rel 0 len rel)
= assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==>
Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2)
(*
* Transitivity of the compatibility relation
*
* i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0)
*)
let lemma_seq_sub_compatibility_is_transitive (#a:Type0)
(len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a)
:Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\
compatible_sub_preorder len rel i1 j1 rel1 /\
compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2))
(ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2))
= let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in
let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in
let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2)
= Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2)
(fun _ ->
assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1));
assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2));
assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2)));
assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2))))
in
let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\
rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in
let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in
let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2)
= Classical.arrow_to_impl #(t1 s s2) #(t2 s s2)
(fun _ ->
assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2));
assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2));
assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)));
assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))
(Seq.replace_subseq s (i1 + i2) (i1 + j2) s2)))
in
Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1
noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 =
| Null
| Buffer:
max_length:U32.t ->
content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) ->
idx:U32.t ->
length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} ->
mbuffer a rrel rel
let g_is_null #_ #_ #_ b = Null? b
let mnull #_ #_ #_ = Null
let null_unique #_ #_ #_ _ = ()
let unused_in #_ #_ #_ b h =
match b with
| Null -> False
| Buffer _ content _ _ -> content `HS.unused_in` h
let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 =
match b with
| Null -> True
| Buffer max_length content idx length ->
compatible_sub_preorder (U32.v max_length) rrel
(U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility
let live #_ #rrel #rel h b =
match b with
| Null -> True
| Buffer max_length content idx length ->
h `HS.contains` content /\
buffer_compatible b
let live_null _ _ _ _ = ()
let live_not_unused_in #_ #_ #_ _ _ = ()
let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = ()
let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b)
let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b)
let unused_in_equiv #_ #_ #_ b h =
if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else ()
let live_region_frameOf #_ #_ #_ _ _ = ()
let len #_ #_ #_ b =
match b with
| Null -> 0ul
| Buffer _ _ _ len -> len
let len_null a _ _ = ()
let as_seq #_ #_ #_ h b =
match b with
| Null -> Seq.empty
| Buffer max_len content idx len ->
Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len)
let length_as_seq #_ #_ #_ _ _ = ()
let mbuffer_injectivity_in_first_preorder () = ()
let mgsub #a #rrel #rel sub_rel b i len =
match b with
| Null -> Null
| Buffer max_len content idx length ->
Buffer max_len content (U32.add idx i) (Ghost.hide len)
let live_gsub #_ #rrel #rel _ b i len sub_rel =
match b with
| Null -> ()
| Buffer max_len content idx length ->
let prf () : Lemma
(requires (buffer_compatible b))
(ensures (buffer_compatible (mgsub sub_rel b i len)))
=
lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel
(U32.v idx) (U32.v idx + U32.v length) rel
(U32.v i) (U32.v i + U32.v len) sub_rel
in
Classical.move_requires prf ()
let gsub_is_null #_ #_ #_ _ _ _ _ = ()
let len_gsub #_ #_ #_ _ _ _ _ = ()
let frameOf_gsub #_ #_ #_ _ _ _ _ = ()
let as_addr_gsub #_ #_ #_ _ _ _ _ = ()
let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = ()
#push-options "--z3rlimit 20"
let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 =
let prf () : Lemma
(requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2))
(ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2))
=
lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1
(U32.v i2) (U32.v i2 + U32.v len2) sub_rel2
in
Classical.move_requires prf ()
#pop-options
/// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and
/// length ``len b``.
let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel
let as_seq_gsub #_ #_ #_ h b i len _ =
match b with
| Null -> ()
| Buffer _ content idx len0 ->
Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len)
let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b)
: Lemma (requires s1 === s2)
(ensures a == b)
= Seq.lemma_equal_instances_implies_equal_types ()
let s_lemma_equal_instances_implies_equal_types (_:unit)
: Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b).
{:pattern (has_type s1 (Seq.seq a));
(has_type s2 (Seq.seq b)) }
s1 === s2 ==> a == b)
= Seq.lemma_equal_instances_implies_equal_types()
let live_same_addresses_equal_types_and_preorders'
(#a1 #a2: Type0)
(#rrel1 #rel1: srel a1)
(#rrel2 #rel2: srel a2)
(b1: mbuffer a1 rrel1 rel1)
(b2: mbuffer a2 rrel2 rel2)
(h: HS.mem)
: Lemma
(requires
frameOf b1 == frameOf b2 /\
as_addr b1 == as_addr b2 /\
live h b1 /\
live h b2 /\
(~ (g_is_null b1 /\ g_is_null b2)))
(ensures
a1 == a2 /\
rrel1 == rrel2)
= Heap.lemma_distinct_addrs_distinct_preorders ();
Heap.lemma_distinct_addrs_distinct_mm ();
let s1 : Seq.seq a1 = as_seq h b1 in
assert (Seq.seq a1 == Seq.seq a2);
let s1' : Seq.seq a2 = coerce_eq _ s1 in
assert (s1 === s1');
lemma_equal_instances_implies_equal_types a1 a2 s1 s1'
let live_same_addresses_equal_types_and_preorders
#_ #_ #_ #_ #_ #_ b1 b2 h
= Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h
(* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *)
noeq
type ubuffer_
: Type0
= {
b_max_length: nat;
b_offset: nat;
b_length: nat;
b_is_mm: bool;
}
val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0
let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } )
let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr)
let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel)
:Tot (ubuffer (frameOf b) (as_addr b))
= if Null? b
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
Ghost.hide ({
b_max_length = U32.v (Buffer?.max_length b);
b_offset = U32.v (Buffer?.idx b);
b_length = U32.v (Buffer?.length b);
b_is_mm = HS.is_mm (Buffer?.content b);
})
let ubuffer_preserved'
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h h' : HS.mem)
: GTot Type0
= forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) .
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0
let ubuffer_preserved = ubuffer_preserved'
let ubuffer_preserved_intro
(#r:HS.rid)
(#a:nat)
(b:ubuffer r a)
(h h' :HS.mem)
(f0: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (frameOf b' == r /\ as_addr b' == a /\ live h b'))
(ensures (live h' b'))
))
(f: (
(t':Type0) ->
(rrel:srel t') -> (rel:srel t') ->
(b':mbuffer t' rrel rel) ->
Lemma
(requires (
frameOf b' == r /\ as_addr b' == a /\
live h b' /\ live h' b' /\
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
))))
(ensures (
Buffer? b' /\ (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len /\
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))
))
: Lemma
(ubuffer_preserved b h h')
= let g'
(t':Type0) (rrel rel:srel t')
(b':mbuffer t' rrel rel)
: Lemma
((frameOf b' == r /\ as_addr b' == a) ==> (
(live h b' ==> live h' b') /\ (
((live h b' /\ live h' b' /\ Buffer? b') ==> (
let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in
let Buffer max _ idx len = b' in (
U32.v max == bmax /\
U32.v idx <= boff /\
boff + blen <= U32.v idx + U32.v len
) ==>
Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen))
)))))
= Classical.move_requires (f0 t' rrel rel) b';
Classical.move_requires (f t' rrel rel) b'
in
Classical.forall_intro_4 g'
val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma
(ubuffer_preserved b h h)
let ubuffer_preserved_refl #r #a b h = ()
val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma
(requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3))
(ensures (ubuffer_preserved b h1 h3))
let ubuffer_preserved_trans #r #a b h1 h2 h3 = ()
val same_mreference_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
(f: (
(a' : Type) ->
(pre: Preorder.preorder a') ->
(r': HS.mreference a' pre) ->
Lemma
(requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r'))
(ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r'))
))
: Lemma
(ubuffer_preserved b h1 h2)
let same_mreference_ubuffer_preserved #r #a b h1 h2 f =
ubuffer_preserved_intro b h1 h2
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
(fun t' _ _ b' ->
if Null? b'
then ()
else
f _ _ (Buffer?.content b')
)
val addr_unused_in_ubuffer_preserved
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r)))
(ensures (ubuffer_preserved b h1 h2))
let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = ()
val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b))
let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b
let ubuffer_of_buffer_from_to_none_cond
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot bool
= g_is_null b || U32.v to < U32.v from || U32.v from > length b
let ubuffer_of_buffer_from_to
#a #rrel #rel (b: mbuffer a rrel rel) from to
: GTot (ubuffer (frameOf b) (as_addr b))
= if ubuffer_of_buffer_from_to_none_cond b from to
then
Ghost.hide ({
b_max_length = 0;
b_offset = 0;
b_length = 0;
b_is_mm = false;
})
else
let to' = if U32.v to > length b then length b else U32.v to in
let b1 = ubuffer_of_buffer b in
Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from })
val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b))
(ensures (live h' b /\ as_seq h b == as_seq h' b))
let ubuffer_preserved_elim #_ #_ #_ _ _ _ = ()
val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem)
:Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b))
(ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to))))
let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = ()
let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a)
(b:mbuffer a rrel rel) (h h':HS.mem)
: Lemma (requires (b `unused_in` h))
(ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'))
= Classical.move_requires (fun b -> live_not_unused_in h b) b;
live_null a rrel rel h;
null_unique b;
unused_in_equiv b h;
addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h'
let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 =
larger.b_is_mm == smaller.b_is_mm /\
larger.b_max_length == smaller.b_max_length /\
larger.b_offset <= smaller.b_offset /\
smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller)
val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0
let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller
val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma
(b `ubuffer_includes` b)
let ubuffer_includes_refl #r #a b = ()
val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma
(requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3))
(ensures (b1 `ubuffer_includes` b3))
let ubuffer_includes_trans #r #a b1 b2 b3 = ()
(*
* TODO: not sure how to make this lemma work with preorders
* it creates a buffer larger' in the proof
* we need a compatible preorder for that
* may be take that as an argument?
*)
(*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma
(requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2))
(ensures (ubuffer_preserved smaller h1 h2))
let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 =
ubuffer_preserved_intro smaller h1 h2 (fun t' b' ->
if Null? b'
then ()
else
let (Buffer max_len content idx' len') = b' in
let idx = U32.uint_to_t (G.reveal larger).b_offset in
let len = U32.uint_to_t (G.reveal larger).b_length in
let larger' = Buffer max_len content idx len in
assert (b' == gsub larger' (U32.sub idx' idx) len');
ubuffer_preserved_elim larger' h1 h2
)*)
let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 =
if x1.b_length = 0 || x2.b_length = 0
then True
else
(x1.b_max_length == x2.b_max_length /\
(x1.b_offset + x1.b_length <= x2.b_offset \/
x2.b_offset + x2.b_length <= x1.b_offset))
(* TODO: added this because of #606, now that it is fixed, we may not need it anymore *)
let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) =
r1 == r2 /\ a1 == a2 /\
ubuffer_disjoint' (G.reveal b1) (G.reveal b2)
val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0
let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2
val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a)
:Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1)
let ubuffer_disjoint_sym #_ #_ b1 b2 = ()
val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma
(requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2))
(ensures (ubuffer_disjoint smaller1 smaller2))
let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = ()
val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a)
(h h':HS.mem) (b:mbuffer a rrel rel)
(f: (
(t':Type0) ->
(pre: Preorder.preorder t') ->
(r: HS.mreference t' pre) ->
Lemma
(requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r))
(ensures (h' `HS.contains` r))
))
:Lemma (requires (live h b)) (ensures (live h' b))
let liveness_preservation_intro #_ #_ #_ _ _ b f =
if Null? b
then ()
else f _ _ (Buffer?.content b)
(* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *)
let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 =
forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) .
h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r)
let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r
let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 =
forall (r: HS.rid) (n: nat) . (
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
) ==> (
n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)
)
let modifies_0' (h1 h2: HS.mem) : GTot Type0 =
modifies_0_preserves_mreferences h1 h2 /\
modifies_0_preserves_regions h1 h2 /\
modifies_0_preserves_not_unused_in h1 h2
val modifies_0 (h1 h2: HS.mem) : GTot Type0
let modifies_0 = modifies_0'
val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma
(requires (modifies_0 h1 h2 /\ HS.live_region h1 r))
(ensures (HS.live_region h2 r))
let modifies_0_live_region h1 h2 r = ()
val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma
(requires (modifies_0 h1 h2 /\ h1 `HS.contains` r))
(ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r))
let modifies_0_mreference #a #pre h1 h2 r = ()
let modifies_0_ubuffer
(#r: HS.rid)
(#a: nat)
(b: ubuffer r a)
(h1 h2: HS.mem)
: Lemma
(requires (modifies_0 h1 h2))
(ensures (ubuffer_preserved b h1 h2))
= same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r')
val modifies_0_unused_in
(h1 h2: HS.mem)
(r: HS.rid)
(n: nat)
: Lemma
(requires (
modifies_0 h1 h2 /\
HS.live_region h1 r /\ HS.live_region h2 r /\
n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)
))
(ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)))
let modifies_0_unused_in h1 h2 r n = ()
let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
:GTot Type0
= forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre).
((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==>
(h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r')
let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem)
: GTot Type0
= forall (b':ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Preorder.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.ModifiesGen.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Heap.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowStar.Monotonic.Buffer.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.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Ghost",
"short_module": "G"
},
{
"abbrev": true,
"full_module": "FStar.Preorder",
"short_module": "P"
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.Monotonic",
"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": 4,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel ->
from: FStar.UInt32.t ->
to: FStar.UInt32.t ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"LowStar.Monotonic.Buffer.srel",
"LowStar.Monotonic.Buffer.mbuffer",
"FStar.UInt32.t",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_Forall",
"LowStar.Monotonic.Buffer.ubuffer",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.as_addr",
"Prims.l_imp",
"LowStar.Monotonic.Buffer.ubuffer_disjoint",
"LowStar.Monotonic.Buffer.ubuffer_of_buffer_from_to",
"LowStar.Monotonic.Buffer.ubuffer_preserved"
] | [] | false | false | false | false | true | let modifies_1_from_to_preserves_ubuffers
(#a: Type0)
(#rrel #rel: srel a)
(b: mbuffer a rrel rel)
(from to: U32.t)
(h1 h2: HS.mem)
: GTot Type0 =
| forall (b': ubuffer (frameOf b) (as_addr b)).
(ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==>
ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.