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
DoublyLinkedList.fst
DoublyLinkedList.loc_includes_union_l_nodelist_fp0
val loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2: loc) (nl: nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))]
val loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2: loc) (nl: nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))]
let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 252, "start_col": 0, "start_line": 247 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
s1: LowStar.Monotonic.Buffer.loc -> s2: LowStar.Monotonic.Buffer.loc -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.loc_includes s1 (DoublyLinkedList.nodelist_fp0 nl) \/ LowStar.Monotonic.Buffer.loc_includes s2 (DoublyLinkedList.nodelist_fp0 nl)) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.nodelist_fp0 nl)) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.nodelist_fp0 nl)) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.nodelist", "LowStar.Monotonic.Buffer.loc_includes_union_l", "DoublyLinkedList.nodelist_fp0", "Prims.unit", "Prims.l_or", "LowStar.Monotonic.Buffer.loc_includes", "Prims.squash", "LowStar.Monotonic.Buffer.loc_union", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
true
false
true
false
false
let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2: loc) (nl: nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] =
loc_includes_union_l s1 s2 (nodelist_fp0 nl)
false
DoublyLinkedList.fst
DoublyLinkedList.fragment_conn
val fragment_conn (#t: Type) (h0: heap) (f: fragment t) : GTot Type0
val fragment_conn (#t: Type) (h0: heap) (f: fragment t) : GTot Type0
let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 376, "start_col": 0, "start_line": 375 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> f: DoublyLinkedList.fragment t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.fragment", "DoublyLinkedList.fragment_for_each1", "DoublyLinkedList.piece_conn" ]
[ "recursion" ]
false
false
false
false
true
let rec fragment_conn (#t: Type) (h0: heap) (f: fragment t) : GTot Type0 =
fragment_for_each1 piece_conn h0 f
false
DoublyLinkedList.fst
DoublyLinkedList.fragment_valid
val fragment_valid (#t: Type) (h0: heap) (f: fragment t) : GTot Type0
val fragment_valid (#t: Type) (h0: heap) (f: fragment t) : GTot Type0
let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 407, "start_col": 0, "start_line": 403 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> f: DoublyLinkedList.fragment t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.fragment", "Prims.l_and", "DoublyLinkedList.fragment_ghostly_connections", "DoublyLinkedList.fragment_contained", "DoublyLinkedList.fragment_aa", "DoublyLinkedList.fragment_conn" ]
[]
false
false
false
false
true
let fragment_valid (#t: Type) (h0: heap) (f: fragment t) : GTot Type0 =
fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f
false
DoublyLinkedList.fst
DoublyLinkedList.piece_ghostly_connections
val piece_ghostly_connections (#t: Type) (p: piece t) : GTot Type0
val piece_ghostly_connections (#t: Type) (p: piece t) : GTot Type0
let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 152, "start_col": 0, "start_line": 147 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: DoublyLinkedList.piece t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.piece", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "Prims.l_False", "Prims.int", "Prims.l_and", "Prims.eq2", "DoublyLinkedList.__proj__Mkpiece__item__phead", "FStar.List.Tot.Base.hd", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "FStar.List.Tot.Base.last", "FStar.Ghost.erased", "DoublyLinkedList.__proj__Mkpiece__item__pnodes" ]
[]
false
false
false
false
true
let piece_ghostly_connections (#t: Type) (p: piece t) : GTot Type0 =
let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes
false
DoublyLinkedList.fst
DoublyLinkedList.loc_includes_union_l_piece_fp0
val loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2: loc) (p: piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))]
val loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2: loc) (p: piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))]
let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 266, "start_col": 0, "start_line": 261 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
s1: LowStar.Monotonic.Buffer.loc -> s2: LowStar.Monotonic.Buffer.loc -> p: DoublyLinkedList.piece t -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.loc_includes s1 (DoublyLinkedList.piece_fp0 p) \/ LowStar.Monotonic.Buffer.loc_includes s2 (DoublyLinkedList.piece_fp0 p)) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.piece_fp0 p)) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.piece_fp0 p)) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.piece", "LowStar.Monotonic.Buffer.loc_includes_union_l", "DoublyLinkedList.piece_fp0", "Prims.unit", "Prims.l_or", "LowStar.Monotonic.Buffer.loc_includes", "Prims.squash", "LowStar.Monotonic.Buffer.loc_union", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
true
false
true
false
false
let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2: loc) (p: piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] =
loc_includes_union_l s1 s2 (piece_fp0 p)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_aa_r
val nodelist_aa_r (#t: Type) (nl: nodelist t) : GTot Type0
val nodelist_aa_r (#t: Type) (nl: nodelist t) : GTot Type0
let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 319, "start_col": 0, "start_line": 314 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.l_True", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.l_and", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "DoublyLinkedList.nodelist_aa_r" ]
[ "recursion" ]
false
false
false
false
true
let rec nodelist_aa_r (#t: Type) (nl: nodelist t) : GTot Type0 =
match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns
false
Hacl.HPKE.Curve64_CP32_SHA512.fsti
Hacl.HPKE.Curve64_CP32_SHA512.cs
val cs:S.ciphersuite
val cs:S.ciphersuite
let cs:S.ciphersuite = (DH.DH_Curve25519, Hash.SHA2_256, S.Seal AEAD.CHACHA20_POLY1305, Hash.SHA2_512)
{ "file_name": "code/hpke/Hacl.HPKE.Curve64_CP32_SHA512.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 102, "end_line": 10, "start_col": 0, "start_line": 10 }
module Hacl.HPKE.Curve64_CP32_SHA512 open Hacl.Impl.HPKE module S = Spec.Agile.HPKE module DH = Spec.Agile.DH module AEAD = Spec.Agile.AEAD module Hash = Spec.Agile.Hash
{ "checked_file": "/", "dependencies": [ "Vale.X64.CPU_Features_s.fst.checked", "Spec.Agile.HPKE.fsti.checked", "Spec.Agile.Hash.fsti.checked", "Spec.Agile.DH.fst.checked", "Spec.Agile.AEAD.fsti.checked", "prims.fst.checked", "Hacl.Impl.HPKE.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": false, "source_file": "Hacl.HPKE.Curve64_CP32_SHA512.fsti" }
[ { "abbrev": true, "full_module": "Spec.Agile.Hash", "short_module": "Hash" }, { "abbrev": true, "full_module": "Spec.Agile.AEAD", "short_module": "AEAD" }, { "abbrev": true, "full_module": "Spec.Agile.DH", "short_module": "DH" }, { "abbrev": true, "full_module": "Spec.Agile.HPKE", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.HPKE", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HPKE", "short_module": null }, { "abbrev": false, "full_module": "Hacl.HPKE", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Spec.Agile.HPKE.ciphersuite
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.Mktuple4", "Spec.Agile.DH.algorithm", "Spec.Agile.HPKE.hash_algorithm", "Spec.Agile.HPKE.aead", "Spec.Hash.Definitions.hash_alg", "Spec.Agile.DH.DH_Curve25519", "Spec.Hash.Definitions.SHA2_256", "Spec.Agile.HPKE.Seal", "Spec.Agile.AEAD.CHACHA20_POLY1305", "Spec.Hash.Definitions.SHA2_512" ]
[]
false
false
false
true
false
let cs:S.ciphersuite =
(DH.DH_Curve25519, Hash.SHA2_256, S.Seal AEAD.CHACHA20_POLY1305, Hash.SHA2_512)
false
DoublyLinkedList.fst
DoublyLinkedList.loc_includes_union_l_fragment_fp0
val loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2: loc) (f: fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))]
val loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2: loc) (f: fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))]
let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 273, "start_col": 0, "start_line": 268 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
s1: LowStar.Monotonic.Buffer.loc -> s2: LowStar.Monotonic.Buffer.loc -> f: DoublyLinkedList.fragment t -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.loc_includes s1 (DoublyLinkedList.fragment_fp0 f) \/ LowStar.Monotonic.Buffer.loc_includes s2 (DoublyLinkedList.fragment_fp0 f)) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.fragment_fp0 f)) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_union s1 s2) (DoublyLinkedList.fragment_fp0 f)) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.fragment", "LowStar.Monotonic.Buffer.loc_includes_union_l", "DoublyLinkedList.fragment_fp0", "Prims.unit", "Prims.l_or", "LowStar.Monotonic.Buffer.loc_includes", "Prims.squash", "LowStar.Monotonic.Buffer.loc_union", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
true
false
true
false
false
let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2: loc) (f: fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] =
loc_includes_union_l s1 s2 (fragment_fp0 f)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_conn
val nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : GTot Type0 (decreases (length nl))
val nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : GTot Type0 (decreases (length nl))
let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 365, "start_col": 0, "start_line": 357 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial", "" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "Prims.l_True", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.l_and", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "DoublyLinkedList.op_Less_Less_Bar", "DoublyLinkedList.nodelist_conn" ]
[ "recursion" ]
false
false
false
false
true
let rec nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : GTot Type0 (decreases (length nl)) =
match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1 @ h0 |>> n2 /\ n1 <<| n2 @ h0 /\ nodelist_conn h0 rest
false
DoublyLinkedList.fst
DoublyLinkedList.dll_conn
val dll_conn (#t: Type) (h0: heap) (d: dll t) : GTot Type0
val dll_conn (#t: Type) (h0: heap) (d: dll t) : GTot Type0
let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 370, "start_col": 0, "start_line": 367 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> d: DoublyLinkedList.dll t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.dll", "Prims.l_and", "DoublyLinkedList.nodelist_conn", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.l_imp", "Prims.l_not", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.node", "DoublyLinkedList.__proj__Mkdll__item__lhead", "LowStar.Buffer.null", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.op_At", "DoublyLinkedList.__proj__Mkdll__item__ltail", "DoublyLinkedList.__proj__Mknode__item__flink" ]
[]
false
false
false
false
true
let dll_conn (#t: Type) (h0: heap) (d: dll t) : GTot Type0 =
nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead @ h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail @ h0).flink == null)
false
DoublyLinkedList.fst
DoublyLinkedList.fragment_aa_lr
val fragment_aa_lr (#t: Type) (f: fragment t) : GTot Type0
val fragment_aa_lr (#t: Type) (f: fragment t) : GTot Type0
let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1)))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 72, "end_line": 345, "start_col": 0, "start_line": 338 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 =
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: DoublyLinkedList.fragment t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.fragment", "Prims.l_True", "DoublyLinkedList.piece", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.piece_fp0", "Prims.l_and" ]
[ "recursion" ]
false
false
false
false
true
let rec fragment_aa_lr (#t: Type) (f: fragment t) : GTot Type0 =
match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1)))
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_aa_l
val nodelist_aa_l (#t: Type) (nl: nodelist t) : GTot Type0 (decreases (length nl))
val nodelist_aa_l (#t: Type) (nl: nodelist t) : GTot Type0 (decreases (length nl))
let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 326, "start_col": 0, "start_line": 320 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial", "" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.l_True", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.l_and", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "DoublyLinkedList.nodelist_aa_l", "Prims.unit", "FStar.List.Tot.Properties.lemma_unsnoc_length", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.unsnoc" ]
[ "recursion" ]
false
false
false
false
true
let rec nodelist_aa_l (#t: Type) (nl: nodelist t) : GTot Type0 (decreases (length nl)) =
match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns
false
DoublyLinkedList.fst
DoublyLinkedList.loc_equiv_union_union_loc
val loc_equiv_union_union_loc (a b c: Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))]
val loc_equiv_union_union_loc (a b c: Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))]
let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 307, "start_col": 0, "start_line": 289 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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: LowStar.Monotonic.Buffer.loc -> b: LowStar.Monotonic.Buffer.loc -> c: LowStar.Monotonic.Buffer.loc -> FStar.Pervasives.Lemma (requires DoublyLinkedList.loc_equiv b c) (ensures DoublyLinkedList.loc_equiv (LowStar.Monotonic.Buffer.loc_union a b) (LowStar.Monotonic.Buffer.loc_union a c)) [ SMTPat (DoublyLinkedList.loc_equiv (LowStar.Monotonic.Buffer.loc_union a b) (LowStar.Monotonic.Buffer.loc_union a c)) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "LowStar.Monotonic.Buffer.loc_includes_union_l", "Prims.unit", "LowStar.Monotonic.Buffer.loc_union", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.loc_equiv", "Prims.squash", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.logical", "Prims.Nil" ]
[]
true
false
true
false
false
let loc_equiv_union_union_loc (a b c: Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] =
let incl = Mod.loc_includes in let u = Mod.loc_union in Mod.loc_includes_union_l a b c; Mod.loc_includes_union_l a b a; Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a
false
DoublyLinkedList.fst
DoublyLinkedList.unchanged_node_val
val unchanged_node_val : h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> n: b: LowStar.Monotonic.Buffer.mbuffer (DoublyLinkedList.node _) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node _)) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node _)) {LowStar.Monotonic.Buffer.length b == 1 /\ LowStar.Monotonic.Buffer.length b == 1} -> Prims.logical
let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 413, "start_col": 0, "start_line": 411 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> n: b: LowStar.Monotonic.Buffer.mbuffer (DoublyLinkedList.node _) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node _)) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node _)) {LowStar.Monotonic.Buffer.length b == 1 /\ LowStar.Monotonic.Buffer.length b == 1} -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.mbuffer", "DoublyLinkedList.node", "LowStar.Buffer.trivial_preorder", "Prims.l_and", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "Prims.l_imp", "DoublyLinkedList.contains", "DoublyLinkedList.__proj__Mknode__item__p", "DoublyLinkedList.op_At", "Prims.logical" ]
[]
false
false
false
false
true
let unchanged_node_val h0 h1 n =
(h0 `contains` n ==> ((n @ h0).p == (n @ h1).p /\ h1 `contains` n))
false
DoublyLinkedList.fst
DoublyLinkedList.unchanged_node_vals
val unchanged_node_vals (h0 h1: HS.mem) (ns: nodelist 'a) : GTot prop
val unchanged_node_vals (h0 h1: HS.mem) (ns: nodelist 'a) : GTot prop
let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns'
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 75, "end_line": 418, "start_col": 0, "start_line": 415 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> ns: DoublyLinkedList.nodelist 'a -> Prims.GTot Prims.prop
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "DoublyLinkedList.nodelist", "Prims.l_True", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.l_and", "DoublyLinkedList.unchanged_node_val", "DoublyLinkedList.unchanged_node_vals", "Prims.prop" ]
[ "recursion" ]
false
false
false
false
true
let rec unchanged_node_vals (h0 h1: HS.mem) (ns: nodelist 'a) : GTot prop =
match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns'
false
DoublyLinkedList.fst
DoublyLinkedList.extract_nodelist_fp0
val extract_nodelist_fp0 (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[ i ])))
val extract_nodelist_fp0 (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[ i ])))
let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 483, "start_col": 0, "start_line": 476 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> i: Prims.nat{i < FStar.List.Tot.Base.length nl} -> FStar.Pervasives.Lemma (ensures LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 nl) (LowStar.Monotonic.Buffer.loc_buffer nl.[ i ]))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "DoublyLinkedList.extract_nodelist_fp0", "FStar.List.Tot.Base.tl", "Prims.op_Subtraction", "Prims.unit", "Prims.l_True", "Prims.squash", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.op_String_Access", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec extract_nodelist_fp0 (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[ i ]))) =
match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1)
false
DoublyLinkedList.fst
DoublyLinkedList.extract_nodelist_aa_l
val extract_nodelist_aa_l (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl))
val extract_nodelist_aa_l (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl))
let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 509, "start_col": 0, "start_line": 495 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> i: Prims.nat{i < FStar.List.Tot.Base.length nl} -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_l nl) (ensures (let _ = FStar.List.Tot.Base.split3 nl i in (let FStar.Pervasives.Native.Mktuple3 #_ #_ #_ left n _ = _ in LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer n) (DoublyLinkedList.nodelist_fp0 left)) <: Type0)) (decreases FStar.List.Tot.Base.length nl)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "Prims.bool", "Prims.list", "FStar.List.Pure.Properties.lemma_split3_unsnoc", "Prims.unit", "DoublyLinkedList.extract_nodelist_aa_l", "FStar.List.Pure.Properties.lemma_unsnoc_split3", "FStar.Pervasives.Native.tuple3", "FStar.List.Tot.Base.split3", "FStar.List.Tot.Properties.lemma_unsnoc_length", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.unsnoc", "DoublyLinkedList.nodelist_aa_l", "Prims.squash", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec extract_nodelist_aa_l (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) =
if i = length nl - 1 then () else (let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i)
false
DoublyLinkedList.fst
DoublyLinkedList.extract_nodelist_contained
val extract_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[ i ]))
val extract_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[ i ]))
let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 54, "end_line": 474, "start_col": 0, "start_line": 468 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> i: Prims.nat{i < FStar.List.Tot.Base.length nl} -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_contained h0 nl) (ensures DoublyLinkedList.contains h0 nl.[ i ])
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "DoublyLinkedList.extract_nodelist_contained", "FStar.List.Tot.Base.tl", "Prims.op_Subtraction", "Prims.unit", "DoublyLinkedList.nodelist_contained", "Prims.squash", "DoublyLinkedList.contains", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.op_String_Access", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec extract_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[ i ])) =
match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1)
false
DoublyLinkedList.fst
DoublyLinkedList.extract_nodelist_aa_r
val extract_nodelist_aa_r (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right)))
val extract_nodelist_aa_r (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right)))
let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 493, "start_col": 0, "start_line": 485 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> i: Prims.nat{i < FStar.List.Tot.Base.length nl} -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_r nl) (ensures (let _ = FStar.List.Tot.Base.split3 nl i in (let FStar.Pervasives.Native.Mktuple3 #_ #_ #_ _ n right = _ in LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer n) (DoublyLinkedList.nodelist_fp0 right)) <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "DoublyLinkedList.extract_nodelist_aa_r", "FStar.List.Tot.Base.tl", "Prims.op_Subtraction", "Prims.unit", "DoublyLinkedList.nodelist_aa_r", "Prims.squash", "Prims.list", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "FStar.Pervasives.Native.tuple3", "FStar.List.Tot.Base.split3", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec extract_nodelist_aa_r (#t: Type) (nl: nodelist t) (i: nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures (let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) =
match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1)
false
DoublyLinkedList.fst
DoublyLinkedList.node_not_in_dll
val node_not_in_dll : h0: DoublyLinkedList.heap -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> d: DoublyLinkedList.dll t -> Prims.GTot Type0
let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 1367, "start_col": 0, "start_line": 1364 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> d: DoublyLinkedList.dll t -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.heap", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.dll", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.dll_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder" ]
[]
false
false
false
false
true
let node_not_in_dll (#t: Type) (h0: heap) (n: pointer (node t)) (d: dll t) =
let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2
false
DoublyLinkedList.fst
DoublyLinkedList.fst_unsnoc_nodelist_conn
val fst_unsnoc_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl))))
val fst_unsnoc_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl))))
let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 595, "start_col": 0, "start_line": 589 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_conn h0 nl /\ FStar.List.Tot.Base.length nl > 0) (ensures DoublyLinkedList.nodelist_conn h0 (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.fst_unsnoc_nodelist_conn", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_conn", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "FStar.Pervasives.Native.fst", "FStar.List.Tot.Base.unsnoc", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec fst_unsnoc_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) =
match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_aa
val nodelist_append_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2)))
val nodelist_append_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2)))
let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 765, "start_col": 0, "start_line": 760 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa nl1 /\ DoublyLinkedList.nodelist_aa nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2)) (ensures DoublyLinkedList.nodelist_aa (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "DoublyLinkedList.nodelist_append_aa_r", "Prims.unit", "DoublyLinkedList.nodelist_append_aa_l", "Prims.l_and", "DoublyLinkedList.nodelist_aa", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.squash", "FStar.List.Tot.Base.append", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let nodelist_append_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) =
nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2
false
DoublyLinkedList.fst
DoublyLinkedList.tot_piece_to_dll
val tot_piece_to_dll (#t: Type) (h0: heap) (p: piece t {piece_valid h0 p /\ (p.phead @ h0).blink == null /\ (p.ptail @ h0).flink == null} ) : Tot (d: dll t {dll_valid h0 d})
val tot_piece_to_dll (#t: Type) (h0: heap) (p: piece t {piece_valid h0 p /\ (p.phead @ h0).blink == null /\ (p.ptail @ h0).flink == null} ) : Tot (d: dll t {dll_valid h0 d})
let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes }
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 671, "start_col": 0, "start_line": 666 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> p: DoublyLinkedList.piece t { DoublyLinkedList.piece_valid h0 p /\ Mknode?.blink (Mkpiece?.phead p @ h0) == LowStar.Buffer.null /\ Mknode?.flink (Mkpiece?.ptail p @ h0) == LowStar.Buffer.null } -> d: DoublyLinkedList.dll t {DoublyLinkedList.dll_valid h0 d}
Prims.Tot
[ "total" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.piece", "Prims.l_and", "DoublyLinkedList.piece_valid", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.node", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.op_At", "DoublyLinkedList.__proj__Mkpiece__item__phead", "LowStar.Buffer.null", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "DoublyLinkedList.Mkdll", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "DoublyLinkedList.dll", "DoublyLinkedList.dll_valid" ]
[]
false
false
false
false
false
let tot_piece_to_dll (#t: Type) (h0: heap) (p: piece t {piece_valid h0 p /\ (p.phead @ h0).blink == null /\ (p.ptail @ h0).flink == null} ) : Tot (d: dll t {dll_valid h0 d}) =
{ lhead = p.phead; ltail = p.ptail; nodes = p.pnodes }
false
DoublyLinkedList.fst
DoublyLinkedList.extract_nodelist_conn
val extract_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ((nl.[ i ] @ h0 |>> nl.[ i + 1 ]) /\ (nl.[ i ] <<| nl.[ i + 1 ] @ h0))) (decreases (length nl))
val extract_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ((nl.[ i ] @ h0 |>> nl.[ i + 1 ]) /\ (nl.[ i ] <<| nl.[ i + 1 ] @ h0))) (decreases (length nl))
let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 520, "start_col": 0, "start_line": 511 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> i: Prims.nat{i < FStar.List.Tot.Base.length nl - 1} -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_conn h0 nl) (ensures nl.[ i ] @ h0 |>> nl.[ i + 1 ] /\ nl.[ i ] <<| nl.[ i + 1 ] @ h0) (decreases FStar.List.Tot.Base.length nl)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Subtraction", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "DoublyLinkedList.extract_nodelist_conn", "FStar.List.Tot.Base.tl", "Prims.unit", "DoublyLinkedList.nodelist_conn", "Prims.squash", "Prims.l_and", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "DoublyLinkedList.op_String_Access", "Prims.op_Addition", "DoublyLinkedList.op_Less_Less_Bar", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec extract_nodelist_conn (#t: Type) (h0: heap) (nl: nodelist t) (i: nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ((nl.[ i ] @ h0 |>> nl.[ i + 1 ]) /\ (nl.[ i ] <<| nl.[ i + 1 ] @ h0))) (decreases (length nl)) =
match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_contained
val nodelist_append_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2)))
val nodelist_append_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2)))
let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 54, "end_line": 683, "start_col": 0, "start_line": 677 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_contained h0 nl1 /\ DoublyLinkedList.nodelist_contained h0 nl2) (ensures DoublyLinkedList.nodelist_contained h0 (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.nodelist_append_contained", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_contained", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_append_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) =
match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2
false
DoublyLinkedList.fst
DoublyLinkedList.fst_unsnoc_nodelist_valid
val fst_unsnoc_nodelist_valid (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl))))
val fst_unsnoc_nodelist_valid (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl))))
let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 603, "start_col": 0, "start_line": 597 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_valid h0 nl /\ FStar.List.Tot.Base.length nl > 0) (ensures DoublyLinkedList.nodelist_valid h0 (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "DoublyLinkedList.fst_unsnoc_nodelist_conn", "Prims.unit", "DoublyLinkedList.fst_unsnoc_nodelist_aa", "DoublyLinkedList.fst_unsnoc_nodelist_contained", "Prims.l_and", "DoublyLinkedList.nodelist_valid", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.squash", "FStar.Pervasives.Native.fst", "Prims.list", "FStar.List.Tot.Base.unsnoc", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let fst_unsnoc_nodelist_valid (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) =
fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl
false
DoublyLinkedList.fst
DoublyLinkedList.tot_dll_to_fragment
val tot_dll_to_fragment (#t: Type) (h0: heap) (d: dll t {dll_valid h0 d /\ d.lhead =!= null}) : Tot (f: fragment t {fragment_valid h0 f})
val tot_dll_to_fragment (#t: Type) (h0: heap) (d: dll t {dll_valid h0 d /\ d.lhead =!= null}) : Tot (f: fragment t {fragment_valid h0 f})
let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 664, "start_col": 0, "start_line": 662 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> d: DoublyLinkedList.dll t {DoublyLinkedList.dll_valid h0 d /\ ~(Mkdll?.lhead d == LowStar.Buffer.null)} -> f: DoublyLinkedList.fragment t {DoublyLinkedList.fragment_valid h0 f}
Prims.Tot
[ "total" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.dll", "Prims.l_and", "DoublyLinkedList.dll_valid", "Prims.l_not", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.node", "DoublyLinkedList.__proj__Mkdll__item__lhead", "LowStar.Buffer.null", "DoublyLinkedList.Frag1", "DoublyLinkedList.tot_dll_to_piece", "DoublyLinkedList.fragment", "DoublyLinkedList.fragment_valid" ]
[]
false
false
false
false
false
let tot_dll_to_fragment (#t: Type) (h0: heap) (d: dll t {dll_valid h0 d /\ d.lhead =!= null}) : Tot (f: fragment t {fragment_valid h0 f}) =
Frag1 (tot_dll_to_piece h0 d)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_includes_l_fp0
val nodelist_includes_l_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i))
val nodelist_includes_l_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i))
let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 654, "start_col": 0, "start_line": 630 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> i: Prims.nat -> j: Prims.nat -> FStar.Pervasives.Lemma (requires i <= j /\ j < FStar.List.Tot.Base.length nl) (ensures (let _ = FStar.List.Tot.Base.splitAt i nl in (let FStar.Pervasives.Native.Mktuple2 #_ #_ a _ = _ in let _ = FStar.List.Tot.Base.splitAt j nl in (let FStar.Pervasives.Native.Mktuple2 #_ #_ b _ = _ in LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 b) (DoublyLinkedList.nodelist_fp0 a)) <: Type0) <: Type0)) (decreases j - i)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.nat", "Prims.op_Equality", "Prims.bool", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "Prims.op_Subtraction", "DoublyLinkedList.fst_unsnoc_nodelist_fp0", "Prims.unit", "FStar.List.Tot.Properties.lemma_unsnoc_append", "Prims.Cons", "FStar.List.Tot.Base.hd", "Prims.Nil", "FStar.List.Pure.Properties.splitAt_assoc", "LowStar.Monotonic.Buffer.loc_includes_trans", "DoublyLinkedList.nodelist_fp0", "FStar.List.Pure.Properties.lemma_splitAt", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.splitAt", "DoublyLinkedList.nodelist_includes_l_fp0", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "Prims.squash", "LowStar.Monotonic.Buffer.loc_includes", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_includes_l_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) =
if i = j then () else (let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then (List.Pure.Properties.splitAt_assoc i 1 nl; lemma_unsnoc_append a [hd a']; fst_unsnoc_nodelist_fp0 b) else (nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a)))
false
DoublyLinkedList.fst
DoublyLinkedList.snd_unsnoc_nodelist_fp0
val snd_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))]
val snd_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))]
let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 562, "start_col": 0, "start_line": 555 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.length nl > 0) (ensures LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 nl) (LowStar.Monotonic.Buffer.loc_buffer (FStar.Pervasives.Native.snd (FStar.List.Tot.Base.unsnoc nl)))) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 nl) (LowStar.Monotonic.Buffer.loc_buffer (FStar.Pervasives.Native.snd (FStar.List.Tot.Base.unsnoc nl)))) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.snd_unsnoc_nodelist_fp0", "Prims.unit", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.Pervasives.Native.snd", "FStar.List.Tot.Base.unsnoc", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec snd_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] =
match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_contained
val nodelist_split_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2))
val nodelist_split_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2))
let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 53, "end_line": 939, "start_col": 0, "start_line": 933 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_contained h0 (nl1 @ nl2)) (ensures DoublyLinkedList.nodelist_contained h0 nl1 /\ DoublyLinkedList.nodelist_contained h0 nl2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.nodelist_split_contained", "Prims.unit", "DoublyLinkedList.nodelist_contained", "FStar.List.Tot.Base.append", "Prims.squash", "Prims.l_and", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_contained (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) =
match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_fp0
val nodelist_append_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))
val nodelist_append_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))
let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 717, "start_col": 0, "start_line": 685 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (ensures DoublyLinkedList.loc_equiv (DoublyLinkedList.nodelist_fp0 (nl1 @ nl2)) (LowStar.Monotonic.Buffer.loc_union (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.unit", "DoublyLinkedList.loc_equiv_trans", "LowStar.Monotonic.Buffer.loc_union", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.List.Tot.Base.append", "DoublyLinkedList.nodelist_append_fp0", "Prims.l_True", "Prims.squash", "DoublyLinkedList.loc_equiv", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_append_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) =
match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); ()
false
DoublyLinkedList.fst
DoublyLinkedList.loc_includes_union_r_inv
val loc_includes_union_r_inv (a b c: Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c))
val loc_includes_union_r_inv (a b c: Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c))
let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 800, "start_col": 0, "start_line": 793 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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: LowStar.Monotonic.Buffer.loc -> b: LowStar.Monotonic.Buffer.loc -> c: LowStar.Monotonic.Buffer.loc -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.loc_includes a (LowStar.Monotonic.Buffer.loc_union b c)) (ensures LowStar.Monotonic.Buffer.loc_includes a b /\ LowStar.Monotonic.Buffer.loc_includes a c)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "LowStar.Monotonic.Buffer.loc_includes_trans", "LowStar.Monotonic.Buffer.loc_union", "Prims.unit", "LowStar.Monotonic.Buffer.loc_includes_union_l", "LowStar.Monotonic.Buffer.loc_includes", "Prims.squash", "Prims.l_and", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let loc_includes_union_r_inv (a b c: Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) =
Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_aa_r
val nodelist_split_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2))
val nodelist_split_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2))
let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 1032, "start_col": 0, "start_line": 1024 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_r (nl1 @ nl2)) (ensures DoublyLinkedList.nodelist_aa_r nl1 /\ DoublyLinkedList.nodelist_aa_r nl2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.nodelist_append_fp0", "FStar.List.Tot.Base.tl", "Prims.unit", "DoublyLinkedList.nodelist_split_aa_r", "DoublyLinkedList.nodelist_aa_r", "FStar.List.Tot.Base.append", "Prims.squash", "Prims.l_and", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) =
match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2
false
DoublyLinkedList.fst
DoublyLinkedList.tot_dll_to_piece
val tot_dll_to_piece (#t: Type) (h0: heap) (d: nonempty_dll t {dll_valid h0 d}) : Tot (p: piece t {piece_valid h0 p})
val tot_dll_to_piece (#t: Type) (h0: heap) (d: nonempty_dll t {dll_valid h0 d}) : Tot (p: piece t {piece_valid h0 p})
let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes }
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 660, "start_col": 0, "start_line": 658 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> d: DoublyLinkedList.nonempty_dll t {DoublyLinkedList.dll_valid h0 d} -> p: DoublyLinkedList.piece t {DoublyLinkedList.piece_valid h0 p}
Prims.Tot
[ "total" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nonempty_dll", "DoublyLinkedList.dll_valid", "DoublyLinkedList.Mkpiece", "DoublyLinkedList.__proj__Mkdll__item__lhead", "DoublyLinkedList.__proj__Mkdll__item__ltail", "DoublyLinkedList.__proj__Mkdll__item__nodes", "DoublyLinkedList.piece", "DoublyLinkedList.piece_valid" ]
[]
false
false
false
false
false
let tot_dll_to_piece (#t: Type) (h0: heap) (d: nonempty_dll t {dll_valid h0 d}) : Tot (p: piece t {piece_valid h0 p}) =
{ phead = d.lhead; ptail = d.ltail; pnodes = d.nodes }
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_aa_r
val nodelist_append_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2)))
val nodelist_append_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2)))
let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 37, "end_line": 758, "start_col": 0, "start_line": 749 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_r nl1 /\ DoublyLinkedList.nodelist_aa_r nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2)) (ensures DoublyLinkedList.nodelist_aa_r (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.nodelist_append_aa_r", "FStar.List.Tot.Base.tl", "Prims.unit", "DoublyLinkedList.nodelist_append_fp0", "Prims.l_and", "DoublyLinkedList.nodelist_aa_r", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_append_aa_r (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) =
match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2
false
DoublyLinkedList.fst
DoublyLinkedList.fst_unsnoc_nodelist_fp0
val fst_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))]
val fst_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))]
let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 553, "start_col": 0, "start_line": 546 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.length nl > 0) (ensures LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 nl) (DoublyLinkedList.nodelist_fp0 (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl )))) [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 nl) (DoublyLinkedList.nodelist_fp0 (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl)))) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.fst_unsnoc_nodelist_fp0", "Prims.unit", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.nodelist_fp0", "FStar.Pervasives.Native.fst", "FStar.List.Tot.Base.unsnoc", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec fst_unsnoc_nodelist_fp0 (#t: Type) (nl: nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] =
match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns
false
DoublyLinkedList.fst
DoublyLinkedList.fst_unsnoc_nodelist_contained
val fst_unsnoc_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl))))
val fst_unsnoc_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl))))
let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 570, "start_col": 0, "start_line": 564 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_contained h0 nl /\ FStar.List.Tot.Base.length nl > 0) (ensures DoublyLinkedList.nodelist_contained h0 (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.fst_unsnoc_nodelist_contained", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_contained", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "FStar.Pervasives.Native.fst", "FStar.List.Tot.Base.unsnoc", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec fst_unsnoc_nodelist_contained (#t: Type) (h0: heap) (nl: nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) =
match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_includes_r_fp0
val nodelist_includes_r_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i))
val nodelist_includes_r_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i))
let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 628, "start_col": 0, "start_line": 607 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> i: Prims.nat -> j: Prims.nat -> FStar.Pervasives.Lemma (requires i <= j /\ j < FStar.List.Tot.Base.length nl) (ensures (let _ = FStar.List.Tot.Base.splitAt i nl in (let FStar.Pervasives.Native.Mktuple2 #_ #_ _ a = _ in let _ = FStar.List.Tot.Base.splitAt j nl in (let FStar.Pervasives.Native.Mktuple2 #_ #_ _ b = _ in LowStar.Monotonic.Buffer.loc_includes (DoublyLinkedList.nodelist_fp0 a) (DoublyLinkedList.nodelist_fp0 b)) <: Type0) <: Type0)) (decreases j - i)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "Prims.nat", "Prims.op_Equality", "Prims.bool", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.int", "Prims.op_Subtraction", "Prims.unit", "FStar.List.Pure.Properties.splitAt_assoc", "LowStar.Monotonic.Buffer.loc_includes_trans", "DoublyLinkedList.nodelist_fp0", "FStar.List.Pure.Properties.lemma_splitAt", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.splitAt", "DoublyLinkedList.nodelist_includes_r_fp0", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "Prims.squash", "LowStar.Monotonic.Buffer.loc_includes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_includes_r_fp0 (#t: Type) (nl: nodelist t) (i j: nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures (let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) =
if i = j then () else (let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then (List.Pure.Properties.splitAt_assoc i 1 nl; ()) else (nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b)))
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_valid
val nodelist_append_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_valid h0 (append nl1 nl2)))
val nodelist_append_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_valid h0 (append nl1 nl2)))
let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 789, "start_col": 0, "start_line": 779 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_valid h0 nl1 /\ DoublyLinkedList.nodelist_valid h0 nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2) /\ FStar.List.Tot.Base.length nl1 > 0 /\ FStar.List.Tot.Base.length nl2 > 0 /\ FStar.List.Tot.Base.last nl1 @ h0 |>> FStar.List.Tot.Base.hd nl2 /\ FStar.List.Tot.Base.last nl1 <<| FStar.List.Tot.Base.hd nl2 @ h0) (ensures DoublyLinkedList.nodelist_valid h0 (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "DoublyLinkedList.nodelist_append_conn", "Prims.unit", "DoublyLinkedList.nodelist_append_aa", "DoublyLinkedList.nodelist_append_contained", "Prims.l_and", "DoublyLinkedList.nodelist_valid", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "FStar.List.Tot.Base.last", "FStar.List.Tot.Base.hd", "DoublyLinkedList.op_Less_Less_Bar", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let nodelist_append_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) =
nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2
false
DoublyLinkedList.fst
DoublyLinkedList.fragment_defragmentable
val fragment_defragmentable (#t: Type) (h0: heap) (f: fragment t {fragment_valid h0 f}) : GTot Type0
val fragment_defragmentable (#t: Type) (h0: heap) (f: fragment t {fragment_valid h0 f}) : GTot Type0
let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 888, "start_col": 0, "start_line": 879 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> f: DoublyLinkedList.fragment t {DoublyLinkedList.fragment_valid h0 f} -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.fragment", "DoublyLinkedList.fragment_valid", "Prims.l_True", "DoublyLinkedList.piece", "Prims.l_and", "DoublyLinkedList.piece_valid", "Prims.logical", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "DoublyLinkedList.op_Less_Less_Bar", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.List.Tot.Base.last", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Base.hd" ]
[ "recursion" ]
false
false
false
false
true
let rec fragment_defragmentable (#t: Type) (h0: heap) (f: fragment t {fragment_valid h0 f}) : GTot Type0 =
let aux (p1 p2: (p: piece t {piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a @ h0 |>> b) /\ (a <<| b @ h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_aa
val nodelist_split_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2)))
val nodelist_split_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2)))
let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 1041, "start_col": 0, "start_line": 1034 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa (nl1 @ nl2)) (ensures DoublyLinkedList.nodelist_aa nl1 /\ DoublyLinkedList.nodelist_aa nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "DoublyLinkedList.nodelist_split_aa_r", "Prims.unit", "DoublyLinkedList.nodelist_split_aa_l", "DoublyLinkedList.nodelist_split_fp0", "DoublyLinkedList.nodelist_aa", "FStar.List.Tot.Base.append", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.squash", "Prims.l_and", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let nodelist_split_aa (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) =
nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2
false
DoublyLinkedList.fst
DoublyLinkedList.singleton_dll
val singleton_dll (#t: Type) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n]))
val singleton_dll (#t: Type) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n]))
let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 1176, "start_col": 0, "start_line": 1165 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> FStar.HyperStack.ST.StackInline (DoublyLinkedList.dll t)
FStar.HyperStack.ST.StackInline
[]
[]
[ "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.tot_node_to_dll", "DoublyLinkedList.dll", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Prims.unit", "DoublyLinkedList.op_Bang_Less_Bar_Equals", "DoublyLinkedList.op_Bang_Equals_Bar_Greater", "DoublyLinkedList.contains", "LowStar.Buffer.trivial_preorder", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "DoublyLinkedList.dll_valid", "DoublyLinkedList.unchanged_node_vals", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.eq2", "Prims.list", "Prims.Cons", "Prims.Nil" ]
[]
false
true
false
false
false
let singleton_dll (#t: Type) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) =
!=|>n; !<|=n; tot_node_to_dll (ST.get ()) n
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_fp0_equiv
val nodelist_split_fp0_equiv (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))))
val nodelist_split_fp0_equiv (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))))
let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2)))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 37, "end_line": 998, "start_col": 0, "start_line": 970 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 30, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (ensures DoublyLinkedList.loc_equiv (DoublyLinkedList.nodelist_fp0 (nl1 @ nl2)) (LowStar.Monotonic.Buffer.loc_union (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims._assert", "DoublyLinkedList.loc_equiv", "LowStar.Monotonic.Buffer.loc_union", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "Prims.unit", "FStar.List.Tot.Base.append", "DoublyLinkedList.nodelist_split_fp0_equiv", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_fp0_equiv (#t: Type) (nl1 nl2: nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) =
match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2)))
false
DoublyLinkedList.fst
DoublyLinkedList.fst_unsnoc_nodelist_aa
val fst_unsnoc_nodelist_aa (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl))))
val fst_unsnoc_nodelist_aa (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl))))
let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 587, "start_col": 0, "start_line": 572 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa nl /\ FStar.List.Tot.Base.length nl > 0) (ensures DoublyLinkedList.nodelist_aa (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc nl)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.unit", "LowStar.Monotonic.Buffer.loc_disjoint_includes", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_fp0", "FStar.List.Tot.Base.tl", "FStar.Pervasives.Native.fst", "FStar.List.Tot.Base.unsnoc", "DoublyLinkedList.fst_unsnoc_nodelist_aa", "Prims.l_and", "DoublyLinkedList.nodelist_aa", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec fst_unsnoc_nodelist_aa (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) =
match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); ()
false
DoublyLinkedList.fst
DoublyLinkedList.dll_fp0_is_nodelist_fp0
val dll_fp0_is_nodelist_fp0 (#t: Type) (d: dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes)))
val dll_fp0_is_nodelist_fp0 (#t: Type) (d: dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes)))
let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 1077, "start_col": 0, "start_line": 1070 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
d: DoublyLinkedList.dll t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.dll_ghostly_connections d) (ensures DoublyLinkedList.loc_equiv (DoublyLinkedList.dll_fp0 d) (DoublyLinkedList.nodelist_fp0 (FStar.Ghost.reveal (Mkdll?.nodes d))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.dll", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "Prims.bool", "Prims.unit", "DoublyLinkedList.dll_ghostly_connections", "Prims.squash", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "DoublyLinkedList.nodelist_fp0", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let dll_fp0_is_nodelist_fp0 (#t: Type) (d: dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) =
if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_aa_l
val nodelist_split_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2))
val nodelist_split_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2))
let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 1020, "start_col": 0, "start_line": 1000 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 30, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_l (nl1 @ nl2)) (ensures DoublyLinkedList.nodelist_aa_l nl1 /\ DoublyLinkedList.nodelist_aa_l nl2) (decreases FStar.List.Tot.Base.length nl2)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "FStar.List.Tot.Properties.append_l_nil", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.unit", "DoublyLinkedList.nodelist_append_fp0", "DoublyLinkedList.nodelist_split_aa_l", "FStar.List.Tot.Properties.lemma_unsnoc_append", "FStar.List.Tot.Properties.lemma_unsnoc_length", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.unsnoc", "DoublyLinkedList.nodelist_aa_l", "FStar.List.Tot.Base.append", "Prims.squash", "Prims.l_and", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) =
match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; nodelist_split_aa_l nl1 nl2'; nodelist_append_fp0 nl1 nl2'; ()
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_fp0
val nodelist_split_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2)))
val nodelist_split_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2)))
let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 966, "start_col": 0, "start_line": 941 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_r (nl1 @ nl2)) (ensures LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.unit", "LowStar.Monotonic.Buffer.loc_disjoint_union_r", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.List.Tot.Base.hd", "FStar.List.Tot.Base.tl", "FStar.List.Pure.Properties.lemma_append_splitAt", "DoublyLinkedList.nodelist_includes_r_fp0", "FStar.List.Tot.Base.append", "Prims.op_Subtraction", "FStar.List.Tot.Base.length", "FStar.List.Tot.Properties.append_length", "DoublyLinkedList.nodelist_split_fp0", "DoublyLinkedList.nodelist_aa_r", "Prims.squash", "LowStar.Monotonic.Buffer.loc_disjoint", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_fp0 (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) =
match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); lemma_append_splitAt nl1 nl2; Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); ()
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_remains_aa_l
val nodelist_remains_aa_l (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))]
val nodelist_remains_aa_l (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))]
let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 539, "start_col": 0, "start_line": 524 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_l nl /\ FStar.List.Tot.Base.length nl > 0) (ensures DoublyLinkedList.nodelist_aa_l (FStar.List.Tot.Base.tl nl)) (decreases FStar.List.Tot.Base.length nl) [SMTPat (DoublyLinkedList.nodelist_aa_l (FStar.List.Tot.Base.tl nl))]
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.nodelist_remains_aa_l", "Prims.unit", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.unsnoc", "FStar.List.Tot.Base.tl", "FStar.List.Tot.Properties.lemma_unsnoc_length", "Prims.l_and", "DoublyLinkedList.nodelist_aa_l", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_remains_aa_l (#t: Type) (nl: nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] =
match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in nodelist_remains_aa_l ns
false
DoublyLinkedList.fst
DoublyLinkedList.piece_fp0_is_nodelist_fp0
val piece_fp0_is_nodelist_fp0 (#t: Type) (p: piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes)))
val piece_fp0_is_nodelist_fp0 (#t: Type) (p: piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes)))
let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 40, "end_line": 1083, "start_col": 0, "start_line": 1079 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: DoublyLinkedList.piece t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.piece_ghostly_connections p) (ensures DoublyLinkedList.loc_equiv (DoublyLinkedList.piece_fp0 p) (DoublyLinkedList.nodelist_fp0 (FStar.Ghost.reveal (Mkpiece?.pnodes p))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.piece", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "Prims.unit", "DoublyLinkedList.piece_ghostly_connections", "Prims.squash", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.piece_fp0", "DoublyLinkedList.nodelist_fp0", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let piece_fp0_is_nodelist_fp0 (#t: Type) (p: piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) =
lemma_unsnoc_is_last (reveal p.pnodes)
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_valid
val nodelist_split_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0))
val nodelist_split_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0))
let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 1065, "start_col": 0, "start_line": 1055 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_valid h0 (nl1 @ nl2) /\ FStar.List.Tot.Base.length nl1 > 0 /\ FStar.List.Tot.Base.length nl2 > 0) (ensures DoublyLinkedList.nodelist_valid h0 nl1 /\ DoublyLinkedList.nodelist_valid h0 nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2) /\ FStar.List.Tot.Base.last nl1 @ h0 |>> FStar.List.Tot.Base.hd nl2 /\ FStar.List.Tot.Base.last nl1 <<| FStar.List.Tot.Base.hd nl2 @ h0)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "DoublyLinkedList.nodelist_split_conn", "Prims.unit", "DoublyLinkedList.nodelist_split_aa", "DoublyLinkedList.nodelist_split_contained", "Prims.l_and", "DoublyLinkedList.nodelist_valid", "FStar.List.Tot.Base.append", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "FStar.List.Tot.Base.last", "FStar.List.Tot.Base.hd", "DoublyLinkedList.op_Less_Less_Bar", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let nodelist_split_valid (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) =
nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2
false
DoublyLinkedList.fst
DoublyLinkedList.tot_node_to_dll
val tot_node_to_dll (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (dll t) (requires ((h0 `contains` n) /\ (((n @ h0).flink == null)) /\ ((n @ h0).blink == null))) (ensures (fun d -> dll_valid h0 d))
val tot_node_to_dll (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (dll t) (requires ((h0 `contains` n) /\ (((n @ h0).flink == null)) /\ ((n @ h0).blink == null))) (ensures (fun d -> dll_valid h0 d))
let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n }
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 1163, "start_col": 0, "start_line": 1156 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> Prims.Pure (DoublyLinkedList.dll t)
Prims.Pure
[]
[]
[ "DoublyLinkedList.heap", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.Mkdll", "DoublyLinkedList.op_Tilde_Dot", "DoublyLinkedList.dll", "Prims.l_and", "DoublyLinkedList.contains", "LowStar.Buffer.trivial_preorder", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "LowStar.Buffer.null", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.dll_valid" ]
[]
false
false
false
false
false
let tot_node_to_dll (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (dll t) (requires ((h0 `contains` n) /\ (((n @ h0).flink == null)) /\ ((n @ h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) =
{ lhead = n; ltail = n; nodes = ~.n }
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_aa_l
val nodelist_append_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2))
val nodelist_append_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2))
let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 745, "start_col": 0, "start_line": 721 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_aa_l nl1 /\ DoublyLinkedList.nodelist_aa_l nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2)) (ensures DoublyLinkedList.nodelist_aa_l (nl1 @ nl2)) (decreases FStar.List.Tot.Base.length nl2)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "DoublyLinkedList.nodelist", "FStar.List.Tot.Properties.append_l_nil", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.unit", "FStar.List.Tot.Properties.lemma_unsnoc_append", "DoublyLinkedList.nodelist_append_aa_l", "Prims._assert", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "DoublyLinkedList.nodelist_append_fp0", "FStar.List.Tot.Properties.lemma_unsnoc_length", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.unsnoc", "Prims.l_and", "DoublyLinkedList.nodelist_aa_l", "LowStar.Monotonic.Buffer.loc_disjoint", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_append_aa_l (#t: Type) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) =
match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); nodelist_append_aa_l nl1 nl2'; lemma_unsnoc_append nl1 nl2; ()
false
DoublyLinkedList.fst
DoublyLinkedList._aux_fp_split_by_node
val _aux_fp_split_by_node : d0: DoublyLinkedList.dll 'a -> d1: DoublyLinkedList.dll 'a -> n: LowStar.Buffer.pointer (DoublyLinkedList.node 'a) -> Prims.logical
let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 1720, "start_col": 0, "start_line": 1718 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
d0: DoublyLinkedList.dll 'a -> d1: DoublyLinkedList.dll 'a -> n: LowStar.Buffer.pointer (DoublyLinkedList.node 'a) -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "DoublyLinkedList.dll", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.l_and", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "LowStar.Monotonic.Buffer.loc_union", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.loc_disjoint", "Prims.logical" ]
[]
false
false
false
true
true
let _aux_fp_split_by_node (d0 d1: dll 'a) (n: pointer (node 'a)) =
(dll_fp0 d0) `loc_equiv` (B.loc_union (dll_fp0 d1) (Mod.loc_buffer n)) /\ (dll_fp0 d1) `B.loc_disjoint` (Mod.loc_buffer n)
false
DoublyLinkedList.fst
DoublyLinkedList.tot_node_to_piece
val tot_node_to_piece (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (piece t) (requires ((h0 `contains` n))) (ensures (fun p -> piece_valid h0 p))
val tot_node_to_piece (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (piece t) (requires ((h0 `contains` n))) (ensures (fun p -> piece_valid h0 p))
let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n }
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 1185, "start_col": 0, "start_line": 1180 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> Prims.Pure (DoublyLinkedList.piece t)
Prims.Pure
[]
[]
[ "DoublyLinkedList.heap", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.Mkpiece", "DoublyLinkedList.op_Tilde_Dot", "DoublyLinkedList.piece", "DoublyLinkedList.contains", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.piece_valid" ]
[]
false
false
false
false
false
let tot_node_to_piece (#t: Type) (h0: heap) (n: pointer (node t)) : Pure (piece t) (requires ((h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) =
{ phead = n; ptail = n; pnodes = ~.n }
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_append_conn
val nodelist_append_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_conn h0 (append nl1 nl2)))
val nodelist_append_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_conn h0 (append nl1 nl2)))
let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 777, "start_col": 0, "start_line": 767 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_conn h0 nl1 /\ DoublyLinkedList.nodelist_conn h0 nl2 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.nodelist_fp0 nl1) (DoublyLinkedList.nodelist_fp0 nl2) /\ FStar.List.Tot.Base.length nl1 > 0 /\ FStar.List.Tot.Base.length nl2 > 0 /\ FStar.List.Tot.Base.last nl1 @ h0 |>> FStar.List.Tot.Base.hd nl2 /\ FStar.List.Tot.Base.last nl1 <<| FStar.List.Tot.Base.hd nl2 @ h0) (ensures DoublyLinkedList.nodelist_conn h0 (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.nodelist_append_conn", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_conn", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "FStar.List.Tot.Base.last", "FStar.List.Tot.Base.hd", "DoublyLinkedList.op_Less_Less_Bar", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_append_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) =
match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_split_conn
val nodelist_split_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires ((nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0))
val nodelist_split_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires ((nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0))
let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 1053, "start_col": 0, "start_line": 1043 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_conn h0 (nl1 @ nl2) /\ FStar.List.Tot.Base.length nl1 > 0 /\ FStar.List.Tot.Base.length nl2 > 0) (ensures DoublyLinkedList.nodelist_conn h0 nl1 /\ DoublyLinkedList.nodelist_conn h0 nl2 /\ FStar.List.Tot.Base.last nl1 @ h0 |>> FStar.List.Tot.Base.hd nl2 /\ FStar.List.Tot.Base.last nl1 <<| FStar.List.Tot.Base.hd nl2 @ h0)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.nodelist_split_conn", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_conn", "FStar.List.Tot.Base.append", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "Prims.squash", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "FStar.List.Tot.Base.last", "FStar.List.Tot.Base.hd", "DoublyLinkedList.op_Less_Less_Bar", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_split_conn (#t: Type) (h0: heap) (nl1 nl2: nodelist t) : Lemma (requires ((nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1) @ h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2) @ h0)) =
match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2
false
DoublyLinkedList.fst
DoublyLinkedList.nodelist_remains_valid
val nodelist_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (nl: nodelist t) : Lemma (requires ((nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl))
val nodelist_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (nl: nodelist t) : Lemma (requires ((nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl))
let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 1278, "start_col": 0, "start_line": 1269 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> h1: DoublyLinkedList.heap -> loc: LowStar.Monotonic.Buffer.loc -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_valid h0 nl /\ LowStar.Monotonic.Buffer.modifies loc h0 h1 /\ LowStar.Monotonic.Buffer.loc_disjoint loc (DoublyLinkedList.nodelist_fp0 nl)) (ensures DoublyLinkedList.nodelist_valid h1 nl)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.nodelist", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.nodelist_remains_valid", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_valid", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.nodelist_fp0", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec nodelist_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (nl: nodelist t) : Lemma (requires ((nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) =
match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl)
false
DoublyLinkedList.fst
DoublyLinkedList.piece_remains_valid
val piece_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p))
val piece_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p))
let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 1287, "start_col": 0, "start_line": 1280 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> h1: DoublyLinkedList.heap -> loc: LowStar.Monotonic.Buffer.loc -> p: DoublyLinkedList.piece t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.piece_valid h0 p /\ LowStar.Monotonic.Buffer.modifies loc h0 h1 /\ LowStar.Monotonic.Buffer.loc_disjoint loc (DoublyLinkedList.piece_fp0 p)) (ensures DoublyLinkedList.piece_valid h1 p)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "LowStar.Monotonic.Buffer.loc", "DoublyLinkedList.piece", "DoublyLinkedList.nodelist_remains_valid", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "Prims.unit", "Prims.l_and", "DoublyLinkedList.piece_valid", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_disjoint", "DoublyLinkedList.piece_fp0", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let piece_remains_valid (#t: Type) (h0 h1: heap) (loc: Mod.loc) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) =
nodelist_remains_valid h0 h1 loc p.pnodes
false
DoublyLinkedList.fst
DoublyLinkedList.aux_unchanged_payload_nomod
val aux_unchanged_payload_nomod (#t h0 h1: _) (nl: nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl))
val aux_unchanged_payload_nomod (#t h0 h1: _) (nl: nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl))
let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl'
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 1411, "start_col": 0, "start_line": 1404 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ())
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.modifies LowStar.Monotonic.Buffer.loc_none h0 h1) (ensures DoublyLinkedList.unchanged_node_vals h0 h1 nl)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.aux_unchanged_payload_nomod", "Prims.unit", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_none", "Prims.squash", "DoublyLinkedList.unchanged_node_vals", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec aux_unchanged_payload_nomod #t h0 h1 (nl: nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) =
match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl'
false
DoublyLinkedList.fst
DoublyLinkedList.tot_piece_tail
val tot_piece_tail (#t: Type) (h0: heap) (p: piece t) (n: pointer (node t)) : Pure (piece t) (requires ((piece_valid h0 p) /\ (n == (((p.phead) @ h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes))
val tot_piece_tail (#t: Type) (h0: heap) (p: piece t) (n: pointer (node t)) : Pure (piece t) (requires ((piece_valid h0 p) /\ (n == (((p.phead) @ h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes))
let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes }
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 79, "end_line": 1198, "start_col": 0, "start_line": 1189 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> p: DoublyLinkedList.piece t -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> Prims.Pure (DoublyLinkedList.piece t)
Prims.Pure
[]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.piece", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.Mkpiece", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "FStar.Ghost.elift1_p", "Prims.list", "Prims.b2t", "Prims.uu___is_Cons", "FStar.Ghost.tot_to_gtot", "FStar.List.Tot.Base.tl", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "Prims.l_and", "DoublyLinkedList.piece_valid", "Prims.eq2", "LowStar.Buffer.buffer", "Prims.l_or", "Prims.int", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.g_is_null", "Prims.l_True", "Prims.bool", "Prims.logical", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "DoublyLinkedList.__proj__Mkpiece__item__phead", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist" ]
[]
false
false
false
false
false
let tot_piece_tail (#t: Type) (h0: heap) (p: piece t) (n: pointer (node t)) : Pure (piece t) (requires ((piece_valid h0 p) /\ (n == (((p.phead) @ h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) =
{ phead = n; ptail = p.ptail; pnodes = elift1_p (tot_to_gtot tl) p.pnodes }
false
DoublyLinkedList.fst
DoublyLinkedList.aux_unchanged_payload_transitive
val aux_unchanged_payload_transitive (#t h0 h1 h2: _) (nl: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl))
val aux_unchanged_payload_transitive (#t h0 h1 h2: _) (nl: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl))
let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl'
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 1420, "start_col": 0, "start_line": 1413 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.unchanged_node_vals h0 h1 nl /\ DoublyLinkedList.unchanged_node_vals h1 h2 nl) (ensures DoublyLinkedList.unchanged_node_vals h0 h2 nl)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.aux_unchanged_payload_transitive", "Prims.unit", "Prims.l_and", "DoublyLinkedList.unchanged_node_vals", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) =
match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl'
false
DoublyLinkedList.fst
DoublyLinkedList.aux_unchanged_payload_append
val aux_unchanged_payload_append (#t h0 h1: _) (nl1 nl2: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2)))
val aux_unchanged_payload_append (#t h0 h1: _) (nl1 nl2: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2)))
let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 1429, "start_col": 0, "start_line": 1422 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> nl1: DoublyLinkedList.nodelist t -> nl2: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.unchanged_node_vals h0 h1 nl1 /\ DoublyLinkedList.unchanged_node_vals h0 h1 nl2) (ensures DoublyLinkedList.unchanged_node_vals h0 h1 (nl1 @ nl2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "DoublyLinkedList.aux_unchanged_payload_append", "Prims.unit", "Prims.l_and", "DoublyLinkedList.unchanged_node_vals", "Prims.squash", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec aux_unchanged_payload_append #t h0 h1 (nl1: nodelist t) (nl2: nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) =
match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2
false
DoublyLinkedList.fst
DoublyLinkedList.piece_remains_valid_b
val piece_remains_valid_b (#t: Type) (h0 h1: heap) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead @ h0).flink == (p.phead @ h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail @ h0).flink == (p.ptail @ h1).flink)
val piece_remains_valid_b (#t: Type) (h0 h1: heap) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead @ h0).flink == (p.phead @ h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail @ h0).flink == (p.ptail @ h1).flink)
let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else ()
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 1311, "start_col": 0, "start_line": 1294 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: DoublyLinkedList.heap -> h1: DoublyLinkedList.heap -> p: DoublyLinkedList.piece t -> FStar.Pervasives.Lemma (requires DoublyLinkedList.piece_valid h0 p /\ LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_buffer (Mkpiece?.phead p)) h0 h1 /\ DoublyLinkedList.contains h1 (Mkpiece?.phead p) /\ Mknode?.flink (Mkpiece?.phead p @ h0) == Mknode?.flink (Mkpiece?.phead p @ h1)) (ensures DoublyLinkedList.piece_valid h1 p /\ Mknode?.flink (Mkpiece?.ptail p @ h0) == Mknode?.flink (Mkpiece?.ptail p @ h1))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.piece", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.nodelist_remains_valid", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.__proj__Mkpiece__item__phead", "FStar.List.Tot.Base.tl", "Prims.unit", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "DoublyLinkedList.nodelist_includes_r_fp0", "Prims.op_Subtraction", "Prims.bool", "FStar.Ghost.erased", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "Prims.l_and", "DoublyLinkedList.piece_valid", "LowStar.Monotonic.Buffer.modifies", "DoublyLinkedList.contains", "Prims.eq2", "LowStar.Buffer.pointer_or_null", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "Prims.squash", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let piece_remains_valid_b (#t: Type) (h0 h1: heap) (p: piece t) : Lemma (requires ((piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead @ h0).flink == (p.phead @ h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail @ h0).flink == (p.ptail @ h1).flink) =
let nodes = p.pnodes in if length nodes > 1 then (nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes))
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.a_spec
val a_spec : a: Spec.Hash.Definitions.fixed_len_alg -> i: Prims.nat -> Type0
let a_spec (a:fixed_len_alg) (i:nat) = Lib.Sequence.lseq uint8 (hash_length a)
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 78, "end_line": 66, "start_col": 0, "start_line": 66 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1 let reseed #a st entropy_input additional_input = let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
a: Spec.Hash.Definitions.fixed_len_alg -> i: Prims.nat -> Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.fixed_len_alg", "Prims.nat", "Lib.Sequence.lseq", "Lib.IntTypes.uint8", "Spec.Hash.Definitions.hash_length" ]
[]
false
false
false
true
true
let a_spec (a: fixed_len_alg) (i: nat) =
Lib.Sequence.lseq uint8 (hash_length a)
false
DoublyLinkedList.fst
DoublyLinkedList.piece_merge
val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes)))
val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes)))
let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 821, "start_col": 0, "start_line": 817 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: DoublyLinkedList.heap -> p1: DoublyLinkedList.piece t {DoublyLinkedList.piece_valid h0 p1} -> p2: DoublyLinkedList.piece t {DoublyLinkedList.piece_valid h0 p2} -> Prims.Pure (DoublyLinkedList.piece t)
Prims.Pure
[]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.piece", "DoublyLinkedList.piece_valid", "Prims.unit", "DoublyLinkedList.nodelist_append_valid", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Properties.lemma_append_last", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.Mkpiece", "DoublyLinkedList.__proj__Mkpiece__item__phead", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "DoublyLinkedList.op_Hat_At_Hat" ]
[]
false
false
false
false
false
let piece_merge #t h0 p1 p2 =
let p = { phead = p1.phead; ptail = p2.ptail; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.exists_elim
val exists_elim (#t:Type) (#p:t -> Type) (#q:Type) ($s_ex_p: squash (exists (x:t). p x)) (f: (x:t -> squash (p x) -> Tot (squash q))) : Tot (squash q)
val exists_elim (#t:Type) (#p:t -> Type) (#q:Type) ($s_ex_p: squash (exists (x:t). p x)) (f: (x:t -> squash (p x) -> Tot (squash q))) : Tot (squash q)
let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px)))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 34, "start_col": 0, "start_line": 29 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
$s_ex_p: Prims.squash (exists (x: t). p x) -> f: (x: t -> _: Prims.squash (p x) -> Prims.squash q) -> Prims.squash q
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_Exists", "FStar.Squash.bind_squash", "Prims.dtuple2", "FStar.Squash.return_squash" ]
[]
false
false
true
false
false
let exists_elim #t #p #q s_ex_p f =
let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x: t & p x)) -> let (| x , px |) = sig_p in f x (return_squash px)))
false
DoublyLinkedList.fst
DoublyLinkedList._l_insert_after
val _l_insert_after (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a)
val _l_insert_after (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a)
let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 1548, "start_col": 0, "start_line": 1545 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
x0: 'a -> l: Prims.list 'a {FStar.List.Tot.Base.memP x0 l} -> x: 'a -> Prims.GTot (Prims.list 'a)
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.list", "FStar.List.Tot.Base.memP", "FStar.List.Tot.Base.append", "Prims.Cons", "Prims.unit", "Prims._assert", "Prims.eq2", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Properties.split_using", "FStar.List.Tot.Properties.lemma_split_using" ]
[]
false
false
false
false
false
let _l_insert_after (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a) =
let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.forall_intro
val forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x))
val forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x))
let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f'))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 89, "start_col": 0, "start_line": 79 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> p: (_: a -> Type0) -> f: (x: a -> Prims.squash (p x)) -> Prims.squash (forall (x: a). p x)
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "FStar.Squash.return_squash", "Prims.l_Forall", "FStar.Squash.squash_double_arrow" ]
[]
false
false
true
false
false
let forall_intro (a: Type) (p: (a -> Type)) (f: (x: a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) =
let open FStar.Squash in let f' (x: a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f'))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.exists_intro
val exists_intro (a:Type) (p:a -> Type) (v:a) (x: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x))
val exists_intro (a:Type) (p:a -> Type) (v:a) (x: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x))
let exists_intro (a:Type) (p:a -> Type) (v:a) (f: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x)) = exists_intro_simple a p v (f())
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 35, "end_line": 107, "start_col": 0, "start_line": 101 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f')) let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> p: (_: a -> Type0) -> v: a -> x: (_: Prims.unit -> Prims.squash (p v)) -> Prims.squash (exists (x: a). p x)
Prims.Tot
[ "total" ]
[]
[ "Prims.unit", "Prims.squash", "FStar.Classical.Sugar.exists_intro_simple", "Prims.l_Exists" ]
[]
false
false
true
false
false
let exists_intro (a: Type) (p: (a -> Type)) (v: a) (f: (unit -> Tot (squash (p v)))) : Tot (squash (exists x. p x)) =
exists_intro_simple a p v (f ())
false
DoublyLinkedList.fst
DoublyLinkedList.piece_merge_fp0
val piece_merge_fp0 (#t: Type) (h0: heap) (p1: piece t {piece_valid h0 p1}) (p2: piece t {piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a @ h0 |>> b) /\ (a <<| b @ h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))) )
val piece_merge_fp0 (#t: Type) (h0: heap) (p1: piece t {piece_valid h0 p1}) (p2: piece t {piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a @ h0 |>> b) /\ (a <<| b @ h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))) )
let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 875, "start_col": 0, "start_line": 825 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> p1: DoublyLinkedList.piece t {DoublyLinkedList.piece_valid h0 p1} -> p2: DoublyLinkedList.piece t {DoublyLinkedList.piece_valid h0 p2} -> FStar.Pervasives.Lemma (requires (let _ = FStar.List.Tot.Base.last (FStar.Ghost.reveal (Mkpiece?.pnodes p1)), FStar.List.Tot.Base.hd (FStar.Ghost.reveal (Mkpiece?.pnodes p2)) in (let FStar.Pervasives.Native.Mktuple2 #_ #_ a b = _ in a @ h0 |>> b /\ a <<| b @ h0 /\ LowStar.Monotonic.Buffer.loc_disjoint (DoublyLinkedList.piece_fp0 p1) (DoublyLinkedList.piece_fp0 p2)) <: Type0)) (ensures DoublyLinkedList.loc_equiv (DoublyLinkedList.piece_fp0 (DoublyLinkedList.piece_merge h0 p1 p2)) (LowStar.Monotonic.Buffer.loc_union (DoublyLinkedList.piece_fp0 p1) (DoublyLinkedList.piece_fp0 p2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.piece", "DoublyLinkedList.piece_valid", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.loc_equiv_trans", "DoublyLinkedList.piece_fp0", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_union", "Prims.unit", "LowStar.Monotonic.Buffer.loc_includes_trans", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "DoublyLinkedList.extract_nodelist_fp0", "Prims.op_Subtraction", "FStar.List.Tot.Base.length", "DoublyLinkedList.loc_includes_union_r_inv", "DoublyLinkedList.nodelist_append_fp0", "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.Native.Mktuple3", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "DoublyLinkedList.piece_merge", "Prims.l_and", "DoublyLinkedList.op_Bar_Greater_Greater", "DoublyLinkedList.op_At", "DoublyLinkedList.op_Less_Less_Bar", "LowStar.Monotonic.Buffer.loc_disjoint", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.List.Tot.Base.last", "FStar.List.Tot.Base.hd", "Prims.squash", "DoublyLinkedList.loc_equiv", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let piece_merge_fp0 (#t: Type) (h0: heap) (p1: piece t {piece_valid h0 p1}) (p2: piece t {piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a @ h0 |>> b) /\ (a <<| b @ h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))) ) =
let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.or_elim_simple
val or_elim_simple (p q r: Type) (x: squash (p \/ q)) (f: (squash p -> Tot (squash r))) (g: (squash q -> Tot (squash r))) : Tot (squash r)
val or_elim_simple (p q r: Type) (x: squash (p \/ q)) (f: (squash p -> Tot (squash r))) (g: (squash q -> Tot (squash r))) : Tot (squash r)
let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q)))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 51, "start_col": 0, "start_line": 36 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: Type0 -> r: Type -> x: Prims.squash (p \/ q) -> f: (_: Prims.squash p -> Prims.squash r) -> g: (_: Prims.squash q -> Prims.squash r) -> Prims.squash r
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_or", "FStar.Squash.bind_squash", "Prims.sum", "FStar.Squash.return_squash" ]
[]
false
false
true
true
false
let or_elim_simple (p q r: Type) (x: squash (p \/ q)) (f: (squash p -> Tot (squash r))) (g: (squash q -> Tot (squash r))) : Tot (squash r) =
let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (function | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q)))
false
DoublyLinkedList.fst
DoublyLinkedList._l_insert_before
val _l_insert_before (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a)
val _l_insert_before (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a)
let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 23, "end_line": 1674, "start_col": 0, "start_line": 1672 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
x0: 'a -> l: Prims.list 'a {FStar.List.Tot.Base.memP x0 l} -> x: 'a -> Prims.GTot (Prims.list 'a)
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.list", "FStar.List.Tot.Base.memP", "FStar.List.Tot.Base.append", "Prims.Cons", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Properties.split_using" ]
[]
false
false
false
false
false
let _l_insert_before (x0: 'a) (l: list 'a {x0 `memP` l}) (x: 'a) : GTot (list 'a) =
let l1, l2 = split_using l x0 in l1 `append` (x :: l2)
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.implies_intro
val implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q()))
val implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q()))
let implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q())) = let open FStar.Squash in let f' (x:p) : GTot (squash (q ())) = f (return_squash x) in return_squash (squash_double_arrow (return_squash f'))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 120, "start_col": 0, "start_line": 110 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f')) let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p) let exists_intro (a:Type) (p:a -> Type) (v:a) (f: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x)) = exists_intro_simple a p v (f())
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash p -> Type0) -> f: (_: Prims.squash p -> Prims.squash (q ())) -> Prims.squash (p ==> q ())
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "FStar.Squash.return_squash", "Prims.l_imp", "FStar.Squash.squash_double_arrow" ]
[]
false
false
true
false
false
let implies_intro (p: Type) (q: (squash p -> Type)) (f: (squash p -> Tot (squash (q ())))) : Tot (squash (p ==> q ())) =
let open FStar.Squash in let f' (x: p) : GTot (squash (q ())) = f (return_squash x) in return_squash (squash_double_arrow (return_squash f'))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.or_intro_left
val or_intro_left (p:Type) (q:squash (~p) -> Type) (f:unit -> Tot (squash p)) : Tot (squash (p \/ q()))
val or_intro_left (p:Type) (q:squash (~p) -> Type) (f:unit -> Tot (squash p)) : Tot (squash (p \/ q()))
let or_intro_left (p:Type) (q:squash (~p) -> Type) (f:unit -> Tot (squash p)) : Tot (squash (p \/ q())) = f()
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 127, "start_col": 0, "start_line": 122 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f')) let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p) let exists_intro (a:Type) (p:a -> Type) (v:a) (f: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x)) = exists_intro_simple a p v (f()) let implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q())) = let open FStar.Squash in let f' (x:p) : GTot (squash (q ())) = f (return_squash x) in return_squash (squash_double_arrow (return_squash f'))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash (~p) -> Type0) -> f: (_: Prims.unit -> Prims.squash p) -> Prims.squash (p \/ q ())
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_not", "Prims.unit", "Prims.l_or" ]
[]
false
false
true
false
false
let or_intro_left (p: Type) (q: (squash (~p) -> Type)) (f: (unit -> Tot (squash p))) : Tot (squash (p \/ q ())) =
f ()
false
DoublyLinkedList.fst
DoublyLinkedList.lemma_dll_links_contained
val lemma_dll_links_contained (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in (h0 `contains` (nodes.[ i ] @ h0).flink) /\ (h0 `contains` (nodes.[ i ] @ h0).blink)))
val lemma_dll_links_contained (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in (h0 `contains` (nodes.[ i ] @ h0).flink) /\ (h0 `contains` (nodes.[ i ] @ h0).blink)))
let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 1221, "start_col": 0, "start_line": 1204 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> d: DoublyLinkedList.dll t -> i: Prims.nat -> FStar.Pervasives.Lemma (requires DoublyLinkedList.dll_valid h0 d /\ i < FStar.List.Tot.Base.length (FStar.Ghost.reveal (Mkdll?.nodes d))) (ensures (let nodes = Mkdll?.nodes d in DoublyLinkedList.contains h0 (Mknode?.flink ((FStar.Ghost.reveal nodes).[ i ] @ h0)) /\ DoublyLinkedList.contains h0 (Mknode?.blink ((FStar.Ghost.reveal nodes).[ i ] @ h0))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.dll", "Prims.nat", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "Prims.unit", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "FStar.List.Tot.Base.length", "Prims.bool", "DoublyLinkedList.extract_nodelist_contained", "Prims.op_Addition", "DoublyLinkedList.extract_nodelist_conn", "DoublyLinkedList.nodelist", "FStar.Ghost.reveal", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.l_and", "DoublyLinkedList.dll_valid", "Prims.b2t", "Prims.op_LessThan", "Prims.squash", "DoublyLinkedList.contains", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "DoublyLinkedList.op_String_Access", "DoublyLinkedList.__proj__Mknode__item__blink", "FStar.Ghost.erased", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_dll_links_contained (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in (h0 `contains` (nodes.[ i ] @ h0).flink) /\ (h0 `contains` (nodes.[ i ] @ h0).blink))) =
let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.exists_intro_simple
val exists_intro_simple (a: Type) (p: (a -> Type)) (v: a) (f: squash (p v)) : Tot (squash (exists x. p x))
val exists_intro_simple (a: Type) (p: (a -> Type)) (v: a) (f: squash (p v)) : Tot (squash (exists x. p x))
let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p)
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 99, "start_col": 0, "start_line": 91 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f'))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> p: (_: a -> Type0) -> v: a -> f: Prims.squash (p v) -> Prims.squash (exists (x: a). p x)
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "FStar.Squash.squash_double_sum", "FStar.Squash.return_squash", "Prims.dtuple2", "Prims.Mkdtuple2", "Prims.l_Exists" ]
[]
false
false
true
false
false
let exists_intro_simple (a: Type) (p: (a -> Type)) (v: a) (f: squash (p v)) : Tot (squash (exists x. p x)) =
let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p)
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.and_elim
val and_elim (p:Type) (q:squash p -> Type) (r:Type) (_:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r)
val and_elim (p:Type) (q:squash p -> Type) (r:Type) (_:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r)
let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q)))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 77, "start_col": 0, "start_line": 68 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash p -> Type0) -> r: Type -> x: Prims.squash (p /\ q ()) -> f: (_: Prims.squash p -> _: Prims.squash (q ()) -> Prims.squash r) -> Prims.squash r
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_and", "FStar.Squash.bind_squash", "Prims.pair", "FStar.Squash.return_squash" ]
[]
false
false
true
false
false
let and_elim (p: Type) (q: (squash p -> Type)) (r: Type) (x: squash (p /\ q ())) (f: (squash p -> squash (q ()) -> Tot (squash r))) : Tot (squash r) =
let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q)))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.or_intro_right
val or_intro_right (p:Type) (q:squash (~p) -> Type) (f:squash (~p) -> Tot (squash (q()))) : Tot (squash (p \/ q()))
val or_intro_right (p:Type) (q:squash (~p) -> Type) (f:squash (~p) -> Tot (squash (q()))) : Tot (squash (p \/ q()))
let or_intro_right (p:Type) (q:squash (~p) -> Type) (f:squash (~p) -> Tot (squash (q()))) : Tot (squash (p \/ q())) = or_elim_simple p (~p) (p \/ q()) () (fun s_p -> or_intro_left p q (fun _ -> s_p)) (fun s_np -> f s_np)
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 138, "start_col": 0, "start_line": 129 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f')) let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p) let exists_intro (a:Type) (p:a -> Type) (v:a) (f: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x)) = exists_intro_simple a p v (f()) let implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q())) = let open FStar.Squash in let f' (x:p) : GTot (squash (q ())) = f (return_squash x) in return_squash (squash_double_arrow (return_squash f')) let or_intro_left (p:Type) (q:squash (~p) -> Type) (f:unit -> Tot (squash p)) : Tot (squash (p \/ q())) = f()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash (~p) -> Type0) -> f: (_: Prims.squash (~p) -> Prims.squash (q ())) -> Prims.squash (p \/ q ())
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_not", "FStar.Classical.Sugar.or_elim_simple", "Prims.l_or", "FStar.Classical.Sugar.or_intro_left", "Prims.unit" ]
[]
false
false
true
false
false
let or_intro_right (p: Type) (q: (squash (~p) -> Type)) (f: (squash (~p) -> Tot (squash (q ())))) : Tot (squash (p \/ q ())) =
or_elim_simple p (~p) (p \/ q ()) () (fun s_p -> or_intro_left p q (fun _ -> s_p)) (fun s_np -> f s_np)
false
DoublyLinkedList.fst
DoublyLinkedList.tot_defragmentable_fragment_to_dll
val tot_defragmentable_fragment_to_dll (#t: Type) (h0: heap) (f: fragment t { fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead @ h0).blink == null) /\ ((b.ptail @ h0).flink == null))) }) : Tot (d: dll t { dll_valid h0 d /\ (dll_fp0 d) `loc_equiv` (fragment_fp0 f) /\ (reveal d.nodes == (match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> (reveal p1.pnodes) `append` (reveal p2.pnodes) | Frag3 p1 p2 p3 -> ((reveal p1.pnodes) `append` (reveal p2.pnodes)) `append` (reveal p3.pnodes))) })
val tot_defragmentable_fragment_to_dll (#t: Type) (h0: heap) (f: fragment t { fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead @ h0).blink == null) /\ ((b.ptail @ h0).flink == null))) }) : Tot (d: dll t { dll_valid h0 d /\ (dll_fp0 d) `loc_equiv` (fragment_fp0 f) /\ (reveal d.nodes == (match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> (reveal p1.pnodes) `append` (reveal p2.pnodes) | Frag3 p1 p2 p3 -> ((reveal p1.pnodes) `append` (reveal p2.pnodes)) `append` (reveal p3.pnodes))) })
let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 927, "start_col": 0, "start_line": 897 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 40, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: DoublyLinkedList.heap -> f: DoublyLinkedList.fragment t { DoublyLinkedList.fragment_valid h0 f /\ DoublyLinkedList.fragment_defragmentable h0 f /\ (DoublyLinkedList.fragment_length f > 0 ==> (let _ = (match f with | DoublyLinkedList.Frag1 #_ p1 -> p1, p1 | DoublyLinkedList.Frag2 #_ p1 p2 -> p1, p2 | DoublyLinkedList.Frag3 #_ p1 _ p3 -> p1, p3) <: DoublyLinkedList.piece t * DoublyLinkedList.piece t in (let FStar.Pervasives.Native.Mktuple2 #_ #_ a b = _ in Mknode?.blink (Mkpiece?.phead a @ h0) == LowStar.Buffer.null /\ Mknode?.flink (Mkpiece?.ptail b @ h0) == LowStar.Buffer.null) <: Prims.logical)) } -> d: DoublyLinkedList.dll t { DoublyLinkedList.dll_valid h0 d /\ DoublyLinkedList.loc_equiv (DoublyLinkedList.dll_fp0 d) (DoublyLinkedList.fragment_fp0 f) /\ FStar.Ghost.reveal (Mkdll?.nodes d) == (match f with | DoublyLinkedList.Frag0 #_ -> [] | DoublyLinkedList.Frag1 #_ p1 -> FStar.Ghost.reveal (Mkpiece?.pnodes p1) | DoublyLinkedList.Frag2 #_ p1 p2 -> FStar.Ghost.reveal (Mkpiece?.pnodes p1) @ FStar.Ghost.reveal (Mkpiece?.pnodes p2) | DoublyLinkedList.Frag3 #_ p1 p2 p3 -> (FStar.Ghost.reveal (Mkpiece?.pnodes p1) @ FStar.Ghost.reveal (Mkpiece?.pnodes p2)) @ FStar.Ghost.reveal (Mkpiece?.pnodes p3)) }
Prims.Tot
[ "total" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.fragment", "Prims.l_and", "DoublyLinkedList.fragment_valid", "DoublyLinkedList.fragment_defragmentable", "Prims.l_imp", "Prims.b2t", "Prims.op_GreaterThan", "DoublyLinkedList.fragment_length", "DoublyLinkedList.piece", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.node", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.op_At", "DoublyLinkedList.__proj__Mkpiece__item__phead", "LowStar.Buffer.null", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.__proj__Mkpiece__item__ptail", "Prims.logical", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "DoublyLinkedList.empty_list", "DoublyLinkedList.tot_piece_to_dll", "DoublyLinkedList.piece_merge", "Prims.unit", "DoublyLinkedList.piece_merge_fp0", "DoublyLinkedList.dll", "DoublyLinkedList.dll_valid", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "DoublyLinkedList.fragment_fp0", "Prims.list", "LowStar.Buffer.pointer", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.Nil", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Base.append" ]
[]
false
false
false
false
false
let tot_defragmentable_fragment_to_dll (#t: Type) (h0: heap) (f: fragment t { fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead @ h0).blink == null) /\ ((b.ptail @ h0).flink == null))) }) : Tot (d: dll t { dll_valid h0 d /\ (dll_fp0 d) `loc_equiv` (fragment_fp0 f) /\ (reveal d.nodes == (match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> (reveal p1.pnodes) `append` (reveal p2.pnodes) | Frag3 p1 p2 p3 -> ((reveal p1.pnodes) `append` (reveal p2.pnodes)) `append` (reveal p3.pnodes))) }) =
match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3)
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.or_elim
val or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r)
val or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r)
let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q))
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 66, "start_col": 0, "start_line": 53 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash (~p) -> Type0) -> r: Type -> p_or: Prims.squash (p \/ q ()) -> left: (_: Prims.squash p -> Prims.squash r) -> right: (_: Prims.squash (~p) -> _: Prims.squash (q ()) -> Prims.squash r) -> Prims.squash r
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.l_not", "Prims.l_or", "FStar.Classical.Sugar.or_elim_simple" ]
[]
false
false
true
false
false
let or_elim (p: Type) (q: (squash (~p) -> Type)) (r: Type) (p_or: squash (p \/ q ())) (left: (squash p -> Tot (squash r))) (right: (squash (~p) -> squash (q ()) -> Tot (squash r))) : Tot (squash r) =
or_elim_simple p (~p) r () (fun (s: squash p) -> left s) (fun (np: squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p: squash p) -> left pf_p) (fun (pf_q: squash (q ())) -> right np pf_q))
false
FStar.Classical.Sugar.fst
FStar.Classical.Sugar.and_intro
val and_intro (p:Type) (q:squash p -> Type) (left:unit -> Tot (squash p)) (right:squash p -> Tot (squash (q()))) : Tot (squash (p /\ q()))
val and_intro (p:Type) (q:squash p -> Type) (left:unit -> Tot (squash p)) (right:squash p -> Tot (squash (q()))) : Tot (squash (p /\ q()))
let and_intro (p:Type) (q:squash p -> Type) (f:unit -> Tot (squash p)) (g:squash p -> Tot (squash (q()))) : Tot (squash (p /\ q())) = let _ = f() in g()
{ "file_name": "ulib/FStar.Classical.Sugar.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 146, "start_col": 0, "start_line": 140 }
(* Copyright 2021 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Classical.Sugar /// This module provides a few combinators that are targeted /// by the desugaring phase of the F* front end let forall_elim (#a:Type) (#p:a -> Type) (v:a) (f:squash (forall (x:a). p x)) : Tot (squash (p v)) = () let exists_elim #t #p #q s_ex_p f = let open FStar.Squash in bind_squash s_ex_p (fun ex_p -> bind_squash ex_p (fun (sig_p: (x:t & p x)) -> let (| x, px |) = sig_p in f x (return_squash px))) let or_elim_simple (p:Type) (q:Type) (r:Type) (x:squash (p \/ q)) (f:squash p -> Tot (squash r)) (g:squash q -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_or_q -> bind_squash p_or_q (fun p_cor_q -> match p_cor_q with | Prims.Left p -> f (return_squash p) | Prims.Right q -> g (return_squash q))) let or_elim (p:Type) (q:squash (~p) -> Type) (r:Type) (p_or:squash (p \/ q())) (left:squash p -> Tot (squash r)) (right:squash (~p) -> squash (q()) -> Tot (squash r)) : Tot (squash r) = or_elim_simple p (~p) r () (fun (s:squash p) -> left s) (fun (np:squash (~p)) -> or_elim_simple p (q ()) r p_or (fun (pf_p:squash p) -> left pf_p) (fun (pf_q:squash (q())) -> right np pf_q)) let and_elim (p:Type) (q:squash p -> Type) (r:Type) (x:squash (p /\ q())) (f:squash p -> squash (q()) -> Tot (squash r)) : Tot (squash r) = let open FStar.Squash in bind_squash x (fun p_and_q -> bind_squash p_and_q (fun (Prims.Pair p q) -> f (return_squash p) (return_squash q))) let forall_intro (a:Type) (p:a -> Type) (f: (x:a -> Tot (squash (p x)))) : Tot (squash (forall x. p x)) = let open FStar.Squash in let f' (x:a) : GTot (squash (p x)) = f x in return_squash (squash_double_arrow (return_squash f')) let exists_intro_simple (a:Type) (p:a -> Type) (v:a) (f: squash (p v)) : Tot (squash (exists x. p x)) = let open FStar.Squash in let p = (| v, f |) in squash_double_sum (return_squash p) let exists_intro (a:Type) (p:a -> Type) (v:a) (f: unit -> Tot (squash (p v))) : Tot (squash (exists x. p x)) = exists_intro_simple a p v (f()) let implies_intro (p:Type) (q:squash p -> Type) (f:(squash p -> Tot (squash (q())))) : Tot (squash (p ==> q())) = let open FStar.Squash in let f' (x:p) : GTot (squash (q ())) = f (return_squash x) in return_squash (squash_double_arrow (return_squash f')) let or_intro_left (p:Type) (q:squash (~p) -> Type) (f:unit -> Tot (squash p)) : Tot (squash (p \/ q())) = f() let or_intro_right (p:Type) (q:squash (~p) -> Type) (f:squash (~p) -> Tot (squash (q()))) : Tot (squash (p \/ q())) = or_elim_simple p (~p) (p \/ q()) () (fun s_p -> or_intro_left p q (fun _ -> s_p)) (fun s_np -> f s_np)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Squash.fsti.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "FStar.Classical.Sugar.fst" }
[ { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Classical", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Type0 -> q: (_: Prims.squash p -> Type0) -> left: (_: Prims.unit -> Prims.squash p) -> right: (_: Prims.squash p -> Prims.squash (q ())) -> Prims.squash (p /\ q ())
Prims.Tot
[ "total" ]
[]
[ "Prims.squash", "Prims.unit", "Prims.l_and" ]
[]
false
false
true
false
false
let and_intro (p: Type) (q: (squash p -> Type)) (f: (unit -> Tot (squash p))) (g: (squash p -> Tot (squash (q ())))) : Tot (squash (p /\ q ())) =
let _ = f () in g ()
false
DoublyLinkedList.fst
DoublyLinkedList.aux_unchanged_payload
val aux_unchanged_payload (#t h0 h1 n0: _) (nl: nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0 @ h0).p == (n0 @ h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)))) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl))
val aux_unchanged_payload (#t h0 h1 n0: _) (nl: nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0 @ h0).p == (n0 @ h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)))) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl))
let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ())
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 82, "end_line": 1402, "start_col": 0, "start_line": 1380 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> n0: b: LowStar.Monotonic.Buffer.mbuffer (DoublyLinkedList.node t) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node t)) (LowStar.Buffer.trivial_preorder (DoublyLinkedList.node t)) { LowStar.Monotonic.Buffer.length b == 1 /\ LowStar.Monotonic.Buffer.length b == 1 /\ LowStar.Monotonic.Buffer.length b == 1 } -> nl: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_buffer n0) h0 h1 /\ Mknode?.p (n0 @ h0) == Mknode?.p (n0 @ h1) /\ DoublyLinkedList.nodelist_aa_r nl /\ (FStar.List.Tot.Base.memP n0 nl \/ LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer n0) (DoublyLinkedList.nodelist_fp0 nl))) (ensures DoublyLinkedList.unchanged_node_vals h0 h1 nl) (decreases FStar.List.Tot.Base.length nl)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.mbuffer", "DoublyLinkedList.node", "LowStar.Buffer.trivial_preorder", "Prims.l_and", "Prims.eq2", "Prims.int", "LowStar.Monotonic.Buffer.length", "DoublyLinkedList.nodelist", "LowStar.Buffer.pointer", "Prims.list", "FStar.Classical.or_elim", "FStar.List.Tot.Base.memP", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "DoublyLinkedList.nodelist_fp0", "Prims.unit", "Prims.l_False", "Prims.l_or", "DoublyLinkedList.extract_nodelist_fp0", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "FStar.List.Tot.Base.index", "FStar.List.Tot.Properties.index_of", "Prims.logical", "DoublyLinkedList.unchanged_node_val", "Prims._assert", "Prims.l_imp", "DoublyLinkedList.aux_unchanged_payload", "LowStar.Monotonic.Buffer.modifies", "DoublyLinkedList.__proj__Mknode__item__p", "DoublyLinkedList.op_At", "DoublyLinkedList.nodelist_aa_r", "Prims.squash", "DoublyLinkedList.unchanged_node_vals", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec aux_unchanged_payload #t h0 h1 n0 (nl: nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0 @ h0).p == (n0 @ h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)))) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) =
match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_: (_: unit { match _ with | _ -> n0 `memP` nl | _ -> False })) -> FStar.Classical.or_elim #_ #_ #goal (fun (_: (_: unit { match _ with | _ -> n == n0 | _ -> False })) -> ()) (fun (_: (_: unit { match _ with | _ -> n0 `memP` nl' | _ -> False })) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_: (_: unit { match _ with | _ -> Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl) | _ -> False })) -> ())
false
DoublyLinkedList.fst
DoublyLinkedList._l_remove_mid
val _l_remove_mid (l: list 'a {length l > 0}) (x: 'a{x `memP` l}) : GTot (list 'a)
val _l_remove_mid (l: list 'a {length l > 0}) (x: 'a{x `memP` l}) : GTot (list 'a)
let _l_remove_mid (l:list 'a{length l > 0}) (x:'a {x `memP` l}) : GTot (list 'a) = let l1, x0 :: l2 = lemma_split_using l x; split_using l x in assert (x == x0); l1 `append` l2
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 1840, "start_col": 0, "start_line": 1837 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20" let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y ) #reset-options let rec _lemma_only_head_can_point_left_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) = match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0) let rec _lemma_only_tail_can_point_right_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l)) = match l with | [_] -> () | _ -> _lemma_only_tail_can_point_right_to_null h0 e (tl l) let rec _lemma_all_nodes_are_unique (#t:Type) (h0:heap) (l:nodelist t) (i j:nat) : Lemma (requires ( (nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l)@h0).blink == null) /\ (index l i == index l j))) (ensures (i = j)) = match i, j with | 0, 0 -> () | 0, _ -> extract_nodelist_conn h0 l (j - 1) | _, 0 -> extract_nodelist_conn h0 l (i - 1) | _ -> extract_nodelist_conn h0 l (i - 1); extract_nodelist_conn h0 l (j - 1); _lemma_all_nodes_are_unique h0 l (i - 1) (j - 1) #set-options "--z3rlimit 50" let dll_remove_tail (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.ltail@h0).blink) h0 h1 /\ _aux_fp_split_by_node d y d.ltail /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == fst (unsnoc (reveal d.nodes)))) = let h0 = ST.get () in let e = d.ltail in let e1 = (!*e).blink in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; if is_null e1 then ( _lemma_only_head_can_point_left_to_null h0 e d.nodes; empty_list ) else ( extract_nodelist_contained h0 d.nodes (length (d.nodes) - 2); extract_nodelist_conn h0 d.nodes (length (d.nodes) - 2); // assert (e == (reveal d.nodes).[length (reveal d.nodes) - 1]); // assert (e1 == (reveal d.nodes).[length (reveal d.nodes) - 2]); !=|> e1; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e1 e in let Frag2 p1 p2 = f in lemma_snoc_length (reveal p1.pnodes, e); // assert (reveal p1.pnodes == fst (unsnoc (reveal d.nodes))); let f' = Frag1 p1 in piece_remains_valid_f h0 h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e1 (d.nodes); // assert (reveal y.nodes == reveal p1.pnodes); y ) #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: Prims.list 'a {FStar.List.Tot.Base.length l > 0} -> x: 'a{FStar.List.Tot.Base.memP x l} -> Prims.GTot (Prims.list 'a)
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.list", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "FStar.List.Tot.Base.memP", "FStar.List.Tot.Base.append", "Prims.unit", "Prims._assert", "Prims.eq2", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Properties.split_using", "FStar.List.Tot.Properties.lemma_split_using" ]
[]
false
false
false
false
false
let _l_remove_mid (l: list 'a {length l > 0}) (x: 'a{x `memP` l}) : GTot (list 'a) =
let l1, x0 :: l2 = lemma_split_using l x; split_using l x in assert (x == x0); l1 `append` l2
false
DoublyLinkedList.fst
DoublyLinkedList._lemma_only_head_can_point_left_to_null
val _lemma_only_head_can_point_left_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l))
val _lemma_only_head_can_point_left_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l))
let rec _lemma_only_head_can_point_left_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) = match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 1767, "start_col": 0, "start_line": 1756 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20" let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y ) #reset-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> e: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> l: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.memP e l /\ Mknode?.blink (e @ h0) == LowStar.Buffer.null /\ DoublyLinkedList.nodelist_conn h0 l) (ensures e == FStar.List.Tot.Base.hd l)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.nodelist", "Prims.list", "FStar.Classical.or_elim", "Prims.eq2", "FStar.List.Tot.Base.hd", "Prims.l_not", "Prims.unit", "Prims.squash", "DoublyLinkedList.extract_nodelist_conn", "DoublyLinkedList._lemma_only_head_can_point_left_to_null", "FStar.List.Tot.Base.tl", "Prims.l_and", "FStar.List.Tot.Base.memP", "LowStar.Buffer.buffer", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.op_At", "LowStar.Buffer.null", "DoublyLinkedList.nodelist_conn", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec _lemma_only_head_can_point_left_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) =
match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0)
false
DoublyLinkedList.fst
DoublyLinkedList._lemma_all_nodes_are_unique
val _lemma_all_nodes_are_unique (#t: Type) (h0: heap) (l: nodelist t) (i j: nat) : Lemma (requires ((nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l) @ h0).blink == null) /\ (index l i == index l j))) (ensures (i = j))
val _lemma_all_nodes_are_unique (#t: Type) (h0: heap) (l: nodelist t) (i j: nat) : Lemma (requires ((nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l) @ h0).blink == null) /\ (index l i == index l j))) (ensures (i = j))
let rec _lemma_all_nodes_are_unique (#t:Type) (h0:heap) (l:nodelist t) (i j:nat) : Lemma (requires ( (nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l)@h0).blink == null) /\ (index l i == index l j))) (ensures (i = j)) = match i, j with | 0, 0 -> () | 0, _ -> extract_nodelist_conn h0 l (j - 1) | _, 0 -> extract_nodelist_conn h0 l (i - 1) | _ -> extract_nodelist_conn h0 l (i - 1); extract_nodelist_conn h0 l (j - 1); _lemma_all_nodes_are_unique h0 l (i - 1) (j - 1)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 52, "end_line": 1793, "start_col": 0, "start_line": 1777 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20" let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y ) #reset-options let rec _lemma_only_head_can_point_left_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) = match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0) let rec _lemma_only_tail_can_point_right_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l)) = match l with | [_] -> () | _ -> _lemma_only_tail_can_point_right_to_null h0 e (tl l)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> l: DoublyLinkedList.nodelist t -> i: Prims.nat -> j: Prims.nat -> FStar.Pervasives.Lemma (requires DoublyLinkedList.nodelist_conn h0 l /\ i < FStar.List.Tot.Base.length l /\ j < FStar.List.Tot.Base.length l /\ Mknode?.blink (FStar.List.Tot.Base.hd l @ h0) == LowStar.Buffer.null /\ FStar.List.Tot.Base.index l i == FStar.List.Tot.Base.index l j) (ensures i = j)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.nodelist", "Prims.nat", "FStar.Pervasives.Native.Mktuple2", "Prims.int", "DoublyLinkedList.extract_nodelist_conn", "Prims.op_Subtraction", "FStar.Pervasives.Native.tuple2", "DoublyLinkedList._lemma_all_nodes_are_unique", "Prims.unit", "Prims.l_and", "DoublyLinkedList.nodelist_conn", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.__proj__Mknode__item__blink", "DoublyLinkedList.op_At", "FStar.List.Tot.Base.hd", "LowStar.Buffer.null", "FStar.List.Tot.Base.index", "Prims.squash", "Prims.op_Equality", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec _lemma_all_nodes_are_unique (#t: Type) (h0: heap) (l: nodelist t) (i j: nat) : Lemma (requires ((nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l) @ h0).blink == null) /\ (index l i == index l j))) (ensures (i = j)) =
match i, j with | 0, 0 -> () | 0, _ -> extract_nodelist_conn h0 l (j - 1) | _, 0 -> extract_nodelist_conn h0 l (i - 1) | _ -> extract_nodelist_conn h0 l (i - 1); extract_nodelist_conn h0 l (j - 1); _lemma_all_nodes_are_unique h0 l (i - 1) (j - 1)
false
DoublyLinkedList.fst
DoublyLinkedList._lemma_only_tail_can_point_right_to_null
val _lemma_only_tail_can_point_right_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l))
val _lemma_only_tail_can_point_right_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l))
let rec _lemma_only_tail_can_point_right_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l)) = match l with | [_] -> () | _ -> _lemma_only_tail_can_point_right_to_null h0 e (tl l)
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 1775, "start_col": 0, "start_line": 1769 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20" let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y ) #reset-options let rec _lemma_only_head_can_point_left_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) = match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
h0: DoublyLinkedList.heap -> e: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> l: DoublyLinkedList.nodelist t -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.memP e l /\ Mknode?.flink (e @ h0) == LowStar.Buffer.null /\ DoublyLinkedList.nodelist_conn h0 l) (ensures e == FStar.List.Tot.Base.last l)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.nodelist", "Prims.list", "DoublyLinkedList._lemma_only_tail_can_point_right_to_null", "FStar.List.Tot.Base.tl", "Prims.unit", "Prims.l_and", "FStar.List.Tot.Base.memP", "Prims.eq2", "LowStar.Buffer.buffer", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "LowStar.Buffer.null", "DoublyLinkedList.nodelist_conn", "Prims.squash", "FStar.List.Tot.Base.last", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec _lemma_only_tail_can_point_right_to_null (#t: Type) (h0: heap) (e: pointer (node t)) (l: nodelist t) : Lemma (requires (e `memP` l /\ (e @ h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l)) =
match l with | [_] -> () | _ -> _lemma_only_tail_can_point_right_to_null h0 e (tl l)
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.generate_loop
val generate_loop: a:supported_alg -> k:lbytes (hash_length a) -> max:nat -> i:nat{i < max} -> a_spec a i -> Pure (a_spec a (i + 1) & Lib.Sequence.lseq uint8 (hash_length a)) (requires True) (ensures fun _ -> True)
val generate_loop: a:supported_alg -> k:lbytes (hash_length a) -> max:nat -> i:nat{i < max} -> a_spec a i -> Pure (a_spec a (i + 1) & Lib.Sequence.lseq uint8 (hash_length a)) (requires True) (ensures fun _ -> True)
let generate_loop a k max i vi = hmac_input_bound a; let v = hmac a k vi in v, v
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 29, "end_line": 79, "start_col": 0, "start_line": 77 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1 let reseed #a st entropy_input additional_input = let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1 let a_spec (a:fixed_len_alg) (i:nat) = Lib.Sequence.lseq uint8 (hash_length a) val generate_loop: a:supported_alg -> k:lbytes (hash_length a) -> max:nat -> i:nat{i < max} -> a_spec a i -> Pure (a_spec a (i + 1) & Lib.Sequence.lseq uint8 (hash_length a)) (requires True)
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
a: Spec.HMAC_DRBG.supported_alg -> k: Spec.Agile.HMAC.lbytes (Spec.Hash.Definitions.hash_length a) -> max: Prims.nat -> i: Prims.nat{i < max} -> vi: Spec.HMAC_DRBG.a_spec a i -> Prims.Pure (Spec.HMAC_DRBG.a_spec a (i + 1) * Lib.Sequence.lseq Lib.IntTypes.uint8 (Spec.Hash.Definitions.hash_length a))
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.Agile.HMAC.lbytes", "Spec.Hash.Definitions.hash_length", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Spec.HMAC_DRBG.a_spec", "FStar.Pervasives.Native.Mktuple2", "Prims.op_Addition", "Lib.Sequence.lseq", "Lib.IntTypes.uint8", "Spec.Agile.HMAC.hmac", "Prims.unit", "Spec.HMAC_DRBG.hmac_input_bound", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
false
false
let generate_loop a k max i vi =
hmac_input_bound a; let v = hmac a k vi in v, v
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.instantiate
val instantiate: #a:supported_alg -> entropy_input:bytes -> nonce:bytes -> personalization_string:bytes -> Pure (state a) (requires (hash_length a + Seq.length entropy_input + Seq.length nonce + Seq.length personalization_string + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
val instantiate: #a:supported_alg -> entropy_input:bytes -> nonce:bytes -> personalization_string:bytes -> Pure (state a) (requires (hash_length a + Seq.length entropy_input + Seq.length nonce + Seq.length personalization_string + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 59, "start_col": 0, "start_line": 54 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
entropy_input: Spec.Hash.Definitions.bytes -> nonce: Spec.Hash.Definitions.bytes -> personalization_string: Spec.Hash.Definitions.bytes -> Prims.Pure (Spec.HMAC_DRBG.state a)
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.Hash.Definitions.bytes", "Spec.Agile.HMAC.lbytes", "Spec.Hash.Definitions.hash_length", "Spec.HMAC_DRBG.State", "Spec.HMAC_DRBG.state", "FStar.Pervasives.Native.tuple2", "Spec.HMAC_DRBG.update", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.create", "Lib.IntTypes.uint8", "Lib.IntTypes.u8", "FStar.Seq.Base.op_At_Bar" ]
[]
false
false
false
false
false
let instantiate #a entropy_input nonce personalization_string =
let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1
false
DoublyLinkedList.fst
DoublyLinkedList.dll_insert_at_head
val dll_insert_at_head (#t: Type) (d: dll t) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes))
val dll_insert_at_head (#t: Type) (d: dll t) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes))
let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1492, "start_col": 0, "start_line": 1435 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 500, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d: DoublyLinkedList.dll t -> n: LowStar.Buffer.pointer (DoublyLinkedList.node t) -> FStar.HyperStack.ST.StackInline (DoublyLinkedList.dll t)
FStar.HyperStack.ST.StackInline
[]
[]
[ "DoublyLinkedList.dll", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.singleton_dll", "Prims.bool", "DoublyLinkedList.piece", "Prims.l_and", "DoublyLinkedList.dll_valid", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "DoublyLinkedList.fragment_fp0", "Prims.eq2", "Prims.list", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.Nil", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Base.append", "DoublyLinkedList.tot_defragmentable_fragment_to_dll", "Prims.unit", "DoublyLinkedList.piece_remains_valid_b", "DoublyLinkedList.piece_remains_valid", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.fragment", "DoublyLinkedList.Frag2", "DoublyLinkedList.tot_node_to_piece", "DoublyLinkedList.fragment_valid", "DoublyLinkedList.tot_dll_to_fragment", "DoublyLinkedList.aux_unchanged_payload_transitive", "DoublyLinkedList.aux_unchanged_payload", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "DoublyLinkedList.op_Less_Bar_Equals", "DoublyLinkedList.op_Equals_Bar_Greater", "DoublyLinkedList.op_Bang_Less_Bar_Equals", "LowStar.Buffer.pointer_or_null", "DoublyLinkedList.__proj__Mkdll__item__lhead", "LowStar.Monotonic.Buffer.is_null", "DoublyLinkedList.contains", "DoublyLinkedList.node_not_in_dll", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_union", "DoublyLinkedList.unchanged_node_vals", "Prims.Cons" ]
[]
false
true
false
false
false
let dll_insert_at_head (#t: Type) (d: dll t) (n: pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) =
if is_null d.lhead then (singleton_dll n) else (let h = d.lhead in let h0 = ST.get () in !<|=n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_b h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in y)
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.hmac_input_bound
val hmac_input_bound: a:supported_alg -> Lemma ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a)
val hmac_input_bound: a:supported_alg -> Lemma ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a)
let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a)
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 122, "end_line": 32, "start_col": 0, "start_line": 20 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
a: Spec.HMAC_DRBG.supported_alg -> FStar.Pervasives.Lemma (ensures Spec.Hash.Definitions.less_than_max_input_length (Spec.Hash.Definitions.hash_length a + Prims.pow2 32 + Prims.pow2 32 + 1 + Spec.Hash.Definitions.block_length a + Spec.Hash.Definitions.block_length a) a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.HMAC_DRBG.supported_alg", "FStar.Pervasives.assert_norm", "Prims.b2t", "Spec.Hash.Definitions.less_than_max_input_length", "Prims.op_Addition", "Spec.Hash.Definitions.hash_length", "Prims.pow2", "Spec.Hash.Definitions.block_length", "Spec.Hash.Definitions.hash_alg", "Spec.Hash.Definitions.SHA1", "Spec.Hash.Definitions.SHA2_256", "Spec.Hash.Definitions.SHA2_384", "Spec.Hash.Definitions.SHA2_512", "Prims.unit" ]
[]
false
false
true
false
false
let hmac_input_bound =
function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a)
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.reseed
val reseed: #a:supported_alg -> state a -> entropy_input:bytes -> additional_input:bytes -> Pure (state a) (requires (hash_length a + Seq.length entropy_input + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
val reseed: #a:supported_alg -> state a -> entropy_input:bytes -> additional_input:bytes -> Pure (state a) (requires (hash_length a + Seq.length entropy_input + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
let reseed #a st entropy_input additional_input = let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 64, "start_col": 0, "start_line": 61 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
st: Spec.HMAC_DRBG.state a -> entropy_input: Spec.Hash.Definitions.bytes -> additional_input: Spec.Hash.Definitions.bytes -> Prims.Pure (Spec.HMAC_DRBG.state a)
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.HMAC_DRBG.state", "Spec.Hash.Definitions.bytes", "Spec.Agile.HMAC.lbytes", "Spec.Hash.Definitions.hash_length", "Spec.HMAC_DRBG.State", "FStar.Pervasives.Native.tuple2", "Spec.HMAC_DRBG.update", "Spec.HMAC_DRBG.__proj__State__item__k", "Spec.HMAC_DRBG.__proj__State__item__v", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.op_At_Bar", "Lib.IntTypes.uint8" ]
[]
false
false
false
false
false
let reseed #a st entropy_input additional_input =
let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.update
val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 8, "end_line": 52, "start_col": 0, "start_line": 42 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
data: Spec.Hash.Definitions.bytes -> k: Spec.Agile.HMAC.lbytes (Spec.Hash.Definitions.hash_length a) -> v: Spec.Agile.HMAC.lbytes (Spec.Hash.Definitions.hash_length a) -> Prims.Pure (Spec.Agile.HMAC.lbytes (Spec.Hash.Definitions.hash_length a) * Spec.Agile.HMAC.lbytes (Spec.Hash.Definitions.hash_length a))
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.Hash.Definitions.bytes", "Spec.Agile.HMAC.lbytes", "Spec.Hash.Definitions.hash_length", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Lib.IntTypes.uint8", "FStar.Pervasives.Native.Mktuple2", "Prims.bool", "Spec.Agile.HMAC.hmac", "FStar.Seq.Base.op_At_Bar", "FStar.Seq.Base.seq", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "FStar.Seq.Base.cons", "Lib.IntTypes.u8", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
false
false
let update #a data k v =
let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v
false
Hacl.Spec.Bignum.Definitions.fst
Hacl.Spec.Bignum.Definitions.lbignum
val lbignum : t: Hacl.Spec.Bignum.Definitions.limb_t -> len: Lib.IntTypes.size_nat -> Type0
let lbignum (t:limb_t) len = lseq (limb t) len
{ "file_name": "code/bignum/Hacl.Spec.Bignum.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 46, "end_line": 21, "start_col": 0, "start_line": 21 }
module Hacl.Spec.Bignum.Definitions open FStar.Mul open Lib.IntTypes open Lib.Sequence #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" val blocks: x:size_pos -> m:size_pos -> Tot (r:size_pos{x <= m * r}) let blocks x m = (x - 1) / m + 1 val blocks0: x:size_nat -> m:size_pos -> Tot (r:size_pos{x <= m * r}) let blocks0 x m = if x = 0 then 1 else (x - 1) / m + 1 inline_for_extraction noextract let limb_t = t:inttype{t = U32 \/ t = U64} inline_for_extraction noextract
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": false, "source_file": "Hacl.Spec.Bignum.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.Sequence", "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": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
t: Hacl.Spec.Bignum.Definitions.limb_t -> len: Lib.IntTypes.size_nat -> Type0
Prims.Tot
[ "total" ]
[]
[ "Hacl.Spec.Bignum.Definitions.limb_t", "Lib.IntTypes.size_nat", "Lib.Sequence.lseq", "Hacl.Spec.Bignum.Definitions.limb" ]
[]
false
false
false
true
true
let lbignum (t: limb_t) len =
lseq (limb t) len
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.generate
val generate: #a:supported_alg -> state a -> n:nat -> additional_input:bytes -> Pure (option (lbytes n & state a)) (requires n <= max_output_length /\ (hash_length a + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
val generate: #a:supported_alg -> state a -> n:nat -> additional_input:bytes -> Pure (option (lbytes n & state a)) (requires n <= max_output_length /\ (hash_length a + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
let generate #a st n additional_input = if st.reseed_counter > reseed_interval then None else let k, v = if Seq.length additional_input > 0 then update additional_input st.k st.v else st.k, st.v in let max = n / hash_length a in let v, output = Lib.Sequence.generate_blocks (hash_length a) max max (a_spec a) (generate_loop a k max) v in let v, output = if max * hash_length a < n then let v = hmac a k v in v, output @| Lib.Sequence.sub #_ #(hash_length a) v 0 (n - max * hash_length a) else v, output in let k, v = update additional_input k v in Some (output, State k v (st.reseed_counter + 1))
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 52, "end_line": 104, "start_col": 0, "start_line": 81 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1 let reseed #a st entropy_input additional_input = let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1 let a_spec (a:fixed_len_alg) (i:nat) = Lib.Sequence.lseq uint8 (hash_length a) val generate_loop: a:supported_alg -> k:lbytes (hash_length a) -> max:nat -> i:nat{i < max} -> a_spec a i -> Pure (a_spec a (i + 1) & Lib.Sequence.lseq uint8 (hash_length a)) (requires True) (ensures fun _ -> True) let generate_loop a k max i vi = hmac_input_bound a; let v = hmac a k vi in v, v
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
st: Spec.HMAC_DRBG.state a -> n: Prims.nat -> additional_input: Spec.Hash.Definitions.bytes -> Prims.Pure (FStar.Pervasives.Native.option (Spec.Agile.HMAC.lbytes n * Spec.HMAC_DRBG.state a))
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.HMAC_DRBG.state", "Prims.nat", "Spec.Hash.Definitions.bytes", "Prims.op_GreaterThan", "Spec.HMAC_DRBG.__proj__State__item__reseed_counter", "Spec.HMAC_DRBG.reseed_interval", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HMAC.lbytes", "Prims.bool", "Spec.Hash.Definitions.hash_length", "Spec.HMAC_DRBG.a_spec", "Lib.Sequence.seq", "Lib.IntTypes.uint8", "Prims.eq2", "Prims.int", "Lib.Sequence.length", "FStar.Mul.op_Star", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "Spec.HMAC_DRBG.State", "Prims.op_Addition", "FStar.Pervasives.Native.option", "Spec.HMAC_DRBG.update", "Prims.op_LessThan", "FStar.Seq.Base.op_At_Bar", "Lib.Sequence.sub", "Prims.op_Subtraction", "Spec.Agile.HMAC.hmac", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Prims.op_Multiply", "Lib.Sequence.generate_blocks", "Spec.HMAC_DRBG.generate_loop", "Prims.op_Division", "FStar.Seq.Base.length", "Spec.HMAC_DRBG.__proj__State__item__k", "Spec.HMAC_DRBG.__proj__State__item__v" ]
[]
false
false
false
false
false
let generate #a st n additional_input =
if st.reseed_counter > reseed_interval then None else let k, v = if Seq.length additional_input > 0 then update additional_input st.k st.v else st.k, st.v in let max = n / hash_length a in let v, output = Lib.Sequence.generate_blocks (hash_length a) max max (a_spec a) (generate_loop a k max) v in let v, output = if max * hash_length a < n then let v = hmac a k v in v, output @| Lib.Sequence.sub #_ #(hash_length a) v 0 (n - max * hash_length a) else v, output in let k, v = update additional_input k v in Some (output, State k v (st.reseed_counter + 1))
false
Spec.HMAC_DRBG.fst
Spec.HMAC_DRBG.generate'
val generate': #a:supported_alg -> state a -> n:nat -> additional_input:bytes -> Pure (option (lbytes n & state a)) (requires n <= max_output_length /\ (hash_length a + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
val generate': #a:supported_alg -> state a -> n:nat -> additional_input:bytes -> Pure (option (lbytes n & state a)) (requires n <= max_output_length /\ (hash_length a + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True)
let generate' #a st n additional_input = hmac_input_bound a; if st.reseed_counter > reseed_interval then None else let k, v = if Seq.length additional_input > 0 then update additional_input st.k st.v else st.k, st.v in // let max = ceil (n / hash_length a) in let max = (n + hash_length a - 1) / hash_length a in let v, output = Lib.Sequence.generate_blocks (hash_length a) max max (a_spec a) (generate_loop a k max) v in let k, v = update additional_input k v in Some (Seq.slice output 0 n, State k v (st.reseed_counter + 1))
{ "file_name": "specs/drbg/Spec.HMAC_DRBG.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 135, "start_col": 0, "start_line": 117 }
module Spec.HMAC_DRBG open Lib.IntTypes open FStar.Seq open FStar.Mul open Spec.Hash.Definitions open Spec.Agile.HMAC #set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50" noeq type state (a:supported_alg) = | State: k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> reseed_counter:nat -> state a let hmac_input_bound = function | SHA1 -> let a = SHA1 in assert_norm ((hash_length a + pow2 32 + pow2 32 +1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_256 -> let a = SHA2_256 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_384 -> let a = SHA2_384 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) | SHA2_512 -> let a = SHA2_512 in assert_norm ((hash_length a + pow2 32 + pow2 32 + 1 + block_length a + block_length a) `less_than_max_input_length` a) val update: #a:supported_alg -> data:bytes -> k:lbytes (hash_length a) -> v:lbytes (hash_length a) -> Pure (lbytes (hash_length a) & lbytes (hash_length a)) (requires (hash_length a + Seq.length data + 1 + block_length a) `less_than_max_input_length` a) (ensures fun _ -> True) let update #a data k v = let zero_data = Seq.cons (u8 0) data in let k = hmac a k (v @| zero_data) in let v = hmac a k v in if Seq.length data = 0 then k, v else let one_data = Seq.cons (u8 1) data in let k = hmac a k (v @| one_data) in let v = hmac a k v in k, v let instantiate #a entropy_input nonce personalization_string = let seed_material = entropy_input @| nonce @| personalization_string in let k = Seq.create (hash_length a) (u8 0) in let v = Seq.create (hash_length a) (u8 1) in let k, v = update #a seed_material k v in State k v 1 let reseed #a st entropy_input additional_input = let seed_material = entropy_input @| additional_input in let k, v = update #a seed_material st.k st.v in State k v 1 let a_spec (a:fixed_len_alg) (i:nat) = Lib.Sequence.lseq uint8 (hash_length a) val generate_loop: a:supported_alg -> k:lbytes (hash_length a) -> max:nat -> i:nat{i < max} -> a_spec a i -> Pure (a_spec a (i + 1) & Lib.Sequence.lseq uint8 (hash_length a)) (requires True) (ensures fun _ -> True) let generate_loop a k max i vi = hmac_input_bound a; let v = hmac a k vi in v, v let generate #a st n additional_input = if st.reseed_counter > reseed_interval then None else let k, v = if Seq.length additional_input > 0 then update additional_input st.k st.v else st.k, st.v in let max = n / hash_length a in let v, output = Lib.Sequence.generate_blocks (hash_length a) max max (a_spec a) (generate_loop a k max) v in let v, output = if max * hash_length a < n then let v = hmac a k v in v, output @| Lib.Sequence.sub #_ #(hash_length a) v 0 (n - max * hash_length a) else v, output in let k, v = update additional_input k v in Some (output, State k v (st.reseed_counter + 1)) (** Equivalently, but proving it requires proving extensionality of generate_blocks *) val generate': #a:supported_alg -> state a -> n:nat -> additional_input:bytes -> Pure (option (lbytes n & state a)) (requires n <= max_output_length /\ (hash_length a + Seq.length additional_input + 1 + block_length a) `less_than_max_input_length` a)
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "Spec.Agile.HMAC.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Spec.HMAC_DRBG.fst" }
[ { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Agile.HMAC", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "Spec", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "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
st: Spec.HMAC_DRBG.state a -> n: Prims.nat -> additional_input: Spec.Hash.Definitions.bytes -> Prims.Pure (FStar.Pervasives.Native.option (Spec.Agile.HMAC.lbytes n * Spec.HMAC_DRBG.state a))
Prims.Pure
[]
[]
[ "Spec.HMAC_DRBG.supported_alg", "Spec.HMAC_DRBG.state", "Prims.nat", "Spec.Hash.Definitions.bytes", "Prims.op_GreaterThan", "Spec.HMAC_DRBG.__proj__State__item__reseed_counter", "Spec.HMAC_DRBG.reseed_interval", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.tuple2", "Spec.Agile.HMAC.lbytes", "Prims.bool", "Spec.Hash.Definitions.hash_length", "Spec.HMAC_DRBG.a_spec", "Lib.Sequence.seq", "Lib.IntTypes.uint8", "Prims.eq2", "Prims.int", "Lib.Sequence.length", "FStar.Mul.op_Star", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Base.slice", "Spec.HMAC_DRBG.State", "Prims.op_Addition", "FStar.Pervasives.Native.option", "Spec.HMAC_DRBG.update", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Prims.op_Multiply", "Lib.Sequence.generate_blocks", "Spec.HMAC_DRBG.generate_loop", "Prims.op_Division", "Prims.op_Subtraction", "FStar.Seq.Base.length", "Spec.HMAC_DRBG.__proj__State__item__k", "Spec.HMAC_DRBG.__proj__State__item__v", "Prims.unit", "Spec.HMAC_DRBG.hmac_input_bound" ]
[]
false
false
false
false
false
let generate' #a st n additional_input =
hmac_input_bound a; if st.reseed_counter > reseed_interval then None else let k, v = if Seq.length additional_input > 0 then update additional_input st.k st.v else st.k, st.v in let max = (n + hash_length a - 1) / hash_length a in let v, output = Lib.Sequence.generate_blocks (hash_length a) max max (a_spec a) (generate_loop a k max) v in let k, v = update additional_input k v in Some (Seq.slice output 0 n, State k v (st.reseed_counter + 1))
false
DoublyLinkedList.fst
DoublyLinkedList.lemma_dll_links_disjoint
val lemma_dll_links_disjoint (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in let left = (nodes.[ i ] @ h0).blink in let right = (nodes.[ i ] @ h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right)))
val lemma_dll_links_disjoint (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in let left = (nodes.[ i ] @ h0).blink in let right = (nodes.[ i ] @ h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right)))
let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () )))
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 1262, "start_col": 0, "start_line": 1225 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: DoublyLinkedList.heap -> d: DoublyLinkedList.dll t -> i: Prims.nat -> FStar.Pervasives.Lemma (requires DoublyLinkedList.dll_valid h0 d /\ i < FStar.List.Tot.Base.length (FStar.Ghost.reveal (Mkdll?.nodes d))) (ensures (let nodes = Mkdll?.nodes d in let left = Mknode?.blink ((FStar.Ghost.reveal nodes).[ i ] @ h0) in let right = Mknode?.flink ((FStar.Ghost.reveal nodes).[ i ] @ h0) in LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer left) (LowStar.Monotonic.Buffer.loc_buffer right)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "DoublyLinkedList.heap", "DoublyLinkedList.dll", "Prims.nat", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.list", "Prims.op_Equality", "Prims.int", "Prims.bool", "Prims.op_Subtraction", "FStar.List.Tot.Base.length", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "Prims.unit", "Prims._assert", "LowStar.Monotonic.Buffer.loc_includes", "DoublyLinkedList.nodelist_fp0", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.nodelist_split_aa", "Prims.Cons", "FStar.List.Pure.Properties.lemma_splitAt_reindex_right", "FStar.List.Pure.Properties.lemma_splitAt_reindex_left", "FStar.List.Tot.Base.hd", "FStar.List.Tot.Base.last", "DoublyLinkedList.extract_nodelist_conn", "FStar.Pervasives.Native.tuple2", "FStar.List.Pure.Properties.lemma_splitAt_index_hd", "FStar.List.Pure.Properties.lemma_splitAt", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "FStar.List.Tot.Base.splitAt", "FStar.List.Tot.Properties.lemma_unsnoc_length", "DoublyLinkedList.nodelist", "FStar.Ghost.reveal", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.l_and", "DoublyLinkedList.dll_valid", "Prims.b2t", "Prims.op_LessThan", "Prims.squash", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Buffer.pointer_or_null", "DoublyLinkedList.__proj__Mknode__item__flink", "DoublyLinkedList.op_At", "DoublyLinkedList.op_String_Access", "DoublyLinkedList.__proj__Mknode__item__blink", "FStar.Ghost.erased", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_dll_links_disjoint (#t: Type) (h0: heap) (d: dll t) (i: nat) : Lemma (requires ((dll_valid h0 d) /\ (i < length d.nodes))) (ensures (let nodes = d.nodes in let left = (nodes.[ i ] @ h0).blink in let right = (nodes.[ i ] @ h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) =
let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else (if i = length nl - 1 then (lemma_unsnoc_is_last nl) else (lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); lemma_splitAt_reindex_right i nl 1; nodelist_split_aa l1 (x :: l2); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); ())))
false
Hacl.Spec.Bignum.Definitions.fst
Hacl.Spec.Bignum.Definitions.limb_t
val limb_t : Type0
let limb_t = t:inttype{t = U32 \/ t = U64}
{ "file_name": "code/bignum/Hacl.Spec.Bignum.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 18, "start_col": 0, "start_line": 18 }
module Hacl.Spec.Bignum.Definitions open FStar.Mul open Lib.IntTypes open Lib.Sequence #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" val blocks: x:size_pos -> m:size_pos -> Tot (r:size_pos{x <= m * r}) let blocks x m = (x - 1) / m + 1 val blocks0: x:size_nat -> m:size_pos -> Tot (r:size_pos{x <= m * r}) let blocks0 x m = if x = 0 then 1 else (x - 1) / m + 1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": false, "source_file": "Hacl.Spec.Bignum.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.Sequence", "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": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Type0
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.inttype", "Prims.l_or", "Prims.b2t", "Prims.op_Equality", "Lib.IntTypes.U32", "Lib.IntTypes.U64" ]
[]
false
false
false
true
true
let limb_t =
t: inttype{t = U32 \/ t = U64}
false
DoublyLinkedList.fst
DoublyLinkedList.dll_remove_head
val dll_remove_head (#t: Type) (d: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead @ h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes))
val dll_remove_head (#t: Type) (d: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead @ h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes))
let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1752, "start_col": 0, "start_line": 1724 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d: DoublyLinkedList.dll t -> FStar.HyperStack.ST.StackInline (DoublyLinkedList.dll t)
FStar.HyperStack.ST.StackInline
[]
[]
[ "DoublyLinkedList.dll", "DoublyLinkedList.empty_list", "Prims.bool", "DoublyLinkedList.piece", "Prims.unit", "DoublyLinkedList.aux_unchanged_payload", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "Prims.l_and", "DoublyLinkedList.dll_valid", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "DoublyLinkedList.fragment_fp0", "Prims.eq2", "Prims.list", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "Prims.Nil", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Base.append", "DoublyLinkedList.tot_defragmentable_fragment_to_dll", "DoublyLinkedList.piece_remains_valid_b", "DoublyLinkedList.fragment", "DoublyLinkedList.Frag1", "DoublyLinkedList.tot_dll_to_fragment_split", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "DoublyLinkedList.op_Bang_Less_Bar_Equals", "LowStar.Monotonic.Buffer.is_null", "LowStar.Buffer.trivial_preorder", "LowStar.Buffer.pointer_or_null", "DoublyLinkedList.__proj__Mknode__item__flink", "LowStar.BufferOps.op_Bang_Star", "DoublyLinkedList.__proj__Mkdll__item__lhead", "Prims.b2t", "Prims.op_GreaterThan", "FStar.List.Tot.Base.length", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer", "DoublyLinkedList.op_At", "DoublyLinkedList._aux_fp_split_by_node", "DoublyLinkedList.unchanged_node_vals", "FStar.List.Tot.Base.tl" ]
[]
false
true
false
false
false
let dll_remove_head (#t: Type) (d: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead @ h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) =
let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then (empty_list) else (!<|=e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y)
false
DoublyLinkedList.fst
DoublyLinkedList.dll_append
val dll_append (#t: Type) (d1 d2: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d1) /\ (dll_valid h0 d2) /\ ((dll_fp0 d1) `Mod.loc_disjoint` (dll_fp0 d2)) )) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d1.ltail) (Mod.loc_buffer d2.lhead)) h0 h1 /\ (dll_fp0 y) `loc_equiv` ((dll_fp0 d1) `B.loc_union` (dll_fp0 d2)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == d1.nodes `append` d2.nodes))
val dll_append (#t: Type) (d1 d2: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d1) /\ (dll_valid h0 d2) /\ ((dll_fp0 d1) `Mod.loc_disjoint` (dll_fp0 d2)) )) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d1.ltail) (Mod.loc_buffer d2.lhead)) h0 h1 /\ (dll_fp0 y) `loc_equiv` ((dll_fp0 d1) `B.loc_union` (dll_fp0 d2)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == d1.nodes `append` d2.nodes))
let dll_append (#t:Type) (d1 d2:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d1) /\ (dll_valid h0 d2) /\ (dll_fp0 d1 `Mod.loc_disjoint` dll_fp0 d2))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d1.ltail) (Mod.loc_buffer d2.lhead)) h0 h1 /\ dll_fp0 y `loc_equiv` (dll_fp0 d1 `B.loc_union` dll_fp0 d2) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == d1.nodes `append` d2.nodes)) = let h0 = ST.get () in if is_null d1.lhead then ( let y = d2 in let h1 = ST.get () in aux_unchanged_payload_nomod h0 h1 y.nodes; y ) else ( if is_null d2.lhead then ( let y = d1 in let h1 = ST.get () in aux_unchanged_payload_nomod h0 h1 y.nodes; append_l_nil y.nodes; y ) else ( let n1 = d1.ltail in let n2 = d2.lhead in n1 =|> n2; let h0' = ST.get () in n1 <|= n2; let h1 = ST.get () in // let p1 = tot_dll_to_piece h0 d1 in let p2 = tot_dll_to_piece h0 d2 in let f' = Frag2 p1 p2 in piece_remains_valid_f h0 h0' p1; piece_remains_valid h0 h0' (Mod.loc_buffer n1) p2; piece_remains_valid h0' h1 (Mod.loc_buffer n2) p1; piece_remains_valid_b h0' h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last d1.nodes; aux_unchanged_payload h0 h0' n1 d1.nodes; aux_unchanged_payload h0 h0' n1 d2.nodes; aux_unchanged_payload h0' h1 n2 d1.nodes; aux_unchanged_payload h0' h1 n2 d2.nodes; aux_unchanged_payload_transitive h0 h0' h1 d1.nodes; aux_unchanged_payload_transitive h0 h0' h1 d2.nodes; aux_unchanged_payload_append h0 h1 d1.nodes d2.nodes; y ) )
{ "file_name": "examples/doublylinkedlist/DoublyLinkedList.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1963, "start_col": 0, "start_line": 1910 }
(* Copyright 2008-2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module DoublyLinkedList open FStar open FStar.HyperStack.ST open FStar.Ghost open LowStar.ModifiesPat open FStar.List.Tot open FStar.List.Pure open LowStar.BufferOps module Mod = LowStar.Modifies module ST = FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer /// Convenience renaming unfold let heap = HS.mem unfold let contains #a #rrel #rel h b = B.live #a #rrel #rel h b /// Convenience patterns let lemma_non_null (#t:Type) (a:pointer_or_null t) : Lemma (requires (a =!= null)) (ensures (len a = 1ul)) [SMTPat (len a)] = null_unique a /// Convenience operators unfold let (.[]) (s:list 'a) (n:nat{n < length s}) = index s n unfold let (~.) (#t:Type) (a:t) : Tot (erased (list t)) = [a] unfold let (^+) (#t:Type) (a:t) (b:erased (list t)) : Tot (erased (list t)) = Cons a b unfold let (+^) (#t:Type) (a:erased (list t)) (b:t) : Tot (erased (list t)) = append a [b] unfold let (^@^) (#t:Type) (a:erased (list t)) (b:erased (list t)) : Tot (erased (list t)) = append a b unfold let (@) (a:pointer 't) (h0:heap) = B.get h0 a 0 unfold let (^@) (a:pointer_or_null 't{a =!= null}) (h0:heap) = B.get h0 a 0 /// All the data structures #set-options "--__no_positivity" unopteq (** Node of a doubly linked list *) type node (t:Type0) = { (* forward link *) flink: pointer_or_null (node t); (* backward link *) blink: pointer_or_null (node t); (* payload *) p: t; } #reset-options private type nodelist t = list (pointer (node t)) unopteq (** Doubly linked list head *) type dll (t:Type0) ={ lhead: pointer_or_null (node t); ltail: pointer_or_null (node t); nodes: erased (nodelist t); } type nonempty_dll t = (h:dll t{h.lhead =!= null /\ h.ltail =!= null}) unopteq private (** An "almost valid" dll *) type piece t = { phead: pointer (node t); ptail: pointer (node t); pnodes: erased (nodelist t); } unopteq private (** An intermediate for when linked lists are being formed or destroyed *) type fragment t = | Frag0 : fragment t | Frag1 : piece t -> fragment t | Frag2 : piece t -> piece t -> fragment t | Frag3 : piece t -> piece t -> piece t -> fragment t /// Some useful empty initializers (** Initialize an element of a doubly linked list *) val empty_node: #t:Type -> payload:t -> node t let empty_node #t payload = { flink = null ; blink = null ; p = payload } (** Initialize a doubly linked list head *) val empty_list: #t:Type -> dll t let empty_list #t = { lhead = null ; ltail = null ; nodes = [] } /// Convenience wrappers for writing properties on fragments let fragment_for_each0 (#t:Type) (pr:piece t -> GTot Type0) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr p1 | Frag2 p1 p2 -> pr p1 /\ pr p2 | Frag3 p1 p2 p3 -> pr p1 /\ pr p2 /\ pr p3 let fragment_for_each1 (#t:Type) (#u:Type) (pr:u -> piece t -> GTot Type0) (v:u) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> pr v p1 | Frag2 p1 p2 -> pr v p1 /\ pr v p2 | Frag3 p1 p2 p3 -> pr v p1 /\ pr v p2 /\ pr v p3 let fragment_length (#t:Type) (f:fragment t) : GTot int = match f with | Frag0 -> 0 | Frag1 _ -> 1 | Frag2 _ _ -> 2 | Frag3 _ _ _ -> 3 /// Ghostly connections let dll_ghostly_connections (#t:Type) (d:dll t) : GTot Type0 = let nodes = d.nodes in match length nodes with | 0 -> d.lhead == null /\ d.ltail == null | _ -> d.lhead =!= null /\ d.ltail =!= null /\ d.lhead == hd nodes /\ d.ltail == last nodes let piece_ghostly_connections (#t:Type) (p:piece t) : GTot Type0 = let nodes = p.pnodes in match length nodes with | 0 -> False | _ -> p.phead == hd nodes /\ p.ptail == last nodes let rec fragment_ghostly_connections (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_ghostly_connections f /// Containment properties /// /// WARNING: [@@] and [^@] require containment to reasonably talk about /// what they do. let rec nodelist_contained0 (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> h0 `contains` n /\ nodelist_contained0 h0 ns let rec nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained0 h0 nl let dll_contained (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = h0 `contains` d.lhead /\ h0 `contains` d.ltail /\ nodelist_contained h0 d.nodes let piece_contained (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = h0 `contains` p.phead /\ h0 `contains` p.ptail /\ nodelist_contained h0 p.pnodes let rec fragment_contained (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_contained h0 f /// Footprints let node_fp_f (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.flink let node_fp_b (#t:Type) (n:node t) : GTot Mod.loc = Mod.loc_buffer n.blink let rec nodelist_fp0 (#t:Type) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns) let rec nodelist_fp_f (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).flink) (nodelist_fp_f h0 ns) let rec nodelist_fp_b (#t:Type) (h0:heap) (n:nodelist t) : GTot Mod.loc = match n with | [] -> Mod.loc_none | n :: ns -> Mod.loc_union (Mod.loc_buffer (n@h0).blink) (nodelist_fp_b h0 ns) let dll_fp0 (#t:Type) (d:dll t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_buffer d.ltail)) (nodelist_fp0 d.nodes) let dll_fp_f (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).flink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).flink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_f h0 d.nodes) let dll_fp_b (#t:Type) (h0:heap) (d:dll t) : GTot Mod.loc = let a = if g_is_null d.lhead then Mod.loc_none else Mod.loc_buffer (d.lhead^@h0).blink in let b = if g_is_null d.ltail then Mod.loc_none else Mod.loc_buffer (d.ltail^@h0).blink in Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union a b) (nodelist_fp_b h0 d.nodes) let piece_fp0 (#t:Type) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer p.phead) (Mod.loc_buffer p.ptail)) (nodelist_fp0 p.pnodes) let piece_fp_f (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).flink) (Mod.loc_buffer (p.ptail@h0).flink)) (nodelist_fp_f h0 p.pnodes) let piece_fp_b (#t:Type) (h0:heap) (p:piece t) : GTot Mod.loc = Mod.loc_union // ghostly connections should give us this union for // free, but still useful to have (Mod.loc_union (Mod.loc_buffer (p.phead@h0).blink) (Mod.loc_buffer (p.ptail@h0).blink)) (nodelist_fp_b h0 p.pnodes) let rec fragment_fp0 (#t:Type) (f:fragment t) : GTot Mod.loc = match f with | Frag0 -> Mod.loc_none | Frag1 p1 -> piece_fp0 p1 | Frag2 p1 p2 -> Mod.loc_union (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> Mod.loc_union (piece_fp0 p1) (Mod.loc_union (piece_fp0 p2) (piece_fp0 p3)) /// Helper patterns for footprints let loc_includes_union_l_nodelist_fp0 (#t: Type) (s1 s2:loc) (nl:nodelist t) : Lemma (requires (loc_includes s1 (nodelist_fp0 nl) \/ loc_includes s2 (nodelist_fp0 nl))) (ensures (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))) [SMTPat (loc_includes (loc_union s1 s2) (nodelist_fp0 nl))] = loc_includes_union_l s1 s2 (nodelist_fp0 nl) let loc_includes_union_l_dll_fp0 (#t: Type) (s1 s2:loc) (d:dll t) : Lemma (requires (loc_includes s1 (dll_fp0 d) \/ loc_includes s2 (dll_fp0 d))) (ensures (loc_includes (loc_union s1 s2) (dll_fp0 d))) [SMTPat (loc_includes (loc_union s1 s2) (dll_fp0 d))] = loc_includes_union_l s1 s2 (dll_fp0 d) let loc_includes_union_l_piece_fp0 (#t: Type) (s1 s2:loc) (p:piece t) : Lemma (requires (loc_includes s1 (piece_fp0 p) \/ loc_includes s2 (piece_fp0 p))) (ensures (loc_includes (loc_union s1 s2) (piece_fp0 p))) [SMTPat (loc_includes (loc_union s1 s2) (piece_fp0 p))] = loc_includes_union_l s1 s2 (piece_fp0 p) let loc_includes_union_l_fragment_fp0 (#t: Type) (s1 s2:loc) (f:fragment t) : Lemma (requires (loc_includes s1 (fragment_fp0 f) \/ loc_includes s2 (fragment_fp0 f))) (ensures (loc_includes (loc_union s1 s2) (fragment_fp0 f))) [SMTPat (loc_includes (loc_union s1 s2) (fragment_fp0 f))] = loc_includes_union_l s1 s2 (fragment_fp0 f) /// Equivalence for locations let loc_equiv (a b:Mod.loc) = Mod.loc_includes a b /\ Mod.loc_includes b a let loc_equiv_trans (a b c:Mod.loc) : Lemma (requires (loc_equiv a b /\ loc_equiv b c)) (ensures (loc_equiv a c)) [SMTPat (loc_equiv a b); SMTPat (loc_equiv b c); SMTPat (loc_equiv a c)] = Mod.loc_includes_trans a b c; Mod.loc_includes_trans c b a let loc_equiv_union_union_loc (a b c:Mod.loc) : Lemma (requires (loc_equiv b c)) (ensures (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))) [SMTPat (loc_equiv (Mod.loc_union a b) (Mod.loc_union a c))] = let incl = Mod.loc_includes in let u = Mod.loc_union in // assert (b `incl` c); Mod.loc_includes_union_l a b c; // assert ((a `u` b) `incl` c); Mod.loc_includes_union_l a b a; // assert ((a `u` b) `incl` a); // assert ((a `u` b) `incl` (a `u` c)); Mod.loc_includes_union_l a c b; Mod.loc_includes_union_l a c a /// Anti aliasing properties let node_aa (#t:Type) (n:node t) : GTot Type0 = Mod.loc_disjoint (node_fp_f n) (node_fp_b n) let rec nodelist_aa_r (#t:Type) (nl:nodelist t) : GTot Type0 = match nl with | [] -> True | n :: ns -> Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_r ns let rec nodelist_aa_l (#t:Type) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns) /\ nodelist_aa_l ns let nodelist_aa (#t:Type) (nl:nodelist t) : GTot Type0 = nodelist_aa_l nl /\ nodelist_aa_r nl let dll_aa (#t:Type) (d:dll t) : GTot Type0 = nodelist_aa d.nodes let piece_aa (#t:Type) (p:piece t) : GTot Type0 = nodelist_aa p.pnodes let rec fragment_aa0 (#t:Type) (f:fragment t) : GTot Type0 = fragment_for_each0 piece_aa f let rec fragment_aa_lr (#t:Type) (f:fragment t) : GTot Type0 = match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2) | Frag3 p1 p2 p3 -> ((Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)) /\ (Mod.loc_disjoint (piece_fp0 p2) (piece_fp0 p3)) /\ (Mod.loc_disjoint (piece_fp0 p3) (piece_fp0 p1))) let fragment_aa (#t:Type) (f:fragment t) : GTot Type0 = fragment_aa0 f /\ fragment_aa_lr f /// Connectivity properties let ( |>> ) (#t:Type) (a:node t) (b:pointer (node t)) : GTot Type0 = a.flink == b let ( <<| ) (#t:Type) (a:pointer (node t)) (b: node t) : GTot Type0 = b.blink == a let rec nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 (decreases (length nl)) = match nl with | [] -> True | n1 :: rest -> match rest with | [] -> True | n2 :: ns -> n1@h0 |>> n2 /\ n1 <<| n2@h0 /\ nodelist_conn h0 rest let dll_conn (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = nodelist_conn h0 d.nodes /\ (d.lhead =!= null ==> (d.lhead@h0).blink == null) /\ (d.ltail =!= null ==> (d.ltail@h0).flink == null) let piece_conn (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = nodelist_conn h0 p.pnodes let rec fragment_conn (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_for_each1 piece_conn h0 f /// Validity properties /// /// These are just a combination of /// + Ghostly connections /// + Containment properties /// + Anti aliasing properties /// + Connectivity properties let nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : GTot Type0 = nodelist_contained h0 nl /\ nodelist_aa nl /\ nodelist_conn h0 nl let dll_valid (#t:Type) (h0:heap) (d:dll t) : GTot Type0 = dll_ghostly_connections d /\ dll_contained h0 d /\ dll_aa d /\ dll_conn h0 d let piece_valid (#t:Type) (h0:heap) (p:piece t) : GTot Type0 = piece_ghostly_connections p /\ piece_contained h0 p /\ piece_aa p /\ piece_conn h0 p let fragment_valid (#t:Type) (h0:heap) (f:fragment t) : GTot Type0 = fragment_ghostly_connections f /\ fragment_contained h0 f /\ fragment_aa f /\ fragment_conn h0 f /// Talk about payloads of nodes remaining constant let unchanged_node_val h0 h1 n = (h0 `contains` n ==> ((n@h0).p == (n@h1).p /\ h1 `contains` n)) let rec unchanged_node_vals (h0 h1:HS.mem) (ns:nodelist 'a) : GTot prop = match ns with | [] -> True | n :: ns' -> unchanged_node_val h0 h1 n /\ unchanged_node_vals h0 h1 ns' /// Useful operations on nodes let ( =|> ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ b@h0 == b@h1 /\ (a@h1) |>> b)) = a *= { !*a with flink = b } let ( <|= ) (#t:Type) (a:pointer (node t)) (b:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a /\ h0 `contains` b /\ Mod.loc_disjoint (Mod.loc_buffer a) (Mod.loc_buffer b))) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer b) h0 h1 /\ a@h0 == a@h1 /\ unchanged_node_val h0 h1 b /\ (b@h0).flink == (b@h1).flink /\ a <<| (b@h1))) = b *= { !*b with blink = a } let ( !=|> ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).blink == (a@h1).blink /\ (a@h1).flink == null)) = a *= { !*a with flink = null } let ( !<|= ) (#t:Type) (a:pointer (node t)) : StackInline unit (requires (fun h0 -> h0 `contains` a)) (ensures (fun h0 _ h1 -> Mod.modifies (Mod.loc_buffer a) h0 h1 /\ unchanged_node_val h0 h1 a /\ (a@h0).flink == (a@h1).flink /\ (a@h1).blink == null)) = a *= { !*a with blink = null } /// Extraction lemmas: these allow one to use one of the properties /// above, which are defined inductively, to get the property at one /// of the latter elements of the list. let rec extract_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_contained h0 nl)) (ensures (h0 `contains` nl.[i])) = match i with | 0 -> () | _ -> extract_nodelist_contained h0 (tl nl) (i - 1) let rec extract_nodelist_fp0 (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer nl.[i]))) = match i with | 0 -> () | _ -> extract_nodelist_fp0 (tl nl) (i - 1) let rec extract_nodelist_aa_r (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_r nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 right))) = match i with | 0 -> () | _ -> extract_nodelist_aa_r (tl nl) (i - 1) let rec extract_nodelist_aa_l (#t:Type) (nl:nodelist t) (i:nat{i < length nl}) : Lemma (requires (nodelist_aa_l nl)) (ensures ( let left, n, right = split3 nl i in Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 left))) (decreases (length nl)) = if i = length nl - 1 then () else ( let a, b = unsnoc nl in lemma_unsnoc_length nl; let left, n, right = split3 nl i in lemma_unsnoc_split3 nl i; // assert (append (left) (n :: (fst (unsnoc right))) == a); extract_nodelist_aa_l a i; lemma_split3_unsnoc nl i ) let rec extract_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) (i:nat{i < length nl - 1}) : Lemma (requires (nodelist_conn h0 nl)) (ensures ( (nl.[i]@h0 |>> nl.[i+1]) /\ (nl.[i] <<| nl.[i+1]@h0))) (decreases (length nl)) = match i with | 0 -> () | _ -> extract_nodelist_conn h0 (tl nl) (i - 1) /// Validity is maintained upon breaking the lists, via (hd :: tl) let rec nodelist_remains_aa_l (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa_l nl /\ length nl > 0)) (ensures (nodelist_aa_l (tl nl))) (decreases (length nl)) [SMTPat (nodelist_aa_l (tl nl))] = match nl with | [n] -> () | _ -> let ns, n = unsnoc nl in lemma_unsnoc_length nl; let ns', n' = unsnoc (tl nl) in // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (n' == n); // assert (ns' == tl ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns')); nodelist_remains_aa_l ns (* Rest of the validity predicates are held trivially due to their direction of definition *) /// Properties maintained upon breaking the list, via unsnoc let rec fst_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (nodelist_fp0 (fst (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> fst_unsnoc_nodelist_fp0 ns let rec snd_unsnoc_nodelist_fp0 (#t:Type) (nl:nodelist t) : Lemma (requires (length nl > 0)) (ensures (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))) [SMTPat (Mod.loc_includes (nodelist_fp0 nl) (Mod.loc_buffer (snd (unsnoc nl))))] = match nl with | [_] -> () | n :: ns -> snd_unsnoc_nodelist_fp0 ns let rec fst_unsnoc_nodelist_contained (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_contained h0 nl /\ length nl > 0)) (ensures (nodelist_contained h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_contained h0 (tl nl) let rec fst_unsnoc_nodelist_aa (#t:Type) (nl:nodelist t) : Lemma (requires (nodelist_aa nl /\ length nl > 0)) (ensures (nodelist_aa (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_aa (tl nl); // assert (nodelist_aa_l (fst (unsnoc nl))); let n :: ns = fst (unsnoc nl) in Mod.loc_disjoint_includes (Mod.loc_buffer n) (nodelist_fp0 (tl nl)) (Mod.loc_buffer n) (nodelist_fp0 ns); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 ns)); // assert (nodelist_aa_r (fst (unsnoc nl))); () let rec fst_unsnoc_nodelist_conn (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_conn h0 nl /\ length nl > 0)) (ensures (nodelist_conn h0 (fst (unsnoc nl)))) = match nl with | [_] -> () | _ -> fst_unsnoc_nodelist_conn h0 (tl nl) let fst_unsnoc_nodelist_valid (#t:Type) (h0:heap) (nl:nodelist t) : Lemma (requires (nodelist_valid h0 nl /\ length nl > 0)) (ensures (nodelist_valid h0 (fst (unsnoc nl)))) = fst_unsnoc_nodelist_contained h0 nl; fst_unsnoc_nodelist_aa nl; fst_unsnoc_nodelist_conn h0 nl /// Footprints are included, even upon breaking nodelist even further let rec nodelist_includes_r_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let _, a = splitAt i nl in let _, b = splitAt j nl in Mod.loc_includes (nodelist_fp0 a) (nodelist_fp0 b))) (decreases (j - i)) = if i = j then () else ( let temp, a = splitAt i nl in lemma_splitAt nl temp a i; let temp, b = splitAt j nl in lemma_splitAt nl temp b j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (tl a == b); () ) else ( nodelist_includes_r_fp0 nl i (j - 1); nodelist_includes_r_fp0 nl (j - 1) j; let temp, c = splitAt (j - 1) nl in lemma_splitAt nl temp c (j - 1); Mod.loc_includes_trans (nodelist_fp0 a) (nodelist_fp0 c) (nodelist_fp0 b) ) ) let rec nodelist_includes_l_fp0 (#t:Type) (nl:nodelist t) (i j:nat) : Lemma (requires (i <= j /\ j < length nl)) (ensures ( let a, _ = splitAt i nl in let b, _ = splitAt j nl in Mod.loc_includes (nodelist_fp0 b) (nodelist_fp0 a))) (decreases (j - i)) = if i = j then () else ( let a, a' = splitAt i nl in lemma_splitAt nl a a' i; let b, b' = splitAt j nl in lemma_splitAt nl b b' j; if i = j - 1 then ( List.Pure.Properties.splitAt_assoc i 1 nl; // assert (b == append a [hd a']); lemma_unsnoc_append a [hd a']; // assert (snd (unsnoc b) == hd a'); // assert (fst (unsnoc b) == a); fst_unsnoc_nodelist_fp0 b ) else ( nodelist_includes_l_fp0 nl i (j - 1); nodelist_includes_l_fp0 nl (j - 1) j; let c, c' = splitAt (j - 1) nl in lemma_splitAt nl c c' (j - 1); Mod.loc_includes_trans (nodelist_fp0 b) (nodelist_fp0 c) (nodelist_fp0 a) ) ) /// Total conversions between fragments, pieces, and dlls let tot_dll_to_piece (#t:Type) (h0:heap) (d:nonempty_dll t{dll_valid h0 d}) : Tot (p:piece t{piece_valid h0 p}) = { phead = d.lhead ; ptail = d.ltail ; pnodes = d.nodes } let tot_dll_to_fragment (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d /\ d.lhead =!= null}) : Tot (f:fragment t{fragment_valid h0 f}) = Frag1 (tot_dll_to_piece h0 d) let tot_piece_to_dll (#t:Type) (h0:heap) (p:piece t{ piece_valid h0 p /\ (p.phead@h0).blink == null /\ (p.ptail@h0).flink == null}) : Tot (d:dll t{dll_valid h0 d}) = { lhead = p.phead ; ltail = p.ptail ; nodes = p.pnodes } (* The conversions piece<->fragment are trivial *) /// Properties maintained when appending nodelists let rec nodelist_append_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) (ensures (nodelist_contained h0 (append nl1 nl2))) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_append_contained h0 nl1' nl2 let rec nodelist_append_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ( loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)))) = match nl1 with | [] -> () | n :: nl1' -> nodelist_append_fp0 nl1' nl2; // assert (loc_equiv // (nodelist_fp0 (append nl1' nl2)) // (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))); // assert (loc_equiv // (nodelist_fp0 nl1) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1'))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 nl1')) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2)))); // assert (loc_equiv // (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); loc_equiv_trans (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 nl1') (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2))); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2)) // (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 (append nl1' nl2)))); () #set-options "--z3rlimit 20" let rec nodelist_append_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l nl1 /\ nodelist_aa_l nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_l (append nl1 nl2))) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; nodelist_append_fp0 nl1 nl2'; // assert (nodelist_aa_l nl2'); assert (Mod.loc_includes (nodelist_fp0 nl2) (nodelist_fp0 nl2')); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); lemma_unsnoc_is_last nl2; assert (Mod.loc_includes (nodelist_fp0 nl2) (Mod.loc_buffer n)); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl1)); // assert (loc_equiv (nodelist_fp0 (append nl1 nl2')) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2'))); nodelist_append_aa_l nl1 nl2'; // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (append nl1 nl2'))); lemma_unsnoc_append nl1 nl2; // assert (append nl1 nl2' == fst (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 (fst (unsnoc (append nl1 nl2))))); () #reset-options let rec nodelist_append_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r nl1 /\ nodelist_aa_r nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa_r (append nl1 nl2))) = match nl1 with | [] -> () | _ -> nodelist_append_fp0 (tl nl1) nl2; nodelist_append_aa_r (tl nl1) nl2 let nodelist_append_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) (ensures (nodelist_aa (append nl1 nl2))) = nodelist_append_aa_l nl1 nl2; nodelist_append_aa_r nl1 nl2 let rec nodelist_append_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_conn h0 (append nl1 nl2))) = match nl1 with | [_] -> () | _ -> nodelist_append_conn h0 (tl nl1) nl2 let nodelist_append_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ length nl1 > 0 /\ length nl2 > 0 /\ // For "= 0", it is trivially held (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) (ensures (nodelist_valid h0 (append nl1 nl2))) = nodelist_append_contained h0 nl1 nl2; nodelist_append_aa nl1 nl2; nodelist_append_conn h0 nl1 nl2 /// Useful property for for piece merging let loc_includes_union_r_inv (a b c:Mod.loc) : Lemma (requires (Mod.loc_includes a (Mod.loc_union b c))) (ensures (Mod.loc_includes a b /\ Mod.loc_includes a c)) = Mod.loc_includes_union_l b c b; Mod.loc_includes_trans a (Mod.loc_union b c) b; Mod.loc_includes_union_l b c c; Mod.loc_includes_trans a (Mod.loc_union b c) c /// Piece merging #set-options "--z3rlimit 10" val piece_merge (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Pure (piece t) (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (fun p -> (piece_valid h0 p) /\ (reveal p.pnodes == p1.pnodes `append` p2.pnodes))) let piece_merge #t h0 p1 p2 = let p = { phead = p1.phead ; ptail = p2.ptail ; pnodes = p1.pnodes ^@^ p2.pnodes } in lemma_append_last p1.pnodes p2.pnodes; nodelist_append_valid h0 p1.pnodes p2.pnodes; p #reset-options let piece_merge_fp0 (#t:Type) (h0:heap) (p1:piece t{piece_valid h0 p1}) (p2:piece t{piece_valid h0 p2}) : Lemma (requires (let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\ (a <<| b@h0) /\ Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2))) (ensures (loc_equiv (piece_fp0 (piece_merge h0 p1 p2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)))) = let p = piece_merge h0 p1 p2 in let n1, n2, n = reveal p1.pnodes, reveal p2.pnodes, reveal p.pnodes in nodelist_append_fp0 n1 n2; // assert (loc_equiv (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); // assert (hd n1 == p1.phead); // assert (Mod.loc_includes (nodelist_fp0 n1) (Mod.loc_buffer p1.phead)); // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.phead)); // assert (last n2 == p2.ptail); extract_nodelist_fp0 n2 (length n2 - 1); lemma_unsnoc_is_last n2; // assert (Mod.loc_includes (nodelist_fp0 n2) (Mod.loc_buffer p2.ptail)); extract_nodelist_fp0 n (length n - 1); lemma_unsnoc_is_last n; // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_buffer p.ptail)); loc_includes_union_r_inv (nodelist_fp0 n) (nodelist_fp0 n1) (nodelist_fp0 n2); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n1)); // assert (Mod.loc_includes (nodelist_fp0 n) (nodelist_fp0 n2)); // // assert (loc_equiv (nodelist_fp0 n) (piece_fp0 p)); extract_nodelist_fp0 n1 (length n1 - 1); lemma_unsnoc_is_last n1; // assert (loc_equiv (nodelist_fp0 n1) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 n2) (piece_fp0 p2)); // // assert (Mod.loc_includes (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2))); Mod.loc_includes_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); Mod.loc_includes_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (Mod.loc_includes (piece_fp0 p) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // // assert (Mod.loc_includes (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (nodelist_fp0 n)); loc_equiv_trans (nodelist_fp0 n) (piece_fp0 p) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)); loc_equiv_trans (nodelist_fp0 n) (Mod.loc_union (nodelist_fp0 n1) (nodelist_fp0 n2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); loc_equiv_trans (piece_fp0 p) (nodelist_fp0 n) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)) /// Fragment merging to a dll let rec fragment_defragmentable (#t:Type) (h0:heap) (f:fragment t{fragment_valid h0 f}) : GTot Type0 = let aux (p1 p2:(p:piece t{piece_valid h0 p})) = let a, b = last p1.pnodes, hd p2.pnodes in (a@h0 |>> b) /\(a <<| b@h0) in match f with | Frag0 -> True | Frag1 p1 -> True | Frag2 p1 p2 -> aux p1 p2 | Frag3 p1 p2 p3 -> aux p1 p2 /\ aux p2 p3 let single_piece_fragment_valid (#t:Type) (h0:heap) (p:piece t) : Lemma (requires (piece_valid h0 p)) (ensures (fragment_valid h0 (Frag1 p))) = () #set-options "--z3rlimit 40 --initial_ifuel 2" let tot_defragmentable_fragment_to_dll (#t:Type) (h0:heap) (f:fragment t{ fragment_valid h0 f /\ fragment_defragmentable h0 f /\ (fragment_length f > 0 ==> (let a, b = match f with | Frag1 p1 -> p1, p1 | Frag2 p1 p2 -> p1, p2 | Frag3 p1 _ p3 -> p1, p3 in ((a.phead@h0).blink == null) /\ ((b.ptail@h0).flink == null))) }) : Tot (d:dll t{dll_valid h0 d /\ dll_fp0 d `loc_equiv` fragment_fp0 f /\ (reveal d.nodes == ( match f with | Frag0 -> [] | Frag1 p1 -> reveal p1.pnodes | Frag2 p1 p2 -> reveal p1.pnodes `append` reveal p2.pnodes | Frag3 p1 p2 p3 -> reveal p1.pnodes `append` reveal p2.pnodes `append` reveal p3.pnodes)) }) = match f with | Frag0 -> empty_list | Frag1 p1 -> tot_piece_to_dll h0 p1 | Frag2 p1 p2 -> piece_merge_fp0 h0 p1 p2; tot_piece_to_dll h0 (piece_merge h0 p1 p2) | Frag3 p1 p2 p3 -> piece_merge_fp0 h0 p1 p2; let p' = piece_merge h0 p1 p2 in piece_merge_fp0 h0 p' p3; tot_piece_to_dll h0 (piece_merge h0 p' p3) #reset-options /// Properties of nodelists maintained upon splitting nodelists let rec nodelist_split_contained (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_contained h0 (append nl1 nl2))) (ensures (nodelist_contained h0 nl1 /\ nodelist_contained h0 nl2)) = match nl1 with | [] -> () | _ :: nl1' -> nodelist_split_contained h0 nl1' nl2 let rec nodelist_split_fp0 (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = match nl1 with | [] | [_] -> () | _ -> match nl2 with | [] -> () | _ -> // assert (length nl1 > 1); // assert (length nl2 > 0); nodelist_split_fp0 (tl nl1) nl2; append_length nl1 nl2; nodelist_includes_r_fp0 (tl (append nl1 nl2)) 0 (length nl1 - 1); // assert (snd (splitAt 0 (tl (append nl1 nl2))) == tl (append nl1 nl2)); // assert (snd (splitAt (length nl1 - 1) (tl (append nl1 nl2))) == snd (splitAt (length nl1) (append nl1 nl2))); lemma_append_splitAt nl1 nl2; // assert (snd (splitAt (length nl1) (append nl1 nl2)) == nl2); // assert (Mod.loc_includes (nodelist_fp0 (tl (append nl1 nl2))) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl (append nl1 nl2)))); // assert (Mod.loc_disjoint (Mod.loc_buffer (hd nl1)) (nodelist_fp0 nl2)); // assert (Mod.loc_disjoint (nodelist_fp0 (tl nl1)) (nodelist_fp0 nl2)); Mod.loc_disjoint_union_r (nodelist_fp0 nl2) (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1)); // assert (Mod.loc_disjoint (Mod.loc_union (Mod.loc_buffer (hd nl1)) (nodelist_fp0 (tl nl1))) (nodelist_fp0 nl2)); () #set-options "--z3rlimit 30" let rec nodelist_split_fp0_equiv (#t:Type) (nl1 nl2:nodelist t) : Lemma (ensures ((loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (nodelist_fp0 nl1) (nodelist_fp0 nl2))))) = match nl1 with | [] -> () | n :: ns -> nodelist_split_fp0_equiv ns nl2; assert (loc_equiv (nodelist_fp0 (append nl1 nl2)) (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2)))); // OBSERVE assert (loc_equiv (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_union (nodelist_fp0 ns) (nodelist_fp0 nl2))) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (nodelist_fp0 ns)) (nodelist_fp0 nl2))) // OBSERVE let rec nodelist_split_aa_l (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_l (append nl1 nl2))) (ensures (nodelist_aa_l nl1 /\ nodelist_aa_l nl2)) (decreases (length nl2)) = match nl2 with | [] -> append_l_nil nl1 | _ -> let nl2', n = unsnoc nl2 in lemma_unsnoc_length nl2; lemma_unsnoc_append nl1 nl2; // assert (nodelist_aa_l (append nl1 nl2)); // assert (nodelist_aa_l (append nl1 nl2')); nodelist_split_aa_l nl1 nl2'; // assert (nodelist_aa_l nl2'); // assert (n == snd (unsnoc (append nl1 nl2))); // assert (n == snd (unsnoc nl2)); nodelist_append_fp0 nl1 nl2'; // assert (Mod.loc_includes (nodelist_fp0 (append nl1 nl2')) (nodelist_fp0 nl2')); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (nodelist_fp0 nl2')); // assert (nodelist_aa_l nl2); () #reset-options let rec nodelist_split_aa_r (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa_r (append nl1 nl2))) (ensures (nodelist_aa_r nl1 /\ nodelist_aa_r nl2)) = match nl1 with | [] -> () | _ -> nodelist_split_aa_r (tl nl1) nl2; nodelist_append_fp0 (tl nl1) nl2 let nodelist_split_aa (#t:Type) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_aa (append nl1 nl2))) (ensures (nodelist_aa nl1 /\ nodelist_aa nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2))) = nodelist_split_fp0 nl1 nl2; nodelist_split_aa_l nl1 nl2; nodelist_split_aa_r nl1 nl2 let rec nodelist_split_conn (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires ( (nodelist_conn h0 (append nl1 nl2)) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_conn h0 nl1 /\ nodelist_conn h0 nl2 /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = match nl1 with | [_] -> () | _ -> nodelist_split_conn h0 (tl nl1) nl2 let nodelist_split_valid (#t:Type) (h0:heap) (nl1 nl2:nodelist t) : Lemma (requires (nodelist_valid h0 (append nl1 nl2) /\ length nl1 > 0 /\ length nl2 > 0)) // For "= 0", it is trivially held (ensures (nodelist_valid h0 nl1 /\ nodelist_valid h0 nl2 /\ Mod.loc_disjoint (nodelist_fp0 nl1) (nodelist_fp0 nl2) /\ (last nl1)@h0 |>> (hd nl2) /\ (last nl1) <<| (hd nl2)@h0)) = nodelist_split_contained h0 nl1 nl2; nodelist_split_aa nl1 nl2; nodelist_split_conn h0 nl1 nl2 /// Useful lemma to convert from dll_fp0 or piece_fp0 to nodelist_fp0 /// and vice-versa let dll_fp0_is_nodelist_fp0 (#t:Type) (d:dll t) : Lemma (requires (dll_ghostly_connections d)) (ensures (loc_equiv (dll_fp0 d) (nodelist_fp0 d.nodes))) = if length d.nodes > 0 then lemma_unsnoc_is_last d.nodes else () let piece_fp0_is_nodelist_fp0 (#t:Type) (p:piece t) : Lemma (requires (piece_ghostly_connections p)) (ensures (loc_equiv (piece_fp0 p) (nodelist_fp0 p.pnodes))) = lemma_unsnoc_is_last (reveal p.pnodes) /// Tot dll to fragment, with splitting #set-options "--z3rlimit 60 --initial_fuel 8 --initial_ifuel 1" let tot_dll_to_fragment_split (#t:Type) (h0:heap) (d:dll t{dll_valid h0 d}) (n1 n2:pointer (node t)) : Pure (fragment t) (requires ( n1 `memP` d.nodes /\ n2 `memP` d.nodes /\ n1@h0 |>> n2 /\ n1 <<| n2@h0)) (ensures (fun f -> fragment_valid h0 f /\ fragment_length f = 2 /\ loc_equiv (dll_fp0 d) (fragment_fp0 f) /\ (let Frag2 p1 p2 = f in reveal d.nodes == reveal p1.pnodes `append` reveal p2.pnodes))) = let split_nodes = elift2_p split_using d.nodes (hide n2) in lemma_split_using d.nodes n2; let l1 = elift1 fst split_nodes in let l2 = elift1 snd split_nodes in let p1 = { phead = d.lhead ; ptail = n1 ; pnodes = l1 } in let p2 = { phead = n2 ; ptail = d.ltail ; pnodes = l2 } in let f = Frag2 p1 p2 in dll_fp0_is_nodelist_fp0 d; // assert (loc_equiv (dll_fp0 d) (nodelist_fp0 (reveal d.nodes))); nodelist_split_fp0_equiv l1 l2; nodelist_split_valid h0 l1 l2; lemma_unsnoc_is_last l1; lemma_unsnoc_is_last l2; lemma_unsnoc_is_last (reveal d.nodes); // assert (piece_ghostly_connections p1); // assert ( n2 == hd (reveal l2) ); lemma_append_last l1 l2; // assert ( last (reveal l2) == last (append (reveal l1) (reveal l2)) ); // assert ( d.ltail == last (reveal l2) ); // assert (piece_ghostly_connections p2); // assert (fragment_ghostly_connections f); // assert (nodelist_contained h0 (reveal p1.pnodes)); // assert (nodelist_contained h0 (reveal p2.pnodes)); extract_nodelist_contained h0 l1 (length l1 - 1); // assert (h0 `contains` p1.ptail); // assert (fragment_contained h0 f); // assert (nodelist_aa (reveal p1.pnodes)); // assert (nodelist_aa (reveal p2.pnodes)); piece_fp0_is_nodelist_fp0 p1; piece_fp0_is_nodelist_fp0 p2; // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2)))); // assert (loc_equiv (nodelist_fp0 (reveal l1)) (piece_fp0 p1)); // assert (loc_equiv (nodelist_fp0 (reveal l2)) (piece_fp0 p2)); // assert (loc_equiv // (Mod.loc_union (nodelist_fp0 (reveal l1)) (nodelist_fp0 (reveal l2))) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); loc_equiv_trans (dll_fp0 d) (Mod.loc_union (nodelist_fp0 l1) (nodelist_fp0 l2)) (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2)); // assert (loc_equiv (dll_fp0 d) // (Mod.loc_union (piece_fp0 p1) (piece_fp0 p2))); // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p2)); // assert (fragment_aa f); // assert (nodelist_conn h0 (reveal p1.pnodes)); // assert (nodelist_conn h0 (reveal p2.pnodes)); // assert (fragment_conn h0 f); f #reset-options /// Creating a dll from a single node. Pure and ST forms of this. let tot_node_to_dll (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (dll t) (requires ( (h0 `contains` n) /\ (((n@h0).flink == null)) /\ ((n@h0).blink == null))) (ensures (fun d -> dll_valid h0 d)) = { lhead = n ; ltail = n ; nodes = ~. n } let singleton_dll (#t:Type) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (h0 `contains` n))) (ensures (fun h0 d h1 -> Mod.modifies (Mod.loc_buffer n) h0 h1 /\ dll_valid h1 d /\ unchanged_node_vals h0 h1 d.nodes /\ reveal d.nodes == [n])) = !=|> n; !<|= n; tot_node_to_dll (ST.get ()) n /// Creating a piece from a single node. let tot_node_to_piece (#t:Type) (h0:heap) (n:pointer (node t)) : Pure (piece t) (requires ( (h0 `contains` n))) (ensures (fun p -> piece_valid h0 p)) = { phead = n ; ptail = n ; pnodes = ~. n } /// Getting the "tail" of a piece let tot_piece_tail (#t:Type) (h0:heap) (p:piece t) (n:pointer (node t)) : Pure (piece t) (requires ( (piece_valid h0 p) /\ (n == (((p.phead)@h0).flink)) /\ (length p.pnodes > 1))) (ensures (fun q -> (piece_valid h0 q) /\ (reveal q.pnodes) == tl p.pnodes)) = { phead = n ; ptail = p.ptail ; pnodes = elift1_p (tot_to_gtot tl) p.pnodes } /// If a dll is valid, then both the forward and backward links of /// each of the nodes are contained in the heap, and disjoint from /// each other let lemma_dll_links_contained (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in (h0 `contains` (nodes.[i]@h0).flink) /\ (h0 `contains` (nodes.[i]@h0).blink))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else extract_nodelist_contained h0 nl (i - 1)); (if i = length nl - 1 then () else extract_nodelist_contained h0 nl (i + 1)); lemma_unsnoc_is_last nl #set-options "--z3rlimit 10 --initial_ifuel 2" let lemma_dll_links_disjoint (#t:Type) (h0:heap) (d:dll t) (i:nat) : Lemma (requires ( (dll_valid h0 d) /\ (i < length d.nodes))) (ensures ( let nodes = d.nodes in let left = (nodes.[i]@h0).blink in let right = (nodes.[i]@h0).flink in Mod.loc_disjoint (Mod.loc_buffer left) (Mod.loc_buffer right))) = let nl = reveal d.nodes in match nl with | [_] -> () | _ -> lemma_unsnoc_length nl; let node_split = splitAt i nl in lemma_splitAt nl (fst node_split) (snd node_split) i; lemma_splitAt_index_hd i nl; let l1, x :: l2 = node_split in (if i = 0 then () else extract_nodelist_conn h0 nl (i-1)); (if i = length nl - 1 then () else extract_nodelist_conn h0 nl i); (if i = 0 then () else ( if i = length nl - 1 then (lemma_unsnoc_is_last nl) else ( lemma_unsnoc_is_last l1; let left = last l1 in let right = hd l2 in lemma_splitAt_reindex_left i nl (length l1 - 1); // assert (left == (nl.[i]@h0).blink); lemma_splitAt_reindex_right i nl 1; // assert (right == (nl.[i]@h0).flink); nodelist_split_aa l1 (x :: l2); // assert (Mod.loc_disjoint (nodelist_fp0 l1) (nodelist_fp0 l2)); assert (Mod.loc_includes (nodelist_fp0 l1) (Mod.loc_buffer left)); // OBSERVE assert (Mod.loc_includes (nodelist_fp0 l2) (Mod.loc_buffer right)); // OBSERVE () ))) #reset-options /// When something unrelated to a XYZ is changed, the XYZ itself shall /// remain valid let rec nodelist_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (nl:nodelist t) : Lemma (requires ( (nodelist_valid h0 nl) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (nodelist_fp0 nl)))) (ensures (nodelist_valid h1 nl)) = match nl with | [] -> () | _ -> nodelist_remains_valid h0 h1 loc (tl nl) let piece_remains_valid (#t:Type) (h0 h1:heap) (loc:Mod.loc) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies loc h0 h1) /\ (Mod.loc_disjoint loc (piece_fp0 p)))) (ensures (piece_valid h1 p)) = nodelist_remains_valid h0 h1 loc p.pnodes /// When outward facing pointers of ends of pieces are modified, they /// still remain valid #set-options "--z3rlimit 20" let piece_remains_valid_b (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.phead) h0 h1) /\ (h1 `contains` p.phead) /\ (p.phead@h0).flink == (p.phead@h1).flink)) (ensures (piece_valid h1 p) /\ (p.ptail@h0).flink == (p.ptail@h1).flink) = let nodes = p.pnodes in if length nodes > 1 then ( nodelist_includes_r_fp0 nodes 1 (length nodes - 1); lemma_unsnoc_is_last nodes; // assert (p.ptail == nodes.[length nodes - 1]); // assert (p.ptail@h0 == p.ptail@h1); // assert (h1 `contains` p.ptail); // assert (Mod.loc_disjoint (Mod.loc_buffer p.phead) (nodelist_fp0 (tl nodes))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.phead) (tl nodes) ) else () let piece_remains_valid_f (#t:Type) (h0 h1:heap) (p:piece t) : Lemma (requires ( (piece_valid h0 p) /\ (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1) /\ (h1 `contains` p.ptail) /\ (p.ptail@h0).blink == (p.ptail@h1).blink)) (ensures (piece_valid h1 p) /\ (p.phead@h0).blink == (p.phead@h1).blink) = let nodes = reveal p.pnodes in if length nodes > 1 then ( fst_unsnoc_nodelist_valid h0 nodes; // assert (nodelist_valid h0 (fst (unsnoc nodes))); lemma_unsnoc_is_last nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer p.ptail) (nodelist_fp0 (fst (unsnoc nodes)))); nodelist_remains_valid h0 h1 (Mod.loc_buffer p.ptail) (fst (unsnoc nodes)); // assert (nodelist_contained h1 (fst (unsnoc nodes))); // assert (h1 `contains` (snd (unsnoc nodes))); nodelist_append_contained h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_contained h1 (reveal p.pnodes)); // assert (piece_contained h1 p); extract_nodelist_conn h0 nodes (length nodes - 2); // let nl1 = fst (unsnoc nodes) in lemma_unsnoc_is_last (fst (unsnoc nodes)); // assert (last nl1 == nl1.[length nl1 - 1]); // assert (last nl1 == nl1.[length nodes - 2]); lemma_unsnoc_index nodes (length nodes - 2); // assert (last nl1 == nodes.[length nodes - 2]); // assert ((last (fst (unsnoc nodes)))@h0 |>> (hd [snd (unsnoc nodes)])); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); // assert (Mod.modifies (Mod.loc_buffer p.ptail) h0 h1); extract_nodelist_contained h0 nodes (length nodes - 2); // assert (h0 `contains` last (fst (unsnoc nodes))); // assert (Mod.loc_disjoint (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer p.ptail)); assert (Mod.loc_includes (nodelist_fp0 (fst (unsnoc nodes))) (Mod.loc_buffer (last (fst (unsnoc nodes))))); // OBSERVE // assert (Mod.loc_disjoint (Mod.loc_buffer (last (fst (unsnoc nodes)))) (Mod.loc_buffer p.ptail)); lemma_snoc_length (unsnoc nodes); // assert ((last (fst (unsnoc nodes)))@h0 == (last (fst (unsnoc nodes)))@h1); // assert ((last (fst (unsnoc nodes)))@h1 |>> (hd [snd (unsnoc nodes)])); // assert ((last (fst (unsnoc nodes))) <<| (hd [snd (unsnoc nodes)])@h1); nodelist_append_conn h1 (fst (unsnoc nodes)) [snd (unsnoc nodes)]; // assert (nodelist_conn h1 (reveal p.pnodes)); // assert (piece_conn h1 p); // assert ((p.phead@h0).blink == (p.phead@h1).blink); () ) else () #reset-options /// Testing is a node is within a dll or not let node_not_in_dll (#t:Type) (h0:heap) (n:pointer (node t)) (d:dll t) = let m1 = Mod.loc_buffer n in let m2 = dll_fp0 d in Mod.loc_disjoint m1 m2 /// An empty dll has no nodes let _auto_empty_dll (#t:Type) (h0:heap) (d:dll t) : Lemma (requires (dll_valid h0 d /\ (d.lhead == null \/ d.ltail == null))) (ensures (reveal d.nodes == [])) [SMTPat (dll_valid h0 d); SMTPat (reveal d.nodes)] = () /// Be able to easily reason about unchanged payloads let rec aux_unchanged_payload #t h0 h1 n0 (nl:nodelist t) : Lemma (requires (Mod.modifies (Mod.loc_buffer n0) h0 h1 /\ (n0@h0).p == (n0@h1).p /\ (nodelist_aa_r nl) /\ (n0 `memP` nl \/ Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)) )) (ensures (unchanged_node_vals h0 h1 nl)) (decreases (length nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload h0 h1 n0 nl'; assert (n0 `memP` nl ==> (n == n0 \/ n0 `memP` nl')); let goal () = unchanged_node_val h0 h1 n in FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n0 `memP` nl}) -> FStar.Classical.or_elim #_ #_ #goal (fun (_:unit{n == n0}) -> ()) (fun (_:unit{n0 `memP` nl'}) -> let i = nl' `index_of` n0 in extract_nodelist_fp0 nl' i)) (fun (_:unit{Mod.loc_disjoint (Mod.loc_buffer n0) (nodelist_fp0 nl)}) -> ()) let rec aux_unchanged_payload_nomod #t h0 h1 (nl:nodelist t) : Lemma (requires (Mod.modifies Mod.loc_none h0 h1)) (ensures (unchanged_node_vals h0 h1 nl)) = match nl with | [] -> () | n :: nl' -> aux_unchanged_payload_nomod h0 h1 nl' let rec aux_unchanged_payload_transitive #t h0 h1 h2 (nl:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl /\ unchanged_node_vals h1 h2 nl)) (ensures (unchanged_node_vals h0 h2 nl)) = match nl with | [] -> () | _ :: nl' -> aux_unchanged_payload_transitive h0 h1 h2 nl' let rec aux_unchanged_payload_append #t h0 h1 (nl1 nl2:nodelist t) : Lemma (requires (unchanged_node_vals h0 h1 nl1 /\ unchanged_node_vals h0 h1 nl2)) (ensures (unchanged_node_vals h0 h1 (nl1 `append` nl2))) = match nl1 with | [] -> () | n :: nl' -> aux_unchanged_payload_append h0 h1 nl' nl2 /// Now for the actual ST operations that will be exposed :) #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_head (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.lhead)) h0 h1 /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == n :: reveal d.nodes)) = if is_null d.lhead then ( singleton_dll n ) else ( let h = d.lhead in // let h0 = ST.get () in !<|= n; n =|> h; let h0' = ST.get () in n <|= h; let h1 = ST.get () in // aux_unchanged_payload h0 h0' n d.nodes; aux_unchanged_payload h0' h1 h d.nodes; aux_unchanged_payload_transitive h0 h0' h1 d.nodes; // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p p1 in // assert (fragment_valid h1 [p]); // assert (fragment_ghostly_connections f); // assert (length f = 1); // assert (h1 `contains` (hd f).phead); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0' (hd f)); piece_remains_valid_b h0' h1 p1; // assert (h1 `contains` (hd f).ptail); // assert (nodelist_contained h1 (reveal (hd f).pnodes)); // assert (piece_contained h1 (hd f)); // assert (fragment_contained h1 f); // assert (fragment_aa f); // assert (nodelist_conn h1 (reveal (f.[0]).pnodes)); // assert (fragment_conn h1 f); // assert (fragment_valid h1 f); // assert (fragment_valid h1 f'); // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // assert (is_null ((last f').ptail@h0).flink); // assert (is_null ((last f').ptail@h0').flink); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); y ) #reset-options #set-options "--z3rlimit 500 --max_fuel 2 --max_ifuel 1" let dll_insert_at_tail (#t:Type) (d:dll t) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == snoc (reveal d.nodes, n))) = if is_null d.lhead then ( singleton_dll n ) else ( let t = d.ltail in // let h0 = ST.get () in !=|> n; t <|= n; let h0' = ST.get () in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; // assert (Mod.loc_disjoint (Mod.loc_buffer (t@h0).blink) (Mod.loc_buffer n)); t =|> n; let h1 = ST.get () in // let Frag1 p1 = tot_dll_to_fragment h0 d in let p = tot_node_to_piece h0 n in let f' = Frag2 p1 p in piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; piece_remains_valid_f h0' h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last y.nodes; lemma_snoc_unsnoc (reveal d.nodes, n); lemma_unsnoc_index y.nodes (length (y.nodes) - 2); lemma_unsnoc_length y.nodes; aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h1 t y.nodes; aux_unchanged_payload_transitive h0 h0' h1 y.nodes; y ) #reset-options let _l_insert_after (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, x1 :: l2 = lemma_split_using l x0; split_using l x0 in assert (x0 == x1); l1 `append` (x0 :: (x :: l2)) #set-options "--z3rlimit 1000 --initial_fuel 2 --initial_ifuel 1" let dll_insert_after (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) (Mod.loc_union (Mod.loc_buffer e) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_after e d.nodes n)) = let h0 = ST.get () in // assert (length d.nodes > 0); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in if is_null e2 then ( let y = dll_insert_at_tail d n in assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) else ( extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_unsnoc_is_last d.nodes; extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e + 1); if not (is_null e1) then ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e - 1) ) else (); e <|= n; // let h' = ST.get () in assert (h' `contains` e2); assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e2)); n =|> e2; let h0' = ST.get () in // assert (is_not_null e1 ==> e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (is_not_null e1 ==> Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (Mod.loc_buffer e1)); // assert (is_not_null e1 ==> Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (Mod.loc_buffer e1)); Mod.modifies_buffer_elim e1 (Mod.loc_buffer n) h0 h0'; e =|> n; let h0'' = ST.get () in // assert (h0 `contains` e2); // assert (h0' `contains` e2); // assert (e2 == (reveal d.nodes).[reveal d.nodes `index_of` e + 1]); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e); lemma_split3_r_hd d.nodes (d.nodes `index_of` e); lemma_split3_append d.nodes (d.nodes `index_of` e); lemma_split3_index d.nodes (d.nodes `index_of` e); lemma_split3_length d.nodes (d.nodes `index_of` e); // assert (Mod.loc_includes (nodelist_fp0 (reveal d.nodes)) (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z))); // assert (Mod.loc_includes (nodelist_fp0 (let _,_,z = split3 (reveal d.nodes) (reveal d.nodes `index_of` e) in z)) (Mod.loc_buffer e2)); // assert (Mod.loc_disjoint (Mod.loc_buffer e2) (Mod.loc_buffer e)); // assert (Mod.modifies (Mod.loc_buffer e) h0' h0''); Mod.modifies_buffer_elim e2 (Mod.loc_buffer e) h0' h0''; // assert (h0'' `contains` e2); n <|= e2; let h1 = ST.get () in // // assert (e `memP` reveal d.nodes); // assert (e2 `memP` reveal d.nodes); // assert (e@h0 |>> e2 /\ e <<| e2@h0); let f = tot_dll_to_fragment_split h0 d e e2 in // assert (length f = 2); let Frag2 p1 p3 = f in // assert ([p1 ; p3] == f); let p2 = tot_node_to_piece h0 n in let f' = Frag3 p1 p2 p3 in // assert (Mod.modifies (Mod.loc_buffer n) h0 h0'); // assert (piece_valid h0 p1); // assert (loc_equiv (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (dll_fp0 d)); // assert (Mod.loc_includes (dll_fp0 d) (fragment_fp0 f)); // assert (Mod.loc_includes (fragment_fp0 f) (piece_fp0 p1)); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p1); // assert (Mod.loc_includes (dll_fp0 d) (piece_fp0 p1)); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p1)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p1; // assert (piece_valid h0 p3); Mod.loc_includes_trans (dll_fp0 d) (fragment_fp0 f) (piece_fp0 p3); // assert (Mod.loc_disjoint (Mod.loc_buffer n) (piece_fp0 p3)); piece_remains_valid h0 h0' (Mod.loc_buffer n) p3; piece_remains_valid_f h0' h0'' p1; // assert (Mod.loc_disjoint (piece_fp0 p1) (piece_fp0 p3)); piece_remains_valid h0' h0'' (piece_fp0 p1) p3; piece_remains_valid h0'' h1 (piece_fp0 p3) p1; piece_remains_valid_b h0'' h1 p3; // assert ([p2 ; p3] == append [p2] [p3]); // assert (f' == append [p1] [p2 ; p3]); // // assert (fragment_valid h1 f'); assert (fragment_defragmentable h1 (Frag2 p2 p3)); // OBSERVE // assert (fragment_defragmentable h1 f'); // assert (length f' > 0); // assert (is_null ((hd f').phead@h1).blink); // lemma_unsnoc_is_last f'; // assert (last f' == p3); // assert (is_null ((last f').ptail@h1).flink); let y = tot_defragmentable_fragment_to_dll h1 f' in assume (n `memP` y.nodes); assume (e `memP` y.nodes); assume (e2 `memP` y.nodes); aux_unchanged_payload h0 h0' n y.nodes; aux_unchanged_payload h0' h0'' e y.nodes; aux_unchanged_payload h0'' h1 e2 y.nodes; aux_unchanged_payload_transitive h0 h0' h0'' y.nodes; aux_unchanged_payload_transitive h0 h0'' h1 y.nodes; assume (reveal y.nodes == _l_insert_after e (reveal d.nodes) n); y ) #reset-options let _l_insert_before (x0:'a) (l:list 'a{x0 `memP` l}) (x:'a) : GTot (list 'a) = let l1, l2 = split_using l x0 in l1 `append` (x :: l2) #set-options "--z3rlimit 50" let dll_insert_before (#t:Type) (d:dll t) (e:pointer (node t)) (n:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes) /\ (h0 `contains` n) /\ (node_not_in_dll h0 n d))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d.lhead) (Mod.loc_union (Mod.loc_union (Mod.loc_buffer n) (Mod.loc_buffer d.ltail)) // this is needed due to using "after" // TODO: Figure out a way to remove it (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer e)))) h0 h1 /\ (dll_fp0 y `loc_equiv` B.loc_union (dll_fp0 d) (Mod.loc_buffer n)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == _l_insert_before e d.nodes n)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( let y = dll_insert_at_head d n in assume (reveal y.nodes == _l_insert_before e d.nodes n); y ) else ( extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); let y = dll_insert_after d e1 n in assume (reveal y.nodes == _l_insert_before e (reveal d.nodes) n); y ) #reset-options unfold let _aux_fp_split_by_node (d0 d1:dll 'a) (n:pointer (node 'a)) = dll_fp0 d0 `loc_equiv` B.loc_union (dll_fp0 d1) (Mod.loc_buffer n) /\ dll_fp0 d1 `B.loc_disjoint` Mod.loc_buffer n #set-options "--z3rlimit 20" let dll_remove_head (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.lhead@h0).flink) h0 h1 /\ _aux_fp_split_by_node d y d.lhead /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == tl d.nodes)) = let h0 = ST.get () in let e = d.lhead in let e2 = (!*e).flink in if is_null e2 then ( empty_list ) else ( !<|= e2; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e e2 in let Frag2 p1 p2 = f in // assert (p1.phead == e); // assert (p1.ptail == e); let f' = Frag1 p2 in piece_remains_valid_b h0 h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e2 d.nodes; y ) #reset-options let rec _lemma_only_head_can_point_left_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).blink == null /\ nodelist_conn h0 l)) (ensures (e == hd l)) = match l with | [_] -> () | _ -> FStar.Classical.or_elim #(e == hd l) #(e =!= hd l) #(fun () -> e == hd l) (fun _ -> ()) (fun _ -> _lemma_only_head_can_point_left_to_null h0 e (tl l); extract_nodelist_conn h0 l 0) let rec _lemma_only_tail_can_point_right_to_null (#t:Type) (h0:heap) (e:pointer (node t)) (l:nodelist t) : Lemma (requires (e `memP` l /\ (e@h0).flink == null /\ nodelist_conn h0 l)) (ensures (e == last l)) = match l with | [_] -> () | _ -> _lemma_only_tail_can_point_right_to_null h0 e (tl l) let rec _lemma_all_nodes_are_unique (#t:Type) (h0:heap) (l:nodelist t) (i j:nat) : Lemma (requires ( (nodelist_conn h0 l) /\ (i < length l) /\ (j < length l) /\ (((hd l)@h0).blink == null) /\ (index l i == index l j))) (ensures (i = j)) = match i, j with | 0, 0 -> () | 0, _ -> extract_nodelist_conn h0 l (j - 1) | _, 0 -> extract_nodelist_conn h0 l (i - 1) | _ -> extract_nodelist_conn h0 l (i - 1); extract_nodelist_conn h0 l (j - 1); _lemma_all_nodes_are_unique h0 l (i - 1) (j - 1) #set-options "--z3rlimit 50" let dll_remove_tail (#t:Type) (d:dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (length d.nodes > 0))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_buffer (d.ltail@h0).blink) h0 h1 /\ _aux_fp_split_by_node d y d.ltail /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == fst (unsnoc (reveal d.nodes)))) = let h0 = ST.get () in let e = d.ltail in let e1 = (!*e).blink in lemma_dll_links_contained h0 d (length d.nodes - 1); lemma_unsnoc_is_last d.nodes; if is_null e1 then ( _lemma_only_head_can_point_left_to_null h0 e d.nodes; empty_list ) else ( extract_nodelist_contained h0 d.nodes (length (d.nodes) - 2); extract_nodelist_conn h0 d.nodes (length (d.nodes) - 2); // assert (e == (reveal d.nodes).[length (reveal d.nodes) - 1]); // assert (e1 == (reveal d.nodes).[length (reveal d.nodes) - 2]); !=|> e1; let h1 = ST.get () in let f = tot_dll_to_fragment_split h0 d e1 e in let Frag2 p1 p2 = f in lemma_snoc_length (reveal p1.pnodes, e); // assert (reveal p1.pnodes == fst (unsnoc (reveal d.nodes))); let f' = Frag1 p1 in piece_remains_valid_f h0 h1 p1; let y = tot_defragmentable_fragment_to_dll h1 f' in aux_unchanged_payload h0 h1 e1 (d.nodes); // assert (reveal y.nodes == reveal p1.pnodes); y ) #reset-options let _l_remove_mid (l:list 'a{length l > 0}) (x:'a {x `memP` l}) : GTot (list 'a) = let l1, x0 :: l2 = lemma_split_using l x; split_using l x in assert (x == x0); l1 `append` l2 #set-options "--z3rlimit 400 --initial_fuel 2 --initial_ifuel 2" let dll_remove_node (#t:Type) (d:dll t) (e:pointer (node t)) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d) /\ (e `memP` d.nodes))) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_union (Mod.loc_buffer (d.lhead@h0).flink) (Mod.loc_buffer (d.ltail@h0).blink)) (Mod.loc_union (Mod.loc_buffer (e@h0).blink) (Mod.loc_buffer (e@h0).flink))) h0 h1 /\ _aux_fp_split_by_node d y e /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 d.nodes /\ reveal y.nodes == _l_remove_mid d.nodes e)) = let h0 = ST.get () in extract_nodelist_contained h0 d.nodes (d.nodes `index_of` e); let e1 = (!*e).blink in let e2 = (!*e).flink in lemma_dll_links_contained h0 d (d.nodes `index_of` e); if is_null e1 then ( _lemma_only_head_can_point_left_to_null h0 e d.nodes; dll_remove_head d ) else if is_null e2 then ( _lemma_only_tail_can_point_right_to_null h0 e d.nodes; let y = dll_remove_tail d in let h1 = ST.get () in // assume (unchanged_node_vals h0 h1 (reveal d.nodes)); assume (reveal y.nodes == _l_remove_mid d.nodes e); y ) else ( admit (); lemma_dll_links_contained h0 d (d.nodes `index_of` e); extract_nodelist_conn h0 d.nodes (d.nodes `index_of` e - 1); extract_nodelist_aa_r d.nodes (d.nodes `index_of` e - 1); extract_nodelist_fp0 d.nodes (d.nodes `index_of` e); lemma_dll_links_disjoint h0 d (d.nodes `index_of` e); e1 =|> e2; let h0' = ST.get () in e1 <|= e2; let h1 = ST.get () in // assert (e1 == (reveal d.nodes).[reveal d.nodes `index_of` e - 1]); // assert (e1 `memP` reveal d.nodes); // assert (e1@h0 |>> e); let f = tot_dll_to_fragment_split h0 d e1 e in let Frag2 p1 p2 = f in let p2' = tot_piece_tail h0 p2 e2 in let f' = Frag2 p1 p2' in piece_remains_valid_f h0 h0' p1; piece_remains_valid h0' h1 (Mod.loc_buffer e2) p1; piece_remains_valid h0 h0' (Mod.loc_buffer e1) p2'; piece_remains_valid_b h0' h1 p2'; let y = tot_defragmentable_fragment_to_dll h1 f' in // assert (dll_valid h1 y); assume (_aux_fp_split_by_node d y e); assume (unchanged_node_vals h0 h1 d.nodes); assume (reveal y.nodes == _l_remove_mid d.nodes e); y ) #reset-options #set-options "--z3rlimit 10 --max_fuel 2 --max_ifuel 1"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.ModifiesPat.fst.checked", "LowStar.Modifies.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.Properties.fst.checked", "FStar.List.Pure.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "DoublyLinkedList.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "LowStar.Modifies", "short_module": "Mod" }, { "abbrev": false, "full_module": "LowStar.BufferOps", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "LowStar.ModifiesPat", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d1: DoublyLinkedList.dll t -> d2: DoublyLinkedList.dll t -> FStar.HyperStack.ST.StackInline (DoublyLinkedList.dll t)
FStar.HyperStack.ST.StackInline
[]
[]
[ "DoublyLinkedList.dll", "Prims.unit", "DoublyLinkedList.aux_unchanged_payload_nomod", "FStar.Ghost.reveal", "DoublyLinkedList.nodelist", "DoublyLinkedList.__proj__Mkdll__item__nodes", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Prims.bool", "FStar.List.Tot.Properties.append_l_nil", "LowStar.Buffer.pointer", "DoublyLinkedList.node", "DoublyLinkedList.aux_unchanged_payload_append", "DoublyLinkedList.aux_unchanged_payload_transitive", "DoublyLinkedList.aux_unchanged_payload", "FStar.List.Tot.Properties.lemma_unsnoc_is_last", "Prims.l_and", "DoublyLinkedList.dll_valid", "DoublyLinkedList.loc_equiv", "DoublyLinkedList.dll_fp0", "DoublyLinkedList.fragment_fp0", "Prims.eq2", "Prims.list", "Prims.Nil", "DoublyLinkedList.piece", "DoublyLinkedList.__proj__Mkpiece__item__pnodes", "FStar.List.Tot.Base.append", "DoublyLinkedList.tot_defragmentable_fragment_to_dll", "DoublyLinkedList.piece_remains_valid_b", "DoublyLinkedList.piece_remains_valid", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "DoublyLinkedList.piece_remains_valid_f", "DoublyLinkedList.fragment", "DoublyLinkedList.Frag2", "DoublyLinkedList.piece_valid", "DoublyLinkedList.tot_dll_to_piece", "DoublyLinkedList.op_Less_Bar_Equals", "DoublyLinkedList.op_Equals_Bar_Greater", "LowStar.Buffer.pointer_or_null", "DoublyLinkedList.__proj__Mkdll__item__lhead", "DoublyLinkedList.__proj__Mkdll__item__ltail", "LowStar.Monotonic.Buffer.is_null", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_union", "DoublyLinkedList.unchanged_node_vals" ]
[]
false
true
false
false
false
let dll_append (#t: Type) (d1 d2: dll t) : StackInline (dll t) (requires (fun h0 -> (dll_valid h0 d1) /\ (dll_valid h0 d2) /\ ((dll_fp0 d1) `Mod.loc_disjoint` (dll_fp0 d2)) )) (ensures (fun h0 y h1 -> Mod.modifies (Mod.loc_union (Mod.loc_buffer d1.ltail) (Mod.loc_buffer d2.lhead)) h0 h1 /\ (dll_fp0 y) `loc_equiv` ((dll_fp0 d1) `B.loc_union` (dll_fp0 d2)) /\ dll_valid h1 y /\ unchanged_node_vals h0 h1 y.nodes /\ reveal y.nodes == d1.nodes `append` d2.nodes)) =
let h0 = ST.get () in if is_null d1.lhead then (let y = d2 in let h1 = ST.get () in aux_unchanged_payload_nomod h0 h1 y.nodes; y) else (if is_null d2.lhead then (let y = d1 in let h1 = ST.get () in aux_unchanged_payload_nomod h0 h1 y.nodes; append_l_nil y.nodes; y) else (let n1 = d1.ltail in let n2 = d2.lhead in n1 =|> n2; let h0' = ST.get () in n1 <|= n2; let h1 = ST.get () in let p1 = tot_dll_to_piece h0 d1 in let p2 = tot_dll_to_piece h0 d2 in let f' = Frag2 p1 p2 in piece_remains_valid_f h0 h0' p1; piece_remains_valid h0 h0' (Mod.loc_buffer n1) p2; piece_remains_valid h0' h1 (Mod.loc_buffer n2) p1; piece_remains_valid_b h0' h1 p2; let y = tot_defragmentable_fragment_to_dll h1 f' in lemma_unsnoc_is_last d1.nodes; aux_unchanged_payload h0 h0' n1 d1.nodes; aux_unchanged_payload h0 h0' n1 d2.nodes; aux_unchanged_payload h0' h1 n2 d1.nodes; aux_unchanged_payload h0' h1 n2 d2.nodes; aux_unchanged_payload_transitive h0 h0' h1 d1.nodes; aux_unchanged_payload_transitive h0 h0' h1 d2.nodes; aux_unchanged_payload_append h0 h1 d1.nodes d2.nodes; y))
false
Hacl.Spec.Bignum.Definitions.fst
Hacl.Spec.Bignum.Definitions.blocks
val blocks: x:size_pos -> m:size_pos -> Tot (r:size_pos{x <= m * r})
val blocks: x:size_pos -> m:size_pos -> Tot (r:size_pos{x <= m * r})
let blocks x m = (x - 1) / m + 1
{ "file_name": "code/bignum/Hacl.Spec.Bignum.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 32, "end_line": 11, "start_col": 0, "start_line": 11 }
module Hacl.Spec.Bignum.Definitions open FStar.Mul open Lib.IntTypes open Lib.Sequence #set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked" ], "interface_file": false, "source_file": "Hacl.Spec.Bignum.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.Sequence", "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": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.Bignum", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
x: Lib.IntTypes.size_pos -> m: Lib.IntTypes.size_pos -> r: Lib.IntTypes.size_pos{x <= m * r}
Prims.Tot
[ "total" ]
[]
[ "Lib.IntTypes.size_pos", "Prims.op_Addition", "Prims.op_Division", "Prims.op_Subtraction", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Mul.op_Star" ]
[]
false
false
false
false
false
let blocks x m =
(x - 1) / m + 1
false