diff --git a/Backend/Clos/Convert.lean b/Backend/Clos/Convert.lean index 14e2cc7c9..1e15cac40 100644 --- a/Backend/Clos/Convert.lean +++ b/Backend/Clos/Convert.lean @@ -89,6 +89,9 @@ def Op.ofPrim? : Prim → Option Op | .free => some .free | .load => some .load | .host op => some (.host op) + | .tab => some .tab + | .idx => some .idx + | .drop => some .drop | .store => none | .lit _ => none diff --git a/Backend/Clos/Interp.lean b/Backend/Clos/Interp.lean index 85fbfb1e7..0f560677a 100644 --- a/Backend/Clos/Interp.lean +++ b/Backend/Clos/Interp.lean @@ -1,5 +1,6 @@ import Std.Data.HashMap import Support.Dynamics.Host +import Support.Dynamics.Arrays import Backend.Value import Backend.Clos.Syntax @@ -7,7 +8,8 @@ import Backend.Clos.Syntax # Backend — closure-IR interpreter A fuelled interpreter for the closure IR over an explicit heap of objects (pairs, injections, -closures, boxed `i64`s, cells), counting live objects. It checks the ownership discipline as it +closures, boxed `i64`s, cells), counting live objects. An array is consecutive cells, as in the +source machine (SPEC §9). It checks the ownership discipline as it runs: using a variable consumes it, a consumed variable cannot be used again, and a call or `main` must end with every variable of its frame consumed. Host operations go through `Host.call` at `wasmHost`, as in the source interpreter. @@ -131,6 +133,18 @@ def cellOf (v : CV) : IM (Nat × CV) := do def objFuel : Nat := 1 <<< 20 +/-- Take a pair apart, freeing it. -/ +def unpair (v : CV) : IM (CV × CV) := do + match v with + | .ptr a => match ← getObj a with + | .pair x y => do freeObj a; pure (x, y) + | _ => throw "closure IR: not a pair" + | _ => throw "closure IR: not a pair" + +/-- An array of `n` elements is `max n 1` consecutive cells (SPEC §9); the handle is + `(base, i32[n])`. The extra cell of an empty array keeps the base an object. -/ +def arrSlots (n : Nat) : Nat := max n 1 + def applyOp : Op → List CV → IM CV | .alloc, [v] => allocObj (.cell v) | .free, [l] => do let (a, c) ← cellOf l; freeObj a; pure c @@ -146,8 +160,58 @@ def applyOp : Op → List CV → IM CV match wasmHost.call op sv with | some r => ofSource r.1 | none => throw "closure IR: host operation stuck" + | .idx, [v] => do + let (arr, i) ← unpair v + let (base, n) ← unpair arr + match base, n, i with + | .ptr a, .i32 n, .i32 i => + if i < n then allocObj (.inj false (.ptr (a + i.toNat))) + else allocObj (.inj true .unit) + | _, _, _ => throw "closure IR: idx of a non-array" | _, _ => throw "closure IR: primitive arity" +mutual +/-- Call the closure `f` on `a`; the call frees `f`'s environment record. -/ +def callClos (codes : Array Code) : Nat → CV → CV → IM CV + | 0, _, _ => throw "closure IR: out of fuel" + | n + 1, vf, va => do + match vf with + | .ptr p => + match ← getObj p with + | .clos c vs => do + freeObj p + let some code := codes[c]? | throw "closure IR: bad code index" + if code.env.length != vs.length then throw "closure IR: environment size" + let frame := (code.param, va) :: code.env.zip vs + let (r, rest) ← eval codes n frame code.body + if !rest.isEmpty then + throw s!"closure IR: code {c} leaves {rest.map (·.1)} unconsumed" + pure r + | _ => throw "closure IR: call of a non-closure" + | _ => throw "closure IR: call of a non-closure" + +/-- `tab`'s fill: cell `a + i` gets `f i`, for `i < k`, each call on a copy of `f`. -/ +def tabFill (codes : Array Code) : Nat → CV → Nat → Nat → Nat → IM Unit + | 0, _, _, _, _ => throw "closure IR: out of fuel" + | _ + 1, _, _, _, 0 => pure () + | n + 1, f, a, i, k + 1 => do + let fc ← deepCopy objFuel f + let r ← callClos codes n fc (.i32 (UInt32.ofNat i)) + setObj (a + i) (.cell r) + tabFill codes n f a (i + 1) k + +/-- `drop`'s drain: cell `a + i` is freed and its contents handed to a copy of `f`. -/ +def dropFill (codes : Array Code) : Nat → CV → Nat → Nat → Nat → IM Unit + | 0, _, _, _, _ => throw "closure IR: out of fuel" + | _ + 1, _, _, _, 0 => pure () + | n + 1, f, a, i, k + 1 => do + let (_, c) ← cellOf (.ptr (a + i)) + freeObj (a + i) + let fc ← deepCopy objFuel f + let r ← callClos codes n fc c + deepDrop objFuel r + dropFill codes n f a (i + 1) k + /-- Evaluate `t` in the frame `env`: its value and what is left of the frame. -/ def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env) | 0, _, _ => throw "closure IR: out of fuel" @@ -167,26 +231,33 @@ def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env) | .clos c xs => do let (vs, env) ← takeAll env xs pure (← allocObj (.clos c vs), env) + | .prim .tab [x] => do + let (v, env) ← take env x + let (c, f) ← unpair v + let .i32 c := c | throw "closure IR: tab of a non-length" + let len := (arrLen c).toNat + let base := (← get).next + for _ in List.range (arrSlots len) do discard (allocObj (.cell .unit)) + tabFill codes n f base 0 len + deepDrop objFuel f + pure (← allocObj (.pair (.ptr base) (.i32 (arrLen c))), env) + | .prim .drop [x] => do + let (v, env) ← take env x + let (f, arr) ← unpair v + let (base, len) ← unpair arr + let .ptr a := base | throw "closure IR: drop of a non-array" + let .i32 len := len | throw "closure IR: drop of a non-array" + dropFill codes n f a 0 len.toNat + deepDrop objFuel f + if len.toNat = 0 then freeObj a + pure (.unit, env) | .prim op xs => do let (vs, env) ← takeAll env xs pure (← applyOp op vs, env) | .app f a => do let (vf, env) ← take env f let (va, env) ← take env a - match vf with - | .ptr p => - match ← getObj p with - | .clos c vs => do - freeObj p - let some code := codes[c]? | throw "closure IR: bad code index" - if code.env.length != vs.length then throw "closure IR: environment size" - let frame := (code.param, va) :: code.env.zip vs - let (r, rest) ← eval codes n frame code.body - if !rest.isEmpty then - throw s!"closure IR: code {c} leaves {rest.map (·.1)} unconsumed" - pure (r, env) - | _ => throw "closure IR: call of a non-closure" - | _ => throw "closure IR: call of a non-closure" + pure (← callClos codes n vf va, env) | .letv x e b => do let (v, env) ← eval codes n env e eval codes n ((x, v) :: env) b @@ -215,6 +286,7 @@ def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env) let (v, env) ← take env x deepDrop objFuel v eval codes n env b +end /-- The observable form of a word. -/ def observe : Nat → CV → IM PV diff --git a/Backend/Clos/Syntax.lean b/Backend/Clos/Syntax.lean index 495e4282d..d2929fc7d 100644 --- a/Backend/Clos/Syntax.lean +++ b/Backend/Clos/Syntax.lean @@ -23,7 +23,7 @@ open BoCa abbrev Var := Nat -/-- The primitives of `[TR]` §1 and §8, saturated. -/ +/-- The primitives of `[TR]` §1, §8 and §9, saturated. -/ inductive Op where /-- `alloc v`: a fresh cell holding `v`. -/ | alloc @@ -35,6 +35,12 @@ inductive Op where | store /-- A host operation of §8, applied to its (consumed) argument. -/ | host (op : HostOp) + /-- `tab (n, f)`: an array of `len(n)` slots, slot `i` holding `f i` (§9). -/ + | tab + /-- `idx ((ℓ, n), i)`: `inl` of the `i`-th slot's location when `i < n`, else `inr ()` (§9). -/ + | idx + /-- `drop (f, (ℓ, n))`: each slot's contents to `f`, in order, then the array freed (§9). -/ + | drop deriving Repr, DecidableEq inductive Tm where diff --git a/Backend/Flat/Lower.lean b/Backend/Flat/Lower.lean index ad0b240f9..8c1c7e976 100644 --- a/Backend/Flat/Lower.lean +++ b/Backend/Flat/Lower.lean @@ -78,6 +78,9 @@ def lowerTm (hostIdx : HostOp → Nat) : Tm → Dest → LM (List Stmt) return [.eval (.call fDrop [.load l 1]), .store l 1 (← lv v)] ++ emit d (.c64 unitW) | .prim (.host op) [v], d => do return emit d (.call (hostIdx op) [← lv v]) + | .prim .tab [v], d => do return emit d (.call fArrTab [← lv v]) + | .prim .idx [v], d => do return emit d (.call fArrIdx [← lv v]) + | .prim .drop [v], d => do return emit d (.call fArrDrop [← lv v]) | .prim _ _, _ => throw "flat IR: primitive arity" | .app f a, d => do let f ← lv f diff --git a/Backend/Flat/Runtime.lean b/Backend/Flat/Runtime.lean index 0ef3492d6..c4c998f72 100644 --- a/Backend/Flat/Runtime.lean +++ b/Backend/Flat/Runtime.lean @@ -3,8 +3,8 @@ import Backend.Flat.Syntax /-! # Backend — the flat IR's runtime functions -Deep copy, deep drop and the printer, as flat-IR functions over the representation of -`Backend/Flat/Syntax.lean`. Cells are shared by a copy and left alone by a drop: a location is +Deep copy, deep drop, the printer and the array operations, as flat-IR functions over the +representation of `Backend/Flat/Syntax.lean`. Cells are shared by a copy and left alone by a drop: a location is copied as a word, and cells are freed only by `free`. The printer writes the text of `Backend.PV.render`. @@ -20,6 +20,12 @@ def fDropEnv : Nat := 3 def fPrint : Nat := 4 def fPrintAtom : Nat := 5 def fPrintDec : Nat := 6 +def fArrTab : Nat := 7 +def fArrInit : Nat := 8 +def fArrTabLoop : Nat := 9 +def fArrIdx : Nat := 10 +def fArrDrop : Nat := 11 +def fArrDropLoop : Nat := 12 /-- `copy w`: locals `w`, `h`, `t`, `n`. -/ def copyFn : Fn where @@ -141,7 +147,109 @@ def printDecFn : Fn where .out (add64 (.c64 48) (.op (.div .w64 .rem_u) [n, .c64 10])), .ret (.c64 0) ] +/-! ### Arrays `[spec §9]` + +An array of `n` elements is one range of `max n 1` slots of two words each: a cell header and +the contents. Slot `i` is at `base + 16·i`, and is an ordinary cell object, so its address is +the element's location and `load`, `store`, a copy and a drop treat it as any cell. The array +value is the pair `(base, i32[n])`. An empty array keeps one slot, so the base is an object. -/ + +/-- The address of slot `i`. -/ +def slotAddr (b i : Exp) : Exp := add64 b (shl64 i (.c64 4)) + +/-- `i32[i]` from a 64-bit `i` below `2³²`. -/ +def boxIdx (i : Exp) : Exp := or64 (shl64 i (.c64 32)) (.c64 3) + +/-- `array_tab p`, `p = (i32[c], f)`: `len(c)` slots filled by `f i` in order. Locals `p`, +`n`, `f`, `b`, `s` (the slot count), `t`. -/ +def arrTabFn : Fn where + name := "array_tab" + params := 1 + locals := 5 + body := + let p := Exp.loc 0; let n := Exp.loc 1; let f := Exp.loc 2; let b := Exp.loc 3 + let s := Exp.loc 4 + [ .set 1 (shr64 (.load p 1) (.c64 32)), .set 2 (.load p 2), .free p, + .ite (ltu64 n (.c64 0x80000000)) [] [.set 1 (.c64 0)], + .set 4 n, .ite (eq64 n (.c64 0)) [.set 4 (.c64 1)] [], + .set 3 (.alloc (shl64 s (.c64 1))), + .eval (.call fArrInit [b, .c64 0, s]), + .eval (.call fArrTabLoop [b, f, .c64 0, n]), + .eval (.call fDrop [f]) ] ++ + mkObj 5 hPair [b, boxIdx n] ++ [.ret (.loc 5)] + +/-- `arrInit b i s`: slots `i … s-1` become cells holding `()`. -/ +def arrInitFn : Fn where + name := "array_init" + params := 3 + locals := 0 + body := + let b := Exp.loc 0; let i := Exp.loc 1; let s := Exp.loc 2 + [ .ite (ltu64 i s) + [ .store (slotAddr b i) 0 (.c64 hCell), .store (slotAddr b i) 1 (.c64 unitW), + .tailCall fArrInit [b, add64 i (.c64 1), s] ] [], + .ret (.c64 0) ] + +/-- `arrTabLoop b f i n`: slot `i` gets a copy of `f` applied to `i32[i]`, for `i < n`. -/ +def arrTabLoopFn : Fn where + name := "array_tab_loop" + params := 4 + locals := 2 + body := + let b := Exp.loc 0; let f := Exp.loc 1; let i := Exp.loc 2; let n := Exp.loc 3 + let fc := Exp.loc 4 + [ .ite (ltu64 i n) + [ .set 4 (.call fCopy [f]), + .set 5 (.callInd (.load fc 1) [fc, boxIdx i]), + .store (slotAddr b i) 1 (.loc 5), + .tailCall fArrTabLoop [b, f, add64 i (.c64 1), n] ] [], + .ret (.c64 0) ] + +/-- `array_idx p`, `p = ((b, i32[n]), i32[i])`: `inl` of slot `i` when `i < n` unsigned, else +`inr ()`. Locals `p`, `q`, `w`, `b`, `n`, `t`. -/ +def arrIdxFn : Fn where + name := "array_idx" + params := 1 + locals := 5 + body := + let p := Exp.loc 0; let q := Exp.loc 1; let w := Exp.loc 2; let b := Exp.loc 3 + let n := Exp.loc 4 + [ .set 1 (.load p 1), .set 2 (.load p 2), .free p, + .set 3 (.load q 1), .set 4 (shr64 (.load q 2) (.c64 32)), .free q, + .ite (ltu64 (shr64 w (.c64 32)) n) + (mkObj 5 hInj₁ [slotAddr b (shr64 w (.c64 32))] ++ [.ret (.loc 5)]) + (mkObj 5 hInj₂ [.c64 unitW] ++ [.ret (.loc 5)]) ] + +/-- `array_drop p`, `p = (f, (b, i32[n]))`: each element to a copy of `f`, in order, then the +range freed. Locals `p`, `f`, `q`, `b`, `n`. -/ +def arrDropFn : Fn where + name := "array_drop" + params := 1 + locals := 4 + body := + let p := Exp.loc 0; let f := Exp.loc 1; let q := Exp.loc 2; let b := Exp.loc 3 + let n := Exp.loc 4 + [ .set 1 (.load p 1), .set 2 (.load p 2), .free p, + .set 3 (.load q 1), .set 4 (shr64 (.load q 2) (.c64 32)), .free q, + .eval (.call fArrDropLoop [b, f, .c64 0, n]), + .eval (.call fDrop [f]), .free b, .ret (.c64 unitW) ] + +/-- `arrDropLoop b f i n`: slot `i`'s contents, moved out, to a copy of `f`, for `i < n`. -/ +def arrDropLoopFn : Fn where + name := "array_drop_loop" + params := 4 + locals := 1 + body := + let b := Exp.loc 0; let f := Exp.loc 1; let i := Exp.loc 2; let n := Exp.loc 3 + let fc := Exp.loc 4 + [ .ite (ltu64 i n) + [ .set 4 (.call fCopy [f]), + .eval (.call fDrop [.callInd (.load fc 1) [fc, .load (slotAddr b i) 1]]), + .tailCall fArrDropLoop [b, f, add64 i (.c64 1), n] ] [], + .ret (.c64 0) ] + def runtimeFns : List Fn := - [copyFn, dropFn, copyEnvFn, dropEnvFn, printFn, printAtomFn, printDecFn] + [copyFn, dropFn, copyEnvFn, dropEnvFn, printFn, printAtomFn, printDecFn, + arrTabFn, arrInitFn, arrTabLoopFn, arrIdxFn, arrDropFn, arrDropLoopFn] end Backend.Flat diff --git a/Backend/Flat/Syntax.lean b/Backend/Flat/Syntax.lean index eca5ca467..12e2316ae 100644 --- a/Backend/Flat/Syntax.lean +++ b/Backend/Flat/Syntax.lean @@ -31,6 +31,9 @@ Objects start with a header word; bits 0–7 are the kind: | `i64[n]` | `5` | the raw 64 bits of `n` | | cell | `6` | the contents | +An array (SPEC §9) is one range of `max n 1` cells, two words each, and its value is the pair +`(base, i32[n])`; the cell at `base + 16·i` is element `i` (`Backend/Flat/Runtime.lean`). + `i64` is boxed because the erasure is untyped: a deep copy or drop must tell a pointer from an immediate by the word alone. -/ diff --git a/Backend/README.md b/Backend/README.md index 481de200e..2b1febfc6 100644 --- a/Backend/README.md +++ b/Backend/README.md @@ -59,6 +59,15 @@ byte is its kind, and its fields: | `i64[n]` | `5` | the 64 bits | | cell | `6` | the contents | +An array (SPEC §9) is one allocation of `max n 1` slots of two words, each an ordinary cell +(header `6`, then the contents); element `i` is the slot at `base + 16·i`, so its location is a +cell address and `load`, `store`, copies and drops treat it as any cell. The array value is the +pair `(base, i32[n])`. `idx` compares the index with `n` unsigned and computes the address; +`tab` allocates the slots and fills them in a loop, calling a copy of the tabulator per element; +`drop` moves each element to a copy of the consumer and frees the allocation once +(`Backend/Flat/Runtime.lean`). The closure IR keeps an array as consecutive cells, as the source +machine does. A type-directed layout would drop the header, making the slot `sizeof A`. + `i64` is boxed: the erasure is untyped, so a deep copy or drop tells a pointer from an immediate by the word alone, and a full-width `i64` leaves no bit for that. A type-directed layout would unbox it. diff --git a/Core.lean b/Core.lean index c595bbdf9..af9a47844 100644 --- a/Core.lean +++ b/Core.lean @@ -1,4 +1,5 @@ import Core.Types +import Core.SepRange import Core.FrameRecords import Core.World import Core.ShapeMu @@ -51,3 +52,6 @@ import Core.StreamPrograms import Core.Integers import Core.IntPrograms import Core.Dtor +import Core.ArrayWp +import Core.Arrays +import Core.ArrayPrograms diff --git a/Core/ArrayPrograms.lean b/Core/ArrayPrograms.lean new file mode 100644 index 000000000..e4899ae02 --- /dev/null +++ b/Core/ArrayPrograms.lean @@ -0,0 +1,90 @@ +import Core.IntPrograms +import Core.Dtor + +/-! +# Core — a program over an array `[spec §9]` + +One closed `CDerives` derivation, run by adequacy at every host (`arrProg_runs`) and by the +interpreter at `wasmHost` (`arrProg_eval`, kernel-checked by `decide`): + + a ← array_new (3, λi. i + 10); -- [10, 11, 12] + (a, old) ← array_replace (a, (1, 42)); -- old = inl 11; a = [10, 42, 12] + (a, n) ← array_size a; -- n = 3 + array_free (λx. (), a); -- the three cells freed + (old, n) + +Observed: `(inl 11, 3)` and no cell left, so `tab` placed three cells filled from the tabulator, +`idx` found cell `1`, and `drop` freed every cell. +-/ + +noncomputable section + +variable {ℍ : BoCa.Host} [BoCa.HostIs ℍ] + +namespace BoCa.Fig16.LogRel.Core +open BoCa +open BoCa.Fig16 +open BoCa.Fig16.BoLo +open BoCa.Lifetime (LSub LifeCtx LifeVar) + +/-- `λi. i + 10`. -/ +def tab10V : Val := .lam (hop (.bin .w32 .add) (.pair (.var 0) (i32E 10))) + +theorem d_tab10 {Γ : Ctx VTy} (hΓ : Ctx.Dead Γ) : + CDerives LifeCtx.empty Γ (.val tab10V) (.val (.bang (.lolli .i32 .i32))) := + .bangThunkV (persistent_dead hΓ) rfl (.lam (.ret (.lolliE (.left (deadSplit hΓ)) + (.tensorI (.left (deadSplit hΓ)) (.var (.here hΓ.toP)) (d_i32 10 (Ctx.Dead.cons hΓ).toP)) + (.bangE (.hostOp _ (Ctx.Dead.cons hΓ).toP))))) + +/-- `λx. ()`, the consumer of an `I32`. -/ +def dropI32V : Val := .lam (.val .unit) + +theorem d_dropI32 {Γ : Ctx VTy} (hΓ : Ctx.Dead Γ) : + CDerives LifeCtx.empty Γ (.val dropI32V) (.val (.bang (.lolli .i32 .unit))) := + .bangThunkV (persistent_dead hΓ) rfl (.lam (.ret (.unitI (.drop rfl hΓ.toP)))) + +/-- The program. -/ +def arrProg : Expr := + bindE (.app (.val (.prim .tab)) (.pair (i32E 3) (.val tab10V))) + (.letpair (.app (.val replaceV) (.pair (.var 0) (.pair (i32E 1) (i32E 42)))) + (.letpair (.app (.val sizeV) (.var 1)) + (.seq (.app (.val (.prim .drop)) (.pair (.val dropI32V) (.var 1))) + (.pair (.var 2) (.var 0))))) + +theorem arrProg_derives : + CDerives LifeCtx.empty [] arrProg (.comp (.ret (.tensor (.sum .i32 .i32) .i32))) := by + refine .ret (bindP (A := .array .i32) ?_ ?_) + · exact appP (pairP (d_i32 3 (by cdeadP)) (d_tab10 (by cdead))) (.bangE (.arrNew .i32 (by cdeadP))) + · refine letP (A := .array .i32) (B := .sum .i32 .i32) ?_ ?_ + · exact appP (pairP (.var (by csoloP)) (pairP (d_i32 1 (by cdeadP)) (d_i32 42 (by cdeadP)))) + (.bangE (.arrReplace .i32 (by cdeadP))) + · refine letP (A := .array .i32) (B := .i32) ?_ ?_ + · exact appP (.var (by csoloP)) (.bangE (.arrSize .i32 (by cdeadP))) + · refine .seqV (partP_split (.app (.val (.prim .drop)) (.pair (.val dropI32V) (.var 1))) 0 _) + ?_ ?_ + · refine appP (.tensorI (split_kill _).toP (d_dropI32 (kill_dead _)) (.var (by csoloP))) + (.bangE (.arrFree .i32 (by cdeadP))) + · exact pairP (.var (by csoloP)) (.var (by csoloP)) + +/-- **The program runs at every host**: from the empty memory to a value of +`(I32 ⊕ I32) ⊗ I32`, and the empty memory. -/ +theorem arrProg_runs : ∃ v : Val, + BoCa.BoLo.Steps Adequacy.emptyMem arrProg Adequacy.emptyMem (.val v) := by + have hwp : wpTS [] arrProg (fun _ _ => Fig16.BoLo.pure True) (PMap.empty : WRes) := + wpTS_mono (fun _ v ρ hv => ⟨vP_empty_of_persistent rfl hv, trivial⟩) PMap.empty + (wp_of_derives arrProg_derives) + obtain ⟨v, hsteps, -⟩ := theorem32 arrProg _ hwp + exact ⟨v, hsteps⟩ + +/-- `(inl n, m)` as two integers, when the run ends with no cell left. -/ +def arrOf : Outcome → Option (UInt32 × UInt32) + | .value ⟨.pair (.inj₁ a) b, _⟩ false => do pure (← i32Of a, ← i32Of b) + | _ => none + +set_option maxRecDepth 100000 in +/-- The interpreter at Wasm's host: the old element `11` and the length `3`, no cell left. -/ +theorem arrProg_eval : arrOf (eval arrProg) = some (11, 3) := by decide + +end BoCa.Fig16.LogRel.Core + +end diff --git a/Core/ArrayWp.lean b/Core/ArrayWp.lean new file mode 100644 index 000000000..e0facfc99 --- /dev/null +++ b/Core/ArrayWp.lean @@ -0,0 +1,146 @@ +import Core.Integers + +/-! +# Core — the array steps at the typed world `[spec §9]` + +`tab` places `n` cells holding `()` at fresh consecutive locations and continues as `tabFill`. +In the typed world that is `n` of `TW.alloc`, at the locations `ℓ, …, ℓ + n − 1` above every +location the configuration holds (`fresh_block`): `alloc_block` builds the world after them, +and `wpTS_tab` puts the step in front of the run of `tabFill`. `idx` and `drop` change no memory: +`wpTS_idx` and `wpTS_dropStep` are `wpTS_head`. +-/ + +noncomputable section + +variable {ℍ : BoCa.Host} [BoCa.HostIs ℍ] + +namespace BoCa.Fig16.LogRel.Core +open BoCa +open BoCa.Fig16 +open BoCa.Fig16.BoLo +open BoCa.BoLo (Heap Steps Step1 Head Kont eLoad eStore) +open BoCa.Lifetime (LSub LifeCtx LifeVar) + +/-- `omega` at locations: `Loc` is `ℕ`. -/ +macro "lomega" : tactic => `(tactic| ((try unfold BoCa.Loc at *) <;> omega)) + +/-- A finite map at `Loc = ℕ` misses a whole run of locations: every location from one past +its largest. `[spec §9]` -/ +theorem fresh_block {A : Type} (p : PMap BoCa.Loc A) : ∃ ℓ : Nat, ∀ j : Nat, p.get (ℓ + j) = none := by + obtain ⟨d, hd⟩ := p.finite + have hub : ∀ (t : List Nat) (l : Nat), l ∈ t → l ≤ t.foldr max 0 := by + intro t + induction t with + | nil => intro l hl; exact absurd hl (by simp) + | cons a t ih => + intro l hl + rcases List.mem_cons.mp hl with rfl | hl + · exact Nat.le_max_left _ _ + · exact Nat.le_trans (ih l hl) (Nat.le_max_right _ _) + refine ⟨d.foldr max 0 + 1, fun j => ?_⟩ + cases e : p.get (d.foldr max 0 + 1 + j) with + | none => rfl + | some ψ => + have := hub d _ (hd _ (by rw [e]; simp)) + omega + +/-- One cell placed at a location the heap does not hold: the world after `TW.alloc`, with the +frame kept. `[spec §9]` -/ +theorem alloc_world {ρf ρ σ : WRes} {ps rs : List FrameRec} {μ : Heap} {l : Loc} (v : Val) + (hf : ResU.Hash ρf ρ) (hσ : ResU.CompS ρf ρ σ) (hT : TW σ ps rs) (hμ : ResU.Lower σ μ) + (hμl : μ l = none) : + ∃ x σ', ResU.CompS ρ (ResU.single l (CellU.ownOf v)) x ∧ ResU.Hash ρf x ∧ + ResU.CompS ρf x σ' ∧ TW σ' ps rs ∧ ResU.Lower σ' (μ.upd l v) ∧ ResU.UpdV ρ x ∧ + (∀ a, AgW σ' a → AgW σ a) := by + obtain ⟨τ, hτ, hval⟩ := id hμ + have hρl : ρ.get l = none := + ((ResU.Comp.eq_none_iff hσ l).mp (get_eq_none_of_flat hτ ((lower_eq_none_iff hval).mp hμl))).2 + obtain ⟨x, hx⟩ := (ResU.compS_defined_iff ρ (ResU.single l (CellU.ownOf v))).mpr + (compatS_single_of_get_none hρl) + obtain ⟨σ', hσ'x, hfx, hlow'⟩ := hash_compS_own hσ hμ hμl hx + obtain ⟨y, hy, hyσ'⟩ := (ResU.CompS.assoc ρf ρ (ResU.single l (CellU.ownOf v)) σ').mp + ⟨x, hx, hσ'x⟩ + cases ResU.CompS.functional hy hσ + exact ⟨x, σ', hx, hfx, hσ'x, TW.alloc hT hμ hμl hyσ' (hash_valid_comp hfx hσ'x), hlow', + updV_compS_own hx (hash_valid hf).2 (hash_valid hfx).2, fun a ha => alloc_ag hyσ' ha⟩ + +theorem fill_zero (μ : Heap) (ℓ : Loc) (v : Val) : μ.fill ℓ 0 v = μ := by + funext k; simp only [BoCa.BoLo.Heap.fill]; split + · lomega + · rfl + +theorem fill_succ (μ : Heap) (ℓ : Loc) (n : Nat) (v : Val) : + (μ.upd ℓ v).fill (ℓ + 1) n v = μ.fill ℓ (n + 1) v := by + funext k + simp only [BoCa.BoLo.Heap.fill, BoCa.BoLo.Heap.upd] + by_cases h₁ : ℓ + 1 ≤ k ∧ k < ℓ + 1 + n + · rw [if_pos h₁, if_pos (by lomega)] + · rw [if_neg h₁] + by_cases h₂ : k = ℓ + · rw [if_pos h₂, if_pos (by lomega)] + · rw [if_neg h₂, if_neg (by lomega)] + +/-- `n` cells holding `v` placed at `ℓ + k, …, ℓ + k + n − 1`, locations the heap does not hold: +`n` of `alloc_world`. `[spec §9]` -/ +theorem alloc_block {ρf : WRes} {ps rs : List FrameRec} (v : Val) (ℓ : Nat) : + ∀ (n k : Nat) {ρ σ : WRes} {μ : Heap}, ResU.Hash ρf ρ → ResU.CompS ρf ρ σ → TW σ ps rs → + ResU.Lower σ μ → (∀ j, j < n → μ (ℓ + k + j) = none) → + ∃ x σ' cells, sepRange (fun m => ptoOwn m v) (ℓ + k) n cells ∧ ResU.CompS ρ cells x ∧ + ResU.Hash ρf x ∧ ResU.CompS ρf x σ' ∧ TW σ' ps rs ∧ ResU.Lower σ' (μ.fill (ℓ + k) n v) ∧ + ResU.UpdV ρ x ∧ (∀ a, AgW σ' a → AgW σ a) + | 0, k, ρ, σ, μ, hf, hσ, hT, hμ, _ => by + refine ⟨ρ, σ, PMap.empty, ⟨rfl, trivial⟩, ResU.comp_empty_right ρ, hf, hσ, hT, ?_, + (ResU.updV_self_iff ρ).mpr (hash_valid hf).2, fun _ h => h⟩ + rw [fill_zero]; exact hμ + | n + 1, k, ρ, σ, μ, hf, hσ, hT, hμ, hfr => by + obtain ⟨x₁, σ₁, hx₁, hf₁, hσ₁, hT₁, hμ₁, hu₁, hag₁⟩ := + alloc_world v hf hσ hT hμ (by simpa using hfr 0 (Nat.succ_pos n)) + obtain ⟨x, σ', cells, hcells, hx, hf', hσ', hT', hμ', hu, hag⟩ := + alloc_block v ℓ n (k + 1) hf₁ hσ₁ hT₁ hμ₁ (fun j hj => by + rw [BoCa.BoLo.Heap.upd_other (by lomega)] + have := hfr (j + 1) (by lomega) + rwa [show ℓ + k + (j + 1) = ℓ + (k + 1) + j by lomega] at this) + obtain ⟨bc, hbc, hρx⟩ := compS_reassoc hx₁ hx + refine ⟨x, σ', bc, ⟨_, cells, hbc, rfl, ?_⟩, hρx, hf', hσ', hT', ?_, hu₁.trans hu, + fun a ha => hag₁ a (hag a ha)⟩ + · rwa [show ℓ + k + 1 = ℓ + (k + 1) by omega] + · rw [← fill_succ]; rwa [show ℓ + k + 1 = ℓ + (k + 1) by omega] + +/-- **The `tab` step.** At a fresh run of locations `ℓ, …`, the placeholders `ℓ + i ↦ ()` are +handed to the run of `tabFill`. `[spec §9]` -/ +theorem wpTS_tab (ls : List SRec) (c : UInt32) (f : Val) (Q : List SRec → Val → WProp) : + Entails (all fun ℓ : Loc => wand (sepRange (fun m => ptoOwn m .unit) ℓ (arrLen c).toNat) + (wpTS ls (tabFill f ℓ (arrLen c) 0 (arrLen c).toNat) Q)) + (wpTS ls (.app (.val (.prim .tab)) (.val (Val.pair (Val.i32 c) f))) Q) := by + intro ρ h ρf σ ps hf hσ hT htg + obtain ⟨μ, hμ⟩ := lower_of_hash hf hσ + obtain ⟨τ, hτ, hval⟩ := id hμ + obtain ⟨ℓ, hℓ⟩ := fresh_block τ + have hfree : ∀ j, μ (ℓ + j) = none := fun j => (lower_eq_none_iff hval).mpr (hℓ j) + obtain ⟨x, σ', cells, hcells, hx, hf', hσ', hT', hμ', hu, hag⟩ := + alloc_block .unit ℓ (arrLen c).toNat 0 hf hσ hT hμ (fun j _ => by simpa using hfree j) + simp only [Nat.add_zero] at hcells hμ' + obtain ⟨ρ', ρp, fρ', fρ'p, π, v, μ₁, μ', ps', ls', h₁, h₂, h₃, h₅, h₆, h₇, h₈, h₉, hA, hB, + hT'', htg', hps, hls, hC⟩ := + h ℓ cells x hcells hx ρf σ' ps hf' hσ' hT' (tagged_of_ag hag htg) + cases ResU.Lower.functional h₅ hμ' + exact ⟨ρ', ρp, fρ', fρ'p, π, v, μ, μ', ps', ls', h₁, h₂, h₃, hμ, h₆, h₇, + .more (Step1.head (Head.tab μ c f ℓ (fun i _ => hfree i))) h₈, h₉, hu.trans hA, hB, + hT'', htg', hps, hls, hC⟩ + +/-- The `idx` step. `[spec §9]` -/ +theorem wpTS_idx (ls : List SRec) (ℓ : Loc) (n i : UInt32) (Q : List SRec → Val → WProp) : + Entails (wpTS ls (.val (idxAns ℓ n i)) Q) + (wpTS ls (.app (.val (.prim .idx)) (.val (Val.pair (Val.arr ℓ n) (Val.i32 i)))) Q) := + wpTS_head (fun μ => Head.idx μ ℓ n i) ls Q + +/-- The `drop` step. `[spec §9]` -/ +theorem wpTS_dropStep (ls : List SRec) (f : Val) (ℓ : Loc) (n : UInt32) + (Q : List SRec → Val → WProp) : + Entails (wpTS ls (dropFill f ℓ 0 n.toNat) Q) + (wpTS ls (.app (.val (.prim .drop)) (.val (Val.pair f (Val.arr ℓ n)))) Q) := + wpTS_head (fun μ => Head.drop μ f ℓ n) ls Q + +end BoCa.Fig16.LogRel.Core + +end diff --git a/Core/Arrays.lean b/Core/Arrays.lean new file mode 100644 index 000000000..b5e7fa445 --- /dev/null +++ b/Core/Arrays.lean @@ -0,0 +1,563 @@ +import Core.Integers +import Core.Fix +import Core.ArrayWp + +/-! +# Core — Arrays `[spec §9]` + +`[L]` (`VTy.fam L`) is the run-time family `⊛_{i vP L ls δ (.loc m)) ℓ n.toNat ρ := by + constructor + · rintro ⟨ℓ, n, hs⟩ + obtain ⟨he, hs⟩ := pure_sep_iff.mp hs + exact ⟨ℓ, n, he, hs⟩ + · rintro ⟨ℓ, n, he, hs⟩ + exact ⟨ℓ, n, pure_sep_mk he hs⟩ + +theorem vP_fam_mk {L : VTy} {ls : List SRec} {δ : LSub} {ℓ : Loc} {n : UInt32} {ρ : WRes} + (hs : sepRange (fun m => vP L ls δ (.loc m)) ℓ n.toNat ρ) : vP (.fam L) ls δ (Val.arr ℓ n) ρ := + vP_fam_iff.mpr ⟨ℓ, n, rfl, hs⟩ + +/-- A leaf borrow holds no `own` cell. -/ +theorem noOwn_imm {a : Lifetime.Life} {A : VTy} {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes} + (h : vP (.imm a A) ls δ v ρ) : NoOwn ρ := by + obtain ⟨α, -, ℓ, hs⟩ := h + obtain ⟨-, s, u, σ, hσ, ls₀, rfl, -⟩ := pure_sep_iff.mp hs + exact ResU.restrict_single_other (by simp) + +/-- A family of leaves that hold no `own` cell holds none. -/ +theorem noOwn_fam {L : VTy} {ls : List SRec} {δ : LSub} + (hL : ∀ m ρ, vP L ls δ (.loc m) ρ → NoOwn ρ) {v : Val} {ρ : WRes} + (h : vP (.fam L) ls δ v ρ) : NoOwn ρ := by + obtain ⟨ℓ, n, -, hs⟩ := vP_fam_iff.mp h + exact sepRange_closed noOwn_empty (fun _ _ _ hc h₁ h₂ => noOwn_compS hc h₁ h₂) hL hs + +theorem noOwn_arrayImm {a : Lifetime.Life} {A : VTy} {ls : List SRec} {δ : LSub} {v : Val} + {ρ : WRes} (h : vP (.arrayImm a A) ls δ v ρ) : NoOwn ρ := + noOwn_fam (fun _ _ h => noOwn_imm h) h + +/-- A part of a resource with no `own` cell has none. -/ +theorem noOwn_right {a b c : WRes} (hc : ResU.CompS a b c) (h : NoOwn c) : NoOwn b := + noOwn_iff.mpr fun l ψ hl => by + obtain ⟨ψ', e', k', -, -⟩ := compS_get_right' hc hl + rw [← k']; exact noOwn_iff.mp h l ψ' e' + +/-- A resource with no `own` cell may be dropped in front of a run. -/ +theorem wpTS_drop {ls : List SRec} {e : Expr} {Q : List SRec → Val → WProp} {σ τ ρ : WRes} + (hσ : NoOwn σ) (hc : ResU.CompS σ τ ρ) (h : wpTS ls e Q τ) : wpTS ls e Q ρ := + wpTS_frame_noOwn (R := fun ρ' => ρ' = σ) (fun _ e => e ▸ hσ) ls e Q ρ ⟨σ, τ, hc, rfl, h⟩ + +/-- The constant at a dead context: its resource is `∅`. -/ +theorem sem_const {Δ : LifeCtx} {Γ : Ctx VTy} {v : Val} {A : VTy} (hΓ : Ctx.DeadP Γ) + (hcl : closedAt 0 v.1 = true) + (h : ∀ (δ : LSub) (ls : List SRec), Δ.Models δ → vP A ls δ v PMap.empty) : + SemV Δ Γ (.val v) A := by + intro δ γ ls ρ hδ hg + have hρ : ρ = PMap.empty := gSepC_deadP hΓ (gDenC_iff.mp hg).2 + subst hρ + show wpTS ls (substAll γ (.val v)) _ _ + rw [substAll_closed hcl] + exact wpTS_val ls v _ _ (h δ ls hδ) + +/-! ### `array_len` -/ + +/-- `λp. let (b, n) = p in n`. `[spec §9]` -/ +def arrLenV : Val := .lam (.letpair (.var 0) (.var 0)) + +/-- `array_len : !(ArrayImm @a A ⇒ I32)`. `[spec §9]` -/ +abbrev arrLenTy (a : Lifetime.Life) (A : VTy) : VTy := .bang (.lolli (.arrayImm a A) (.int .w32)) + +theorem arrLen_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {a : Lifetime.Life} {A : VTy} + (hΓ : Ctx.DeadP Γ) : SemV Δ Γ (.val arrLenV) (arrLenTy a A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨ℓ, n, rfl, -⟩ := vP_fam_iff.mp hv + refine wpTS_drop (noOwn_arrayImm hv) (ResU.comp_empty_right σ) ?_ + refine wpTS_lolli ls' _ _ _ _ ?_ + show wpTS ls' (.letpair (.val (Val.arr ℓ n)) (.var 0)) _ _ + refine wpTS_tensor ls' (Val.loc ℓ) (Val.i32 n) (.var 0) _ _ ?_ + exact wpTS_val ls' (Val.i32 n) _ _ (vP_i32 n) + +/-! ### `array_get` -/ + +/-- `array_get : !(ArrayImm @a A ⊗ I32 ⇒ Imm @a A ⊕ 1)`. `[spec §9]` -/ +abbrev arrGetTy (a : Lifetime.Life) (A : VTy) : VTy := + .bang (.lolli (.tensor (.arrayImm a A) (.int .w32)) (.sum (.imm a A) .unit)) + +/-- The argument of an operation on a family and an index. -/ +theorem vP_fam_idx {L : VTy} {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes} + (h : vP (.tensor (.fam L) (.int .w32)) ls δ v ρ) : + ∃ ℓ n i, v = Val.pair (Val.arr ℓ n) (Val.i32 i) ∧ + sepRange (fun m => vP L ls δ (.loc m)) ℓ n.toNat ρ := by + obtain ⟨v₁, v₂, hk⟩ := h + obtain ⟨rfl, ρ₁, ρ₂, hc, h₁, ⟨e₂, i, rfl⟩⟩ := pure_sep_iff.mp hk + subst e₂ + obtain ⟨ℓ, n, rfl, hs⟩ := vP_fam_iff.mp h₁ + rw [ResU.CompS.functional hc (ResU.comp_empty_right ρ₁)] + exact ⟨ℓ, n, i, rfl, hs⟩ + +/-- The leaf `idx` answers: in range, the leaf at `ℓ + i` beside the rest of the family; +otherwise nothing. -/ +theorem idxAns_cases (ℓ : Loc) (n i : UInt32) : + (i.toNat < n.toNat ∧ idxAns ℓ n i = .inj₁ (.loc (ℓ + i.toNat))) ∨ idxAns ℓ n i = .inj₂ .unit := by + unfold idxAns + by_cases h : i < n + · exact Or.inl ⟨UInt32.lt_iff_toNat_lt.mp h, if_pos h⟩ + · exact Or.inr (if_neg h) + +theorem arrGet_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {a : Lifetime.Life} {A : VTy} + (hΓ : Ctx.DeadP Γ) : SemV Δ Γ (.val (.prim .idx)) (arrGetTy a A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨ℓ, n, i, rfl, hs⟩ := vP_fam_idx hv + refine wpTS_head (fun μ => Head.idx μ ℓ n i) ls' _ σ ?_ + rcases idxAns_cases ℓ n i with ⟨hi, he⟩ | he + · rw [he] + obtain ⟨ρi, ρr, hc, hPi, -⟩ := sepRange_pick hi hs + have hσ : NoOwn σ := sepRange_closed noOwn_empty + (fun _ _ _ hc h₁ h₂ => noOwn_compS hc h₁ h₂) (fun _ _ h => noOwn_imm h) hs + refine wpTS_drop (noOwn_right hc hσ) (ResU.CompS.comm hc) ?_ + exact wpTS_val ls' _ _ _ (Or.inl ⟨_, pure_sep_mk rfl hPi⟩) + · rw [he] + have hσ : NoOwn σ := sepRange_closed noOwn_empty + (fun _ _ _ hc h₁ h₂ => noOwn_compS hc h₁ h₂) (fun _ _ h => noOwn_imm h) hs + refine wpTS_drop hσ (ResU.comp_empty_right σ) ?_ + exact wpTS_val ls' _ _ _ (Or.inr ⟨.unit, pure_sep_mk rfl ⟨rfl, rfl⟩⟩) + +/-! ### `array_copy` and the forgets -/ + +/-- A duplicable leaf predicate gives a duplicable range. -/ +theorem sepRange_dup {P : Loc → WProp} + (hP : ∀ m ρ, P m ρ → ∃ τ₁ τ₂, ResU.CompS τ₁ τ₂ ρ ∧ P m τ₁ ∧ P m τ₂) : + ∀ {ℓ : Loc} {n : Nat} {ρ : WRes}, sepRange P ℓ n ρ → + ∃ τ₁ τ₂, ResU.CompS τ₁ τ₂ ρ ∧ sepRange P ℓ n τ₁ ∧ sepRange P ℓ n τ₂ + | _, 0, ρ, h => by + obtain ⟨rfl, -⟩ := h + exact ⟨PMap.empty, PMap.empty, ResU.comp_empty_right _, ⟨rfl, trivial⟩, ⟨rfl, trivial⟩⟩ + | _, _ + 1, _, ⟨ρ₁, ρ₂, hc, h₁, h₂⟩ => by + obtain ⟨τ₁, τ₂, hτ, g₁, g₂⟩ := hP _ _ h₁ + obtain ⟨σ₁, σ₂, hσ, k₁, k₂⟩ := sepRange_dup hP h₂ + obtain ⟨bc, hbc, hρ⟩ := compS_reassoc hτ hc + obtain ⟨ac, hac, hbc'⟩ := compS_lcomm hσ hbc + obtain ⟨X, hX, hρ'⟩ := compS_reassoc' hbc' hρ + exact ⟨X, ac, hρ', ⟨τ₁, σ₁, hX, g₁, k₁⟩, ⟨τ₂, σ₂, hac, g₂, k₂⟩⟩ + +theorem vP_imm_dup {a : Lifetime.Life} {A : VTy} {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes} + (h : vP (.imm a A) ls δ v ρ) : + ∃ τ₁ τ₂, ResU.CompS τ₁ τ₂ ρ ∧ vP (.imm a A) ls δ v τ₁ ∧ vP (.imm a A) ls δ v τ₂ := by + obtain ⟨α, ha, ℓ, hs⟩ := h + obtain ⟨hvl, himm⟩ := pure_sep_iff.mp hs + obtain ⟨τ₁, τ₂, hc, h₁, h₂⟩ := ptoImmS_dup himm + exact ⟨τ₁, τ₂, hc, ⟨α, ha, ℓ, pure_sep_mk hvl h₁⟩, ⟨α, ha, ℓ, pure_sep_mk hvl h₂⟩⟩ + +/-- `array_copy : !(ArrayImm @a A ⇒ ArrayImm @a A ⊗ ArrayImm @a A)`. `[spec §9]` -/ +abbrev arrCopyTy (a : Lifetime.Life) (A : VTy) : VTy := + .bang (.lolli (.arrayImm a A) (.tensor (.arrayImm a A) (.arrayImm a A))) + +/-- `λp. (p, p)`. -/ +def copyV : Val := .lam (.pair (.var 0) (.var 0)) + +theorem arrCopy_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {a : Lifetime.Life} {A : VTy} + (hΓ : Ctx.DeadP Γ) : SemV Δ Γ (.val copyV) (arrCopyTy a A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨ℓ, n, rfl, hs⟩ := vP_fam_iff.mp hv + refine wpTS_lolli ls' _ _ _ _ ?_ + show wpTS ls' (.pair (.val (Val.arr ℓ n)) (.val (Val.arr ℓ n))) _ _ + refine wpTS_val ls' (Val.pair (Val.arr ℓ n) (Val.arr ℓ n)) _ _ ?_ + obtain ⟨τ₁, τ₂, hc, h₁, h₂⟩ := sepRange_dup (fun _ _ h => vP_imm_dup h) hs + exact ⟨_, _, pure_sep_mk rfl ⟨τ₁, τ₂, hc, vP_fam_mk h₁, vP_fam_mk h₂⟩⟩ + +/-- `array_forget : !(ArrayImm @a A ⇒ 1)`. `[spec §9]` -/ +abbrev arrForgetTy (a : Lifetime.Life) (A : VTy) : VTy := .bang (.lolli (.arrayImm a A) .unit) + +/-- `λp. ()`. -/ +def forgetV : Val := .lam (.val .unit) + +/-- A family whose leaves hold no `own` cell is forgotten by `λp. ()`. -/ +theorem forget_fam_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {L : VTy} (hΓ : Ctx.DeadP Γ) + (hL : ∀ ls δ m ρ, vP L ls δ (.loc m) ρ → NoOwn ρ) : + SemV Δ Γ (.val forgetV) (.bang (.lolli (.fam L) .unit)) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + refine wpTS_drop (noOwn_fam (hL ls' δ) hv) (ResU.comp_empty_right σ) ?_ + refine wpTS_lolli ls' _ _ _ _ ?_ + exact wpTS_val ls' .unit _ _ ⟨rfl, rfl⟩ + +theorem arrForget_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {a : Lifetime.Life} {A : VTy} + (hΓ : Ctx.DeadP Γ) : SemV Δ Γ (.val forgetV) (arrForgetTy a A) := + forget_fam_compatX hΓ fun _ _ _ _ h => noOwn_imm h + +/-! ### `array_size` -/ + +/-- `array_size : !(Array A ⇒ Array A ⊗ I32)`. `[spec §9]` -/ +abbrev arrSizeTy (A : VTy) : VTy := .bang (.lolli (.array A) (.tensor (.array A) (.int .w32))) + +/-- `λp. let (b, n) = p in ((b, n), n)`. `[spec §9]` -/ +def sizeV : Val := .lam (.letpair (.var 0) (.pair (.pair (.var 1) (.var 0)) (.var 0))) + +theorem arrSize_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {A : VTy} (hΓ : Ctx.DeadP Γ) : + SemV Δ Γ (.val sizeV) (arrSizeTy A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨ℓ, n, rfl, hs⟩ := vP_fam_iff.mp hv + refine wpTS_lolli ls' _ _ _ _ ?_ + show wpTS ls' (.letpair (.val (Val.arr ℓ n)) (.pair (.pair (.var 1) (.var 0)) (.var 0))) _ _ + refine wpTS_tensor ls' (Val.loc ℓ) (Val.i32 n) _ _ _ ?_ + refine wpTS_val ls' (Val.pair (Val.arr ℓ n) (Val.i32 n)) _ _ ?_ + exact ⟨_, _, pure_sep_mk rfl ⟨σ, PMap.empty, ResU.comp_empty_right σ, vP_fam_mk hs, + vP_i32 n⟩⟩ + +/-! ### `array_replace` -/ + +/-- `array_replace : !(Array A ⊗ (I32 ⊗ A) ⇒ Array A ⊗ (A ⊕ A))`. `[spec §9]` -/ +abbrev arrReplaceTy (A : VTy) : VTy := + .bang (.lolli (.tensor (.array A) (.tensor (.int .w32) A)) (.tensor (.array A) (.sum A A))) + +/-- The in-range branch, under `c` (`0`) and `x` (`1`): +`(λold. store c x; old) (load c)`. -/ +def swapCellE : Expr := + .app (.val (.lam (.seq (.app (.app (.val (.prim .store)) (.var 1)) (.var 2)) (.var 0)))) + (.app (.val (.prim .load)) (.var 0)) + +/-- `λp. let (f, r) = p in let (i, x) = r in +case idx (f, i) {inj₁ c. (f, inj₁ ((λold. store c x; old) (load c))) | inj₂ _. (f, inj₂ x)}`. +`[spec §9]` -/ +def replaceV : Val := + .lam (.letpair (.var 0) (.letpair (.var 0) + (.case (.app (.val (.prim .idx)) (.pair (.var 3) (.var 1))) + (.pair (.var 4) (.inj₁ swapCellE)) + (.pair (.var 4) (.inj₂ (.var 1)))))) + +/-- The in-range branch at the element `x`. -/ +def replaceIn (ℓ : Loc) (n : UInt32) (x : Val) : Expr := + .pair (.val (Val.arr ℓ n)) (.inj₁ (.app (.val (.lam (.seq + (.app (.app (.val (.prim .store)) (.var 1)) (x.1.shift 2 0)) (.var 0)))) + (.app (.val (.prim .load)) (.var 0)))) + +/-- The out-of-range branch at the element `x`. -/ +def replaceOut (ℓ : Loc) (n : UInt32) (x : Val) : Expr := + .pair (.val (Val.arr ℓ n)) (.inj₂ (x.1.shift 1 0)) + +theorem arr_subst_shift_succ (j : Nat) (s : Val) (e : Expr) : + Expr.subst j s (Expr.shift (j + 1) 0 e) = Expr.shift j 0 e := + Expr.subst_shift j 0 j s e (Nat.zero_le j) (by omega) + +theorem shift_arr (d c : Nat) (ℓ : Loc) (n : UInt32) : + Expr.shift d c (Val.arr ℓ n).1 = (Val.arr ℓ n).1 := rfl + +theorem subst_arr (j : Nat) (s : Val) (ℓ : Loc) (n : UInt32) : + Expr.subst j s (Val.arr ℓ n).1 = (Val.arr ℓ n).1 := rfl + +theorem shift_i32 (d c : Nat) (i : UInt32) : Expr.shift d c (Val.i32 i).1 = (Val.i32 i).1 := + rfl + +theorem replace_body₁ (v : Val) : + Expr.subst 0 v (.letpair (.var 0) (.letpair (.var 0) + (.case (.app (.val (.prim .idx)) (.pair (.var 3) (.var 1))) + (.pair (.var 4) (.inj₁ swapCellE)) (.pair (.var 4) (.inj₂ (.var 1)))))) = + .letpair (.val v) (.letpair (.var 0) + (.case (.app (.val (.prim .idx)) (.pair (.var 3) (.var 1))) + (.pair (.var 4) (.inj₁ swapCellE)) (.pair (.var 4) (.inj₂ (.var 1))))) := by + simp [Expr.subst, swapCellE] + +theorem replace_body₂ (ℓ : Loc) (n : UInt32) (w : Val) : + ((Expr.letpair (.var 0) + (.case (.app (.val (.prim .idx)) (.pair (.var 3) (.var 1))) + (.pair (.var 4) (.inj₁ swapCellE)) (.pair (.var 4) (.inj₂ (.var 1))))).subst 0 + (w.shift 1 0)).subst 0 (Val.arr ℓ n) = + .letpair (.val w) + (.case (.app (.val (.prim .idx)) (.pair (.val (Val.arr ℓ n)) (.var 1))) + (.pair (.val (Val.arr ℓ n)) (.inj₁ swapCellE)) + (.pair (.val (Val.arr ℓ n)) (.inj₂ (.var 1)))) := by + simp [Expr.subst, swapCellE, Val.shift, Expr.shift_zero, arr_subst_shift_succ, shift_arr] + +theorem replace_body₃ (ℓ : Loc) (n i : UInt32) (x : Val) : + ((Expr.case (.app (.val (.prim .idx)) (.pair (.val (Val.arr ℓ n)) (.var 1))) + (.pair (.val (Val.arr ℓ n)) (.inj₁ swapCellE)) + (.pair (.val (Val.arr ℓ n)) (.inj₂ (.var 1)))).subst 0 (x.shift 1 0)).subst 0 + (Val.i32 i) = + .case (.app (.val (.prim .idx)) (.val (Val.pair (Val.arr ℓ n) (Val.i32 i)))) + (replaceIn ℓ n x) (replaceOut ℓ n x) := by + simp [Expr.subst, swapCellE, replaceIn, replaceOut, Val.shift, Expr.shift_shift, + arr_subst_shift_succ, subst_arr, shift_i32] + +theorem replace_in (ℓ : Loc) (n : UInt32) (x : Val) (c : Loc) : + (replaceIn ℓ n x).subst 0 (Val.loc c) = + .pair (.val (Val.arr ℓ n)) (.inj₁ (.app (.val (.lam (.seq + (.app (.app (.val (.prim .store)) (.val (.loc c))) (x.1.shift 1 0)) (.var 0)))) + (eLoad c))) := by + simp [replaceIn, eLoad, Expr.subst, Val.shift, Expr.shift, arr_subst_shift_succ, subst_arr] + +theorem replace_out (ℓ : Loc) (n : UInt32) (x : Val) : + (replaceOut ℓ n x).subst 0 Val.unit = (Val.pair (Val.arr ℓ n) (Val.inj₂ x)).1 := by + simp [replaceOut, Expr.subst, Val.pair, Val.inj₂, Expr.shift_zero, arr_subst_shift_succ, + subst_arr] + +theorem replace_store (c : Loc) (x old : Val) : + Expr.subst 0 old (.seq (.app (.app (.val (.prim .store)) (.val (.loc c))) (x.1.shift 1 0)) + (.var 0)) = .seq (eStore c x) (.val old) := by + simp [eStore, Expr.subst, Expr.shift_zero, arr_subst_shift_succ] + +theorem arrReplace_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {A : VTy} + (hΓ : Ctx.DeadP Γ) : SemV Δ Γ (.val replaceV) (arrReplaceTy A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨v₁, v₂, hk⟩ := hv + obtain ⟨rfl, σ₁, σ₂, hc, h₁, h₂⟩ := pure_sep_iff.mp hk + obtain ⟨ℓ, n, rfl, hs⟩ := vP_fam_iff.mp h₁ + obtain ⟨w₁, x, hk₂⟩ := h₂ + obtain ⟨rfl, ρ₀, σx, hc₂, ⟨e₀, i, rfl⟩, hx⟩ := pure_sep_iff.mp hk₂ + subst e₀ + rw [eq_of_compS_empty_left hc₂] at hc + refine wpTS_lolli ls' _ _ _ _ ?_ + rw [replace_body₁] + refine wpTS_tensor ls' (Val.arr ℓ n) (Val.pair (Val.i32 i) x) _ _ _ ?_ + rw [replace_body₂] + refine wpTS_tensor ls' (Val.i32 i) x _ _ _ ?_ + rw [replace_body₃] + refine wpTS_bind (.case .hole _ _) _ ls' _ σ ?_ + refine wpTS_head (fun μ => Head.idx μ ℓ n i) ls' _ σ ?_ + refine wpTS_val ls' (idxAns ℓ n i) _ σ ?_ + simp only [Kont.plug] + rcases idxAns_cases ℓ n i with ⟨hi, he⟩ | he + · rw [he] + refine wpTS_sum₁ ls' _ _ _ _ σ ?_ + rw [replace_in] + obtain ⟨ρi, ρr, hci, hPi, hback⟩ := sepRange_pick hi hs + obtain ⟨ℓ', old, hs'⟩ := hPi + obtain ⟨hl, c, ρold, hcw, hcell, hold⟩ := pure_sep_iff.mp hs' + have hℓ : ℓ' = ℓ + i.toNat := by + have := congrArg Subtype.val hl; simpa using this.symm + subst hℓ + -- `σ = c ● G`, `G = ρold ● (ρr ● σx)` + obtain ⟨F, hF, hiF⟩ := compS_reassoc hci hc + obtain ⟨G, hG, hσ⟩ := compS_reassoc hcw hiF + refine wpTS_bind (.pairR (Val.arr ℓ n) (.inj₁ .hole)) _ ls' _ σ ?_ + refine wpTS_bind (.appR (.val (.lam _)) .hole) (eLoad (ℓ + i.toNat)) ls' _ σ ?_ + refine wpTS_load ls' _ old _ σ ⟨c, G, hσ, hcell, ?_⟩ + intro c₁ p₁ hc₁ hp₁ + rw [hc₁, ← hcell] at hp₁ + simp only [Kont.plug] + refine wpTS_lolli ls' _ old _ p₁ ?_ + rw [replace_store] + refine wpTS_bind (.seq .hole (.val old)) (eStore (ℓ + i.toNat) x) ls' _ p₁ ?_ + refine wpTS_store ls' _ old x _ p₁ ⟨c, G, ResU.CompS.comm hp₁, hcell, ?_⟩ + intro c₃ p₃ hc₃ hp₃ + simp only [Kont.plug] + refine wpTS_1 ls' _ _ p₃ ?_ + refine wpTS_val ls' old _ p₃ ?_ + refine wpTS_val ls' (Val.pair (Val.arr ℓ n) (Val.inj₁ old)) _ p₃ ?_ + -- `p₃ = G ● c₃`: the new leaf `c₃ ● σx` beside the rest of the family, and `old`'s payload + obtain ⟨X, hX, hp₃'⟩ := compS_exch (ResU.CompS.comm hG) hp₃ + obtain ⟨L, hL, hXL⟩ := compS_reassoc hF hX + exact ⟨_, _, pure_sep_mk rfl ⟨X, ρold, hp₃', + vP_fam_mk (hback L X (ResU.CompS.comm hXL) + ⟨_, x, pure_sep_mk rfl ⟨c₃, σx, ResU.CompS.comm hL, hc₃, hx⟩⟩), + Or.inl ⟨old, pure_sep_mk rfl hold⟩⟩⟩ + · rw [he] + refine wpTS_sum₂ ls' _ _ _ _ σ ?_ + rw [replace_out] + exact wpTS_val ls' _ _ σ ⟨_, _, pure_sep_mk rfl + ⟨σ₁, σx, hc, vP_fam_mk hs, Or.inr ⟨x, pure_sep_mk rfl hx⟩⟩⟩ + +/-! ### `array_new` -/ + +/-- `array_new : !(I32 ⊗ !(I32 ⇒ A) ⇒ Array A)`. `[spec §9]` -/ +abbrev arrNewTy (A : VTy) : VTy := + .bang (.lolli (.tensor (.int .w32) (.bang (.lolli (.int .w32) A))) (.array A)) + +/-- A `!`-function is valid at every list with the same members, and applies. -/ +theorem bang_app {A B : VTy} {ls ls' : List SRec} {δ : LSub} {f v : Val} {ρv : WRes} + (hf : vP (.bang (.lolli A B)) ls δ f PMap.empty) (hls : ∀ x, x ∈ ls' ↔ x ∈ ls) + (hv : vP A ls' δ v ρv) : + wpTS ls' (.app (.val f) (.val v)) (fun ls'' => vP B ls'' δ) ρv := + hf.2 ls' (ext_of_same hls _) v ρv ρv hv (compS_empty_left ρv) + +/-- The filled cells, as leaves of `Array A`. -/ +abbrev doneP (A : VTy) (ls : List SRec) (δ : LSub) : Loc → WProp := + fun m => vP (.ref A) ls δ (.loc m) + +/-- The placeholders. -/ +abbrev todoP : Loc → WProp := fun m => ptoOwn m .unit + +theorem doneP_same {A : VTy} {ls ls' : List SRec} {δ : LSub} (hls : ∀ x, x ∈ ls' ↔ x ∈ ls) + {ℓ : Loc} {n : Nat} {ρ : WRes} (h : sepRange (doneP A ls δ) ℓ n ρ) : + sepRange (doneP A ls' δ) ℓ n ρ := + sepRange_mono' (fun _ _ h => vP_same hls h) h + +/-- **The fill.** With the cells `ℓ, …, ℓ + i − 1` filled and `ℓ + i, …, ℓ + i + k − 1` holding +`()`, the rest of `tabFill` runs to the array. `[spec §9]` -/ +theorem tabFill_wp {A : VTy} {δ : LSub} {ls₀ : List SRec} (f : Val) (ℓ : Loc) (N : UInt32) + (hf : vP (.bang (.lolli (.int .w32) A)) ls₀ δ f PMap.empty) : + ∀ (k i : Nat) (ls : List SRec) (ρd ρt ρ : WRes), (∀ x, x ∈ ls ↔ x ∈ ls₀) → + i + k = N.toNat → sepRange (doneP A ls δ) ℓ i ρd → sepRange todoP (ℓ + i) k ρt → + ResU.CompS ρd ρt ρ → wpTS ls (tabFill f ℓ N i k) (fun ls' => vP (.array A) ls' δ) ρ + | 0, i, ls, ρd, ρt, ρ, _, hN, hd, ht, hc => by + obtain ⟨rfl, -⟩ := ht + rw [ResU.CompS.functional hc (ResU.comp_empty_right ρd)] + refine wpTS_val ls (Val.arr ℓ N) _ ρd (vP_fam_mk ?_) + rw [← hN]; exact hd + | k + 1, i, ls, ρd, ρt, ρ, hls, hN, hd, ⟨c, ρt', hct, hcell, ht'⟩, hc => by + show wpTS ls (.seq (.app (.app (.val (.prim .store)) (.val (.loc (ℓ + i)))) + (.app (.val f) (.val (Val.i32 (UInt32.ofNat i))))) (tabFill f ℓ N (i + 1) k)) _ ρ + refine wpTS_bind (.seq .hole (tabFill f ℓ N (i + 1) k)) _ ls _ ρ ?_ + -- `ρ = c ● (ρd ● ρt')` + obtain ⟨R, hR, hρ⟩ := compS_lcomm hct hc + refine wpTS_bind_frame (.appR (.app (.val (.prim .store)) (.val (.loc (ℓ + i)))) .hole) + (.app (.val f) (.val (Val.i32 (UInt32.ofNat i)))) ls (fun ls' => vP A ls' δ) + (fun ls' => ptoOwn (ℓ + i) .unit ⋆ (sepRange (doneP A ls' δ) ℓ i ⋆ sepRange todoP (ℓ + i + 1) k)) + _ ?_ ?_ ρ ⟨PMap.empty, ρ, compS_empty_left ρ, + bang_app (ls' := ls) hf hls (vP_i32 _), c, R, hρ, hcell, ρd, ρt', hR, hd, ht'⟩ + · intro ls' hls' σ ⟨σ₁, σ₂, hσ, h₁, σ₃, σ₄, hσ', h₃, h₄⟩ + exact ⟨σ₁, σ₂, hσ, h₁, σ₃, σ₄, hσ', doneP_same hls' h₃, h₄⟩ + · intro ls' hls' w σ ⟨σw, σr, hσ, hw, c', R', hR', hc', σd, σt, hdt, hd', ht''⟩ + simp only [Kont.plug] + -- `σ = c' ● (σw ● R')` + obtain ⟨G, hG, hσG⟩ := compS_lcomm hR' hσ + refine wpTS_store ls' (ℓ + i) .unit w _ σ ⟨c', G, hσG, hc', ?_⟩ + intro c₃ σ₃ hc₃ hcomp₃ + refine wpTS_1 ls' _ _ σ₃ ?_ + -- the new leaf `σw ● c₃`, beside the filled cells and the rest of the placeholders + obtain ⟨L, hL, hσ₃⟩ := compS_exch hG hcomp₃ + obtain ⟨M, hM, hσ₃'⟩ := compS_lcomm hdt hσ₃ + obtain ⟨D, hD, hσ₃''⟩ := compS_reassoc' hM hσ₃' + have hleaf : doneP A ls' δ (ℓ + i) L := + ⟨ℓ + i, w, pure_sep_mk rfl ⟨c₃, σw, ResU.CompS.comm hL, hc₃, hw⟩⟩ + refine tabFill_wp f ℓ N hf k (i + 1) ls' D σt σ₃ (fun x => (hls' x).trans (hls x)) + (by rw [← hN]; ring) (sepRange_append hD hd' ⟨L, PMap.empty, ResU.comp_empty_right L, + hleaf, ⟨rfl, trivial⟩⟩) ?_ hσ₃'' + rwa [show ℓ + i + 1 = ℓ + (i + 1) by ring] at ht'' + +theorem arrNew_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {A : VTy} (hΓ : Ctx.DeadP Γ) : + SemV Δ Γ (.val (.prim .tab)) (arrNewTy A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨v₁, f, hk⟩ := hv + obtain ⟨rfl, σ₁, σ₂, hc, ⟨e₁, c, rfl⟩, hf⟩ := pure_sep_iff.mp hk + have e₂ : σ₂ = PMap.empty := hf.1 + subst e₁; subst e₂ + cases eq_empty_of_compS_empty hc + refine wpTS_tab ls' c f _ PMap.empty ?_ + intro ℓ cells x hcells hx + rw [eq_of_compS_empty_left hx] + exact tabFill_wp f ℓ (arrLen c) hf (arrLen c).toNat 0 ls' PMap.empty cells cells + (fun _ => Iff.rfl) (by simp) ⟨rfl, trivial⟩ (by simpa using hcells) (compS_empty_left cells) + +/-! ### `array_free` -/ + +/-- `array_free : !(!(A ⇒ 1) ⊗ Array A ⇒ 1)`. `[spec §9]` -/ +abbrev arrFreeTy (A : VTy) : VTy := + .bang (.lolli (.tensor (.bang (.lolli A .unit)) (.array A)) .unit) + +/-- **The drain.** The cells `ℓ + i, …, ℓ + i + k − 1` are freed in order, each value handed +to the consumer. `[spec §9]` -/ +theorem dropFill_wp {A : VTy} {δ : LSub} {ls₀ : List SRec} (f : Val) (ℓ : Loc) + (hf : vP (.bang (.lolli A .unit)) ls₀ δ f PMap.empty) : + ∀ (k i : Nat) (ls : List SRec) (ρ : WRes), (∀ x, x ∈ ls ↔ x ∈ ls₀) → + sepRange (doneP A ls δ) (ℓ + i) k ρ → + wpTS ls (dropFill f ℓ i k) (fun ls' => vP .unit ls' δ) ρ + | 0, i, ls, ρ, _, hs => by + obtain ⟨rfl, -⟩ := hs + exact wpTS_val ls .unit _ _ ⟨rfl, rfl⟩ + | k + 1, i, ls, ρ, hls, ⟨ρl, ρr, hc, hleaf, hrest⟩ => by + show wpTS ls (.seq (.app (.val f) (.app (.val (.prim .free)) (.val (.loc (ℓ + i))))) + (dropFill f ℓ (i + 1) k)) _ ρ + refine wpTS_bind (.seq .hole (dropFill f ℓ (i + 1) k)) _ ls _ ρ ?_ + refine wpTS_bind (.appR (.val f) .hole) (.app (.val (.prim .free)) (.val (.loc (ℓ + i)))) + ls _ ρ ?_ + obtain ⟨ℓ', w, hk⟩ := hleaf + obtain ⟨hl, c, ρw, hcw, hcell, hw⟩ := pure_sep_iff.mp hk + have hℓ : ℓ' = ℓ + i := by + have := congrArg Subtype.val hl; simpa using this.symm + subst hℓ + obtain ⟨R, hR, hρ⟩ := compS_reassoc hcw hc + refine wpTS_free ls _ w _ ρ ⟨c, R, hρ, hcell, ?_⟩ + simp only [Kont.plug] + refine wpTS_bind_frame .hole (.app (.val f) (.val w)) ls (fun ls' => vP .unit ls' δ) + (fun ls' => sepRange (doneP A ls' δ) (ℓ + i + 1) k) _ ?_ ?_ R + ⟨ρw, ρr, hR, bang_app hf hls hw, hrest⟩ + · intro ls' hls' σ h + exact doneP_same hls' h + · rintro ls' hls' u σ ⟨σu, σr, hσ, ⟨e, rfl⟩, hr⟩ + subst e + rw [eq_of_compS_empty_left hσ] + simp only [Kont.plug] + refine wpTS_val ls' .unit _ σr ?_ + refine wpTS_1 ls' _ _ σr ?_ + refine dropFill_wp f ℓ hf k (i + 1) ls' σr (fun x => (hls' x).trans (hls x)) ?_ + rwa [show ℓ + i + 1 = ℓ + (i + 1) by ring] at hr + +theorem arrFree_compatX {Δ : LifeCtx} {Γ : Ctx VTy} {A : VTy} (hΓ : Ctx.DeadP Γ) : + SemV Δ Γ (.val (.prim .drop)) (arrFreeTy A) := by + refine sem_const hΓ rfl fun δ ls _ => ⟨rfl, ?_⟩ + intro ls' _ v σ τ hv hcomp + rw [eq_of_compS_empty_left hcomp] + obtain ⟨f, v₂, hk⟩ := hv + obtain ⟨rfl, σ₁, σ₂, hc, hf, h₂⟩ := pure_sep_iff.mp hk + have e₁ : σ₁ = PMap.empty := hf.1 + subst e₁ + rw [eq_of_compS_empty_left hc] + obtain ⟨ℓ, n, rfl, hs⟩ := vP_fam_iff.mp h₂ + refine wpTS_dropStep ls' f ℓ n _ σ₂ ?_ + exact dropFill_wp f ℓ hf n.toNat 0 ls' σ₂ (fun _ => Iff.rfl) (by simpa using hs) + +end BoCa.Fig16.LogRel.Core + +end diff --git a/Core/Dtor.lean b/Core/Dtor.lean index 1db735a0e..445d1d3af 100644 --- a/Core/Dtor.lean +++ b/Core/Dtor.lean @@ -18,6 +18,8 @@ value goes unused (`drop_derives`), so the elaborated term uses the value exactl | `μX. T`, `T` in `FShape` | `foldOwn` whose algebra is the destructor of `T[1/X]` | | `U(C & F 1)` | `close`: force, then the second projection | | `U(νX. C & F R)`, `R` closed | `close`, then the destructor of the residual `R` | +| `Array A` | `array_free` with the destructor of `A` as the consumer | +| `ArrayImm @a A` | `array_forget` | The first two rows are the forgettable types, whose destructor does nothing; every type of `Affine` (`Core/Records.lean`) is one (`hasDtor_of_affine`). The other rows deallocate or run @@ -78,6 +80,7 @@ theorem toFShape_ty : ∀ A : VTy, (toFShape A).ty = A | .ref .unk => rfl | .ref (.mu _) => rfl | .ref (.int _) => rfl + | .ref (.fam _) => rfl | .imm _ _ => rfl | .mut _ _ => rfl | .box _ _ => rfl @@ -85,6 +88,7 @@ theorem toFShape_ty : ∀ A : VTy, (toFShape A).ty = A | .unk => rfl | .mu _ => rfl | .int _ => rfl + | .fam _ => rfl /-! ### The destructors -/ @@ -107,6 +111,8 @@ def dtorAt : Bool → VTy → Expr | _, .thunk (.nu (.amp _ (.ret R))) => .val (.lam (bindE (.app (.var 0) (.val (.inj₂ .unit))) (.app (dtorAt false R) (.var 0)))) | _, .thunk _ => .val (.lam (.app (.var 0) (.val (.inj₂ .unit)))) + | _, .fam (.ref A) => .val (.lam (.app (.val (.prim .drop)) + (.pair (.val (.lam (.app (dtorAt false A) (.var 0)))) (.var 0)))) | _, _ => forget /-- At a thunk the destructor does not depend on the mode. -/ @@ -146,6 +152,7 @@ theorem algE_eq : ∀ A : VTy, algE A = dtorE ((toFShape A).resO .unit) | .ref .unk => rfl | .ref (.mu _) => rfl | .ref (.int _) => rfl + | .ref (.fam _) => rfl | .imm _ _ => rfl | .mut _ _ => rfl | .box _ _ => rfl @@ -153,6 +160,7 @@ theorem algE_eq : ∀ A : VTy, algE A = dtorE ((toFShape A).resO .unit) | .unk => rfl | .mu _ => rfl | .int _ => rfl + | .fam L => by cases L <;> rfl /-! ### Which types have one -/ @@ -179,6 +187,11 @@ inductive HasDtor : VTy → Prop where `close`, then the destructor of `R`. `[spec §7, §12]` -/ | nu {C R} (hC : (CTy.amp C (.ret R)).NuOk) (hRc : R.Cl 0) (h : HasDtor R) : HasDtor (.thunk (.nu (.amp C (.ret R)))) + /-- An array: each element to the destructor of `A`, then the cells freed (`array_free`). + `[spec §9, §12]` -/ + | array {A} (h : HasDtor A) : HasDtor (.fam (.ref A)) + /-- A family of leaf views, forgotten (`array_forget`). `[spec §9, §12]` -/ + | arrayImm {a A} : HasDtor (.fam (.imm a A)) theorem hasDtor_of_affine {A : VTy} (h : Affine A) : HasDtor A := by induction h with @@ -265,6 +278,16 @@ theorem dtor_wf {Δ : LifeCtx} {T : VTy} (hT : HasDtor T) : exact .thunkV rfl (.lam (.bind (.left (deadSplit hΓ)) hM (.ret (.lolliE (.left (.dead (deadSplit hΓ))) (.var (.here (Ctx.Dead.cons hΓ).toP)) (ih (.cons (.cons hΓ))))))) + | array _ ih => + intro Γ hΓ + exact .lamV (.lolliE (.left (deadSplit hΓ)) + (.tensorI (.right (deadSplit hΓ)) + (.bangThunkV (dead_noLive (Ctx.Dead.cons hΓ)) rfl + (.lam (.ret (.lolliE (.left (.dead (deadSplit hΓ))) + (.var (.here (Ctx.Dead.cons hΓ).toP)) (ih (.cons (.cons hΓ))))))) + (.var (.here hΓ.toP))) + (.bangE (.arrFree _ (Ctx.Dead.cons hΓ).toP))) + | arrayImm => intro Γ hΓ; exact .bangE (.arrForget _ _ hΓ.toP) /-! ### A borrow's destructor frees nothing -/ diff --git a/Core/FrameRecords.lean b/Core/FrameRecords.lean index dab0455ce..0e7eece37 100644 --- a/Core/FrameRecords.lean +++ b/Core/FrameRecords.lean @@ -58,6 +58,9 @@ inductive RefPos : VTy → Val → Loc → VTy → Prop /-- A closed `μX. A` is read at its unrolling `A[μX. A/X]`. -/ | mu {A : VTy} {v : Val} {l : Loc} {S : VTy} : (VTy.mu A).Cl 0 → RefPos (VTy.inst (.mu A) A) v l S → RefPos (.mu A) v l S + /-- The leaf at `ℓ + i`, `i < n`, of a family `(ℓ, n)`. `[spec §9]` -/ + | fam {L : VTy} {ℓ : Loc} {c : UInt32} {i : Nat} {l : Loc} {S : VTy} : i < c.toNat → + RefPos L (Val.loc (ℓ + i)) l S → RefPos (.fam L) (Val.arr ℓ c) l S /-- `Chain R T v p ℓ S u`: `ℓ ↦ own(u)` is in `R`, reached from `(T, v)` by a chain of `RefPos` steps whose intermediate values are read off `R`; `S` is `ℓ`'s pointee type and `p` @@ -90,6 +93,21 @@ inductive MutPos : VTy → Val → Loc → VTy → Prop /-- A closed `μX. A` is read at its unrolling `A[μX. A/X]`. -/ | mu {A : VTy} {v : Val} {l : Loc} {S : VTy} : (VTy.mu A).Cl 0 → MutPos (VTy.inst (.mu A) A) v l S → MutPos (.mu A) v l S + /-- The leaf at `ℓ + i`, `i < n`, of a family `(ℓ, n)`. `[spec §9]` -/ + | fam {L : VTy} {ℓ : Loc} {c : UInt32} {i : Nat} {l : Loc} {S : VTy} : i < c.toNat → + MutPos L (Val.loc (ℓ + i)) l S → MutPos (.fam L) (Val.arr ℓ c) l S + +/-- A leaf of a family, named by its location. `[spec §9]` -/ +theorem RefPos.fam_at {L S : VTy} {ℓ : Nat} {n : UInt32} {m : Nat} {l : Loc} (h₁ : ℓ ≤ m) + (h₂ : m < ℓ + n.toNat) (h : RefPos L (Val.loc m) l S) : RefPos (.fam L) (Val.arr ℓ n) l S := by + obtain ⟨i, rfl⟩ : ∃ i, m = ℓ + i := ⟨m - ℓ, by omega⟩ + exact RefPos.fam (by omega) h + +/-- A leaf of a family, named by its location. `[spec §9]` -/ +theorem MutPos.fam_at {L S : VTy} {ℓ : Nat} {n : UInt32} {m : Nat} {l : Loc} (h₁ : ℓ ≤ m) + (h₂ : m < ℓ + n.toNat) (h : MutPos L (Val.loc m) l S) : MutPos (.fam L) (Val.arr ℓ n) l S := by + obtain ⟨i, rfl⟩ : ∃ i, m = ℓ + i := ⟨m - ℓ, by omega⟩ + exact MutPos.fam (by omega) h /-- `δ′` agrees with `δ` on the free lifetime variables of `T`. -/ def AgreeOn (T : VTy) (δ' δ : LSub) : Prop := ∀ y, VFree y T → δ'.find? y = δ.find? y @@ -189,6 +207,7 @@ def TyEq : VTy → LSub → VTy → LSub → Prop | .mut a A, δ, .mut a' A', δ' => a.interp δ = a'.interp δ' ∧ TyEq A δ A' δ' | .tvar n, _, .tvar n', _ => n = n' | .mu A, δ, .mu A', δ' => TyEq A δ A' δ' + | .fam A, δ, .fam A', δ' => TyEq A δ A' δ' | _, _, _, _ => False /-- `C` at `δ` and `C′` at `δ′` are one computation type. `[about ours]` -/ def CTyEq : CTy → LSub → CTy → LSub → Prop diff --git a/Core/Fundamental.lean b/Core/Fundamental.lean index 470b87fa4..33e7565e7 100644 --- a/Core/Fundamental.lean +++ b/Core/Fundamental.lean @@ -1,5 +1,6 @@ import Core.Compat import Core.Integers +import Core.Arrays import Core.FoldReuse import Core.Nu import Support.LogicalRelation.Adequacy @@ -153,6 +154,29 @@ inductive CDerives : LifeCtx → Ctx VTy → Expr → Jdg → Prop where /-- A host operation at `!(A ⊸ F B)`, its signature in `SPEC.md` §8. `[spec §8]` -/ | hostOp {Δ Γ} (op : HostOp) (hΓ : Ctx.DeadP Γ) : CDerives Δ Γ (.val (.prim (.host op))) (.val (.bang (hostSig op))) + /-- `array_new : !(I32 ⊗ !(I32 ⇒ A) ⇒ Array A)`, the step `tab`. `[spec §9]` -/ + | arrNew {Δ Γ} (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val (.prim .tab)) (.val (arrNewTy A)) + /-- `array_free : !(!(A ⇒ 1) ⊗ Array A ⇒ 1)`, the step `drop`. `[spec §9]` -/ + | arrFree {Δ Γ} (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val (.prim .drop)) (.val (arrFreeTy A)) + /-- `array_size : !(Array A ⇒ Array A ⊗ I32)`. `[spec §9]` -/ + | arrSize {Δ Γ} (A : VTy) (hΓ : Ctx.DeadP Γ) : CDerives Δ Γ (.val sizeV) (.val (arrSizeTy A)) + /-- `array_replace : !(Array A ⊗ (I32 ⊗ A) ⇒ Array A ⊗ (A ⊕ A))`. `[spec §9]` -/ + | arrReplace {Δ Γ} (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val replaceV) (.val (arrReplaceTy A)) + /-- `array_len : !(ArrayImm @a A ⇒ I32)`. `[spec §9]` -/ + | arrLen {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val arrLenV) (.val (arrLenTy a A)) + /-- `array_get : !(ArrayImm @a A ⊗ I32 ⇒ Imm @a A ⊕ 1)`, the step `idx`. `[spec §9]` -/ + | arrGet {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val (.prim .idx)) (.val (arrGetTy a A)) + /-- `array_copy : !(ArrayImm @a A ⇒ ArrayImm @a A ⊗ ArrayImm @a A)`. `[spec §9]` -/ + | arrCopy {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val copyV) (.val (arrCopyTy a A)) + /-- `array_forget : !(ArrayImm @a A ⇒ 1)`. `[spec §9]` -/ + | arrForget {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : Ctx.DeadP Γ) : + CDerives Δ Γ (.val forgetV) (.val (arrForgetTy a A)) | withbor1 {Δ Γ A B} (x : LifeVar) (hΓ : Ctx.DeadP Γ) (hx : Δ.find? x = none) (hsc₁ : VScoped Δ A) (hfree₁ : ¬ VFree x A) (hfree₂ : ¬ VFree x B) : CDerives Δ Γ withbor (.val (axWithbor1Ty x Δ.meetOfDom A B)) @@ -319,6 +343,14 @@ theorem fundamentalC {Δ : LifeCtx} {Γ : Ctx VTy} {e : Expr} {j : Jdg} | forgetUnk hΓ => exact forgetUnk_compatX hΓ | lit c hΓ => exact lit_compatX c hΓ | hostOp op hΓ => exact hostOp_compatX op hΓ + | arrNew _ hΓ => exact arrNew_compatX hΓ + | arrFree _ hΓ => exact arrFree_compatX hΓ + | arrSize _ hΓ => exact arrSize_compatX hΓ + | arrReplace _ hΓ => exact arrReplace_compatX hΓ + | arrLen _ _ hΓ => exact arrLen_compatX hΓ + | arrGet _ _ hΓ => exact arrGet_compatX hΓ + | arrCopy _ _ hΓ => exact arrCopy_compatX hΓ + | arrForget _ _ hΓ => exact arrForget_compatX hΓ | withbor1 x hΓ hx hsc₁ hf₁ hf₂ => exact withbor1_compatX x hΓ hx hsc₁ hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact withbor2_compatX x hΓ hx hside hf₁ hf₂ | withbor3 x hΓ hx hf₁ hf₂ => exact withbor3_compatX x hΓ hx hf₁ hf₂ diff --git a/Core/Images.lean b/Core/Images.lean index 3971e4894..45d1a44bf 100644 --- a/Core/Images.lean +++ b/Core/Images.lean @@ -345,6 +345,7 @@ theorem RefPos.not_free {T : VTy} {v : Val} {l : Loc} {S : VTy} (h : RefPos T v | sumR _ ih => exact ih (fun h => hx (Or.inr h)) | box _ ih => exact ih (fun h => hx (Or.inr h)) | mu _ _ ih => exact ih (fun h => (vFree_inst h).elim hx hx) + | fam _ _ ih => exact ih hx theorem Val.pair_inj' {a b c d : Val} (h : Val.pair a b = Val.pair c d) : a = c ∧ b = d := by have hv := congrArg Subtype.val h @@ -444,6 +445,15 @@ theorem image_at_pos {T : VTy} {v : Val} {l : Loc} {S : VTy} (hp : RefPos T v l intro c hc exact ih (fun h => (vFree_inst h).elim hx hx) ((vShape_immReborrow_mu_iff (show A.Cl 1 from hcl) _ _ v c).mp hc) + | @fam L ℓ n i l S hi _ ih => + intro c hc + obtain ⟨ℓ', n', hk⟩ := hc + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨rfl, rfl⟩ := Val.arr_inj he + obtain ⟨ρi, hPi, τ, hτ⟩ := sepRange_le hi hs + obtain ⟨ψ, e, k, d⟩ := ih hx hPi + obtain ⟨ψ', e', k', w', r'⟩ := compS_get_left' hτ e + exact ⟨ψ', e', k'.trans k, by rw [w', r']; exact d⟩ /-- `typed_image_pos`: 6.131's `Ref T′` case against `reb_β`'s `own` clause. At a root `ℓ ↦ own(u)` of `σ` (`RefPos T v ℓ S`), a typed reborrow `c ∈ reb_β(σ) ∩ 𝒱⟦Imm̲ 'x T⟧` has @@ -792,6 +802,18 @@ theorem image_dom {x : LifeVar} {δ δ' : LSub} : obtain ⟨hcl, hG⟩ := hR obtain ⟨ζ, eζ, hζ⟩ := hG.2 ((vShape_immReborrow_mu_iff (show A.Cl 1 from hcl) _ _ v c).mp hc) e exact ⟨ζ, eζ, fun k => let ⟨S, hS⟩ := hζ k; ⟨S, RefPos.mu hcl hS⟩⟩ + | fam L => + obtain ⟨ℓ, n, hk⟩ := hc + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨ℓ', n', hk'⟩ := hR + obtain ⟨he', hs'⟩ := pure_sep_iff.mp hk' + subst he + obtain ⟨rfl, rfl⟩ := Val.arr_inj he' + obtain ⟨i, hi, ρi, ψi, hPi, ei, -⟩ := sepRange_get hs e + obtain ⟨Ri, hRi, τ, hτ⟩ := sepRange_le hi hs' + obtain ⟨ζ₁, eζ₁, hζ₁⟩ := hRi.2 hPi ei + obtain ⟨ζ, eζ, kζ, -, -⟩ := compS_get_left' hτ eζ₁ + exact ⟨ζ, eζ, fun k => let ⟨S, hS⟩ := hζ₁ (kζ ▸ k); ⟨S, RefPos.fam hi hS⟩⟩ open Classical in /-- The cell `reb_β`'s `own`/`mut` clauses would put at `m` over the same value and witness, @@ -1085,6 +1107,11 @@ theorem RefPos.inv_mu {A S : VTy} {v : Val} {l : Loc} (h : RefPos (.mu A) v l S) cases h with | mu hc h => exact ⟨hc, h⟩ +theorem RefPos.inv_fam {L S : VTy} {v : Val} {l : Loc} (h : RefPos (.fam L) v l S) : + ∃ ℓ n j, v = Val.arr ℓ n ∧ j < n.toNat ∧ RefPos L (Val.loc (ℓ + j)) l S := by + cases h with + | fam hj h => exact ⟨_, _, _, rfl, hj, h⟩ + theorem own_of_le {B R : WRes} (h : ResU.Le B R) {l : Loc} {u : Val} (e : B.get l = some (CellU.ownOf u)) : R.get l = some (CellU.ownOf u) := by obtain ⟨τ, hτ⟩ := h @@ -1155,6 +1182,13 @@ theorem own_split {δ : LSub} : obtain ⟨α, -, hb, -⟩ := hR exact ih hb | mu _ _ ih => exact ih (vShape_unfold hR).2 + | fam hi _ ih => + obtain ⟨ℓ', n', hk⟩ := hR + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨rfl, rfl⟩ := Val.arr_inj he + obtain ⟨Ri, hRi, hle⟩ := sepRange_le hi hs + obtain ⟨u, P, B, hB, hP, hle'⟩ := ih hRi + exact ⟨u, P, B, hB, hP, hle'.trans hle⟩ /-- Two roots of a typed escrow are one position, or their blocks compose strictly. The `⊗` clause's `⋆` is `●` (`[TR]` p. 6), and `●` composes no two `own` cells. -/ @@ -1225,6 +1259,28 @@ theorem refPos_blocks {δ : LSub} : obtain ⟨α, -, hb, -⟩ := hR exact ih hb (RefPos.inv_box h₂) | mu _ _ ih => exact ih (vShape_unfold hR).2 (RefPos.inv_mu h₂).2 + | @fam L ℓ n i l S hi h₁' ih => + obtain ⟨ℓ₂, n₂, j, he₂, hj, h₂'⟩ := RefPos.inv_fam h₂ + obtain ⟨rfl, rfl⟩ := Val.arr_inj he₂ + obtain ⟨ℓ', n', hk⟩ := hR + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨rfl, rfl⟩ := Val.arr_inj he + rcases lt_trichotomy i j with hij | rfl | hij + · obtain ⟨Ri, Rj, X, hRi, hRj, hX, hle⟩ := sepRange_pick2 hij hj hs + obtain ⟨u₁, P₁, B₁, a₁, b₁, ⟨Y₁, hY₁⟩⟩ := own_split L hRi h₁' + obtain ⟨u₂, P₂, B₂, a₂, b₂, ⟨Y₂, hY₂⟩⟩ := own_split L hRj h₂' + obtain ⟨X', hX', hle'⟩ := sub_comp hY₁ hY₂ hX + exact Or.inr ⟨u₁, u₂, P₁, P₂, B₁, B₂, X', a₁, b₁, a₂, b₂, hX', hle'.trans hle⟩ + · obtain ⟨Ri, hRi, hle⟩ := sepRange_le hi hs + rcases ih hRi h₂' with h | ⟨u₁, u₂, P₁, P₂, B₁, B₂, X, a₁, b₁, a₂, b₂, hX, hle'⟩ + · exact Or.inl h + · exact Or.inr ⟨u₁, u₂, P₁, P₂, B₁, B₂, X, a₁, b₁, a₂, b₂, hX, hle'.trans hle⟩ + · obtain ⟨Rj, Ri, X, hRj, hRi, hX, hle⟩ := sepRange_pick2 hij hi hs + obtain ⟨u₁, P₁, B₁, a₁, b₁, ⟨Y₁, hY₁⟩⟩ := own_split L hRi h₁' + obtain ⟨u₂, P₂, B₂, a₂, b₂, ⟨Y₂, hY₂⟩⟩ := own_split L hRj h₂' + obtain ⟨X', hX', hle'⟩ := sub_comp hY₂ hY₁ hX + exact Or.inr ⟨u₁, u₂, P₁, P₂, B₁, B₂, X', a₁, b₁, a₂, b₂, ResU.CompS.comm hX', + hle'.trans hle⟩ theorem Chain.own {R : WRes} {T : VTy} {v : Val} {p : Option Loc} {l : Loc} {S : VTy} {u : Val} (h : Chain R T v p l S u) : R.get l = some (CellU.ownOf u) := by diff --git a/Core/Invariant.lean b/Core/Invariant.lean index b81f38845..575ec1655 100644 --- a/Core/Invariant.lean +++ b/Core/Invariant.lean @@ -843,6 +843,15 @@ theorem mut_split {δ : LSub} {T : VTy} {v : Val} {m : Loc} {S : VTy} (hp : MutP | mu _ _ ih => intro R hR exact ih (vShape_unfold hR).2 + | fam hi _ ih => + intro R hR + obtain ⟨ℓ', n', hk'⟩ := hR + obtain ⟨he', hs⟩ := pure_sep_iff.mp hk' + obtain ⟨rfl, rfl⟩ := Val.arr_inj he' + obtain ⟨Ri, hRi, τ, hτ⟩ := sepRange_le hi hs + obtain ⟨ζ, eζ, kζ⟩ := ih hRi + obtain ⟨ζ', eζ', kζ', -, -⟩ := compS_get_left' hτ eζ + exact ⟨ζ', eζ', kζ'.trans kζ⟩ /-- A typed reborrow's cell over a `mut` cell sits at a `Mut` position — the `Mut @a S` clause of `Imm̲` (`[CONF]` Fig. 9, `[TR]` 6.122), carried up through `⊗`, `⊕` and `[@a]`. @@ -944,6 +953,20 @@ theorem image_dom_mut {x : LifeVar} {δ δ' : LSub} : obtain ⟨S, hS⟩ := hG.2 ((vShape_immReborrow_mu_iff (show A.Cl 1 from hcl) _ _ v c).mp hc) e eζ kζ exact ⟨S, MutPos.mu hcl hS⟩ + | fam L => + obtain ⟨ℓ, n, hk⟩ := hc + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨ℓ', n', hk'⟩ := hR + obtain ⟨he', hs'⟩ := pure_sep_iff.mp hk' + subst he + obtain ⟨rfl, rfl⟩ := Val.arr_inj he' + obtain ⟨i, hi, ρi, ψi, hPi, ei, -⟩ := sepRange_get hs e + obtain ⟨Ri, hRi, τ, hτ⟩ := sepRange_le hi hs' + obtain ⟨ζ₁, eζ₁, -⟩ := image_dom L hPi hRi.1 ei + obtain ⟨ζ', eζ', kζ', -, -⟩ := compS_get_left' hτ eζ₁ + rw [eζ] at eζ'; cases Option.some.inj eζ' + obtain ⟨S, hS⟩ := hRi.2 hPi ei eζ₁ (kζ'.symm.trans kζ) + exact ⟨S, MutPos.fam hi hS⟩ /-- A cell of a valid world that is neither `imm` nor absent at the top keeps `ag` off its location. -/ @@ -1276,6 +1299,11 @@ theorem MutPos.inv_mu {A S : VTy} {v : Val} {l : Loc} (h : MutPos (.mu A) v l S) cases h with | mu hc h => exact ⟨hc, h⟩ +theorem MutPos.inv_fam {L S : VTy} {v : Val} {l : Loc} (h : MutPos (.fam L) v l S) : + ∃ ℓ n j, v = Val.arr ℓ n ∧ j < n.toNat ∧ MutPos L (Val.loc (ℓ + j)) l S := by + cases h with + | fam hj h => exact ⟨_, _, _, rfl, hj, h⟩ + theorem MutPos.free {T : VTy} {v : Val} {l : Loc} {S : VTy} (h : MutPos T v l S) {y : LifeVar} (hy : VFree y S) : VFree y T := by induction h with @@ -1286,6 +1314,7 @@ theorem MutPos.free {T : VTy} {v : Val} {l : Loc} {S : VTy} (h : MutPos T v l S) | sumR _ ih => exact Or.inr (ih hy) | box _ ih => exact Or.inr (ih hy) | mu _ _ ih => exact (vFree_inst (ih hy)).elim id id + | fam _ _ ih => exact ih hy /-- At a `Mut @a S` position a typed escrow holds `ℓ ↦ mut(b, u, χ, Q̂)` with `χ ∈ 𝒱⟦S⟧δ(u)` — `ptoMut`'s `ofS Q̂ = 𝒱⟦S⟧δ` read at the cell's own value and witness. -/ @@ -1341,6 +1370,14 @@ theorem mut_split_den {δ : LSub} {T : VTy} {v : Val} {m : Loc} {S : VTy} (hp : | mu _ _ ih => intro R hR exact ih (vShape_unfold hR).2 + | fam hi _ ih => + intro R hR + obtain ⟨ℓ', n', hk'⟩ := hR + obtain ⟨he', hs⟩ := pure_sep_iff.mp hk' + obtain ⟨rfl, rfl⟩ := Val.arr_inj he' + obtain ⟨Ri, hRi, τ, hτ⟩ := sepRange_le hi hs + obtain ⟨ζ, eζ, kζ, dζ⟩ := ih hRi + exact ⟨ζ, (ResU.CompS.get_left_of_ne_imm hτ eζ (by rw [kζ]; simp)).2, kζ, dζ⟩ theorem not_mut_both {B₁ B₂ X : WRes} (h : ResU.CompS B₁ B₂ X) {l : Loc} {ψ₁ ψ₂ : CellU Loc Val} (e₁ : B₁.get l = some ψ₁) (k₁ : ψ₁.kind ≠ Kind.imm) @@ -1401,6 +1438,23 @@ theorem mutPos_unique {δ : LSub} : obtain ⟨α, -, hb, -⟩ := hR exact ih hb (MutPos.inv_box h₂) | mu _ _ ih => exact ih (vShape_unfold hR).2 (MutPos.inv_mu h₂).2 + | @fam L ℓ n i l S hi h₁' ih => + obtain ⟨ℓ₂, n₂, j, he₂, hj, h₂'⟩ := MutPos.inv_fam h₂ + obtain ⟨rfl, rfl⟩ := Val.arr_inj he₂ + obtain ⟨ℓ', n', hk⟩ := hR + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨rfl, rfl⟩ := Val.arr_inj he + rcases lt_trichotomy i j with hij | rfl | hij + · obtain ⟨Ri, Rj, X, hRi, hRj, hX, -⟩ := sepRange_pick2 hij hj hs + obtain ⟨ζ₁, e₁, k₁⟩ := mut_split h₁' hRi + obtain ⟨ζ₂, e₂, -⟩ := mut_split h₂' hRj + exact (not_mut_both hX e₁ (by rw [k₁]; simp) e₂).elim + · obtain ⟨Ri, hRi, -⟩ := sepRange_le hi hs + exact ih hRi h₂' + · obtain ⟨Rj, Ri, X, hRj, hRi, hX, -⟩ := sepRange_pick2 hij hi hs + obtain ⟨ζ₁, e₁, -⟩ := mut_split h₁' hRi + obtain ⟨ζ₂, e₂, k₂⟩ := mut_split h₂' hRj + exact (not_mut_both hX e₂ (by rw [k₂]; simp) e₁).elim /-- A `Mut` position and a `Ref` position of a typed escrow have strictly composable blocks: the `mut` cell, and the `Ref`'s `ℓ ↦ own(u) ● Q` with `Q ∈ 𝒱⟦S⟧δ(u)`. -/ @@ -1463,6 +1517,27 @@ theorem mut_ref_blocks {δ : LSub} : obtain ⟨α, -, hb, -⟩ := hR exact ih hb (RefPos.inv_box h₂) | mu _ _ ih => exact ih (vShape_unfold hR).2 (RefPos.inv_mu h₂).2 + | @fam L ℓ n i m₀ S hi h₁' ih => + obtain ⟨ℓ₂, n₂, j, he₂, hj, h₂'⟩ := RefPos.inv_fam h₂ + obtain ⟨rfl, rfl⟩ := Val.arr_inj he₂ + obtain ⟨ℓ', n', hk⟩ := hR + obtain ⟨he, hs⟩ := pure_sep_iff.mp hk + obtain ⟨rfl, rfl⟩ := Val.arr_inj he + rcases lt_trichotomy i j with hij | rfl | hij + · obtain ⟨Ri, Rj, X, hRi, hRj, hX, hle⟩ := sepRange_pick2 hij hj hs + obtain ⟨u, Q, B₂, hB, hQ, ⟨Y₂, hY₂⟩⟩ := own_split L hRj h₂' + obtain ⟨Y₁, hY₁⟩ := ResU.Le.refl Ri + obtain ⟨X', hX', hle'⟩ := sub_comp hY₁ hY₂ hX + exact ⟨Ri, B₂, X', u, Q, mut_split h₁' hRi, hB, hQ, hX', hle'.trans hle⟩ + · obtain ⟨Ri, hRi, hle⟩ := sepRange_le hi hs + obtain ⟨B₁, B₂, X, u, Q, hm, hB, hQ, hX, hle'⟩ := ih hRi h₂' + exact ⟨B₁, B₂, X, u, Q, hm, hB, hQ, hX, hle'.trans hle⟩ + · obtain ⟨Rj, Ri, X, hRj, hRi, hX, hle⟩ := sepRange_pick2 hij hi hs + obtain ⟨u, Q, B₂, hB, hQ, ⟨Y₂, hY₂⟩⟩ := own_split L hRj h₂' + obtain ⟨Y₁, hY₁⟩ := ResU.Le.refl Ri + obtain ⟨X', hX', hle'⟩ := sub_comp hY₂ hY₁ hX + exact ⟨Ri, B₂, X', u, Q, mut_split h₁' hRi, hB, hQ, ResU.CompS.comm hX', + hle'.trans hle⟩ theorem GPos.of_chain {R : WRes} {T : VTy} {v : Val} {p : Option Loc} {l₀ : Loc} {S₀ : VTy} {u₀ : Val} (h : Chain R T v p l₀ S₀ u₀) {m : Loc} {S : VTy} (hm : MutPos S₀ u₀ m S) : diff --git a/Core/Nu.lean b/Core/Nu.lean index 5843bfb92..383509557 100644 --- a/Core/Nu.lean +++ b/Core/Nu.lean @@ -63,7 +63,7 @@ theorem mapV_closed : ∀ A : VTy, closedAt 0 (mapV A).1 = true simp [mapV, closedAt, closedAt_mono _ (mapV_closed A) (Nat.zero_le _), closedAt_mono _ (mapV_closed B) (Nat.zero_le _)] | .tvar (_ + 1) | .unit | .zero | .thunk _ | .ref _ | .imm _ _ | .mut _ _ | .box _ _ - | .bang _ | .unk | .mu _ | .int _ => rfl + | .bang _ | .unk | .mu _ | .int _ | .fam _ => rfl /-- `λ_. c () o`: the step's computation at the child `o` of the node `c` runs. -/ def childV (c o : Val) : Val := .lam (.app (.app (.val (c.shift 1 0)) (.val .unit)) (.val o)) @@ -327,7 +327,7 @@ theorem map_ok (C : CTy) (g : Val) (S : VTy) (δ : LSub) : | .unit, hP, _, w, _, hw | .zero, hP, _, w, _, hw | .thunk _, hP, _, w, _, hw | .ref _, hP, _, w, _, hw | .imm _ _, hP, _, w, _, hw | .mut _ _, hP, _, w, _, hw | .box _ _, hP, _, w, _, hw | .bang _, hP, _, w, _, hw | .unk, hP, _, w, _, hw - | .mu _, hP, _, w, _, hw | .int _, hP, _, w, _, hw => + | .mu _, hP, _, w, _, hw | .int _, hP, _, w, _, hw | .fam _, hP, _, w, _, hw => ⟨w, mapV_id_steps rfl _ w, by rw [← vCE_envS_nuEnv hP]; exact hw⟩ /-- **The node lemma**: when `c ()` runs a computation of `D[S/X]`, `node_D (unfold step) c` is diff --git a/Core/Persistent.lean b/Core/Persistent.lean index 90e0f75ff..ee43267a8 100644 --- a/Core/Persistent.lean +++ b/Core/Persistent.lean @@ -30,8 +30,8 @@ open BoCa.Lifetime (LSub LifeCtx LifeVar) /-- A value type whose values hold no resource: `1`, `0`, `Unk`, `I32`, `I64`, `!A`, and `⊗`, `⊕`, `[@a]` and `μ` over persistent types, with a type variable persistent (it stands, under its -`μ`, for a persistent type). `U C`, `Ref A`, `Imm @a A` and `Mut @a A` are not. -`[spec §10, §8]` -/ +`μ`, for a persistent type). `U C`, `Ref A`, `Imm @a A`, `Mut @a A` and `[L]` are not. +`[spec §10, §8, §9]` -/ def VTy.persistent : VTy → Bool | .unit => true | .zero => true @@ -47,6 +47,7 @@ def VTy.persistent : VTy → Bool | .ref _ => false | .imm _ _ => false | .mut _ _ => false + | .fam _ => false /-- `A` is persistent. `[spec §10]` -/ abbrev VTy.Persistent (A : VTy) : Prop := A.persistent = true diff --git a/Core/README.md b/Core/README.md index b224d7602..7ae316bea 100644 --- a/Core/README.md +++ b/Core/README.md @@ -1,8 +1,8 @@ # Core: the consolidated world One CBPV typed world holding `μ` and the three folds, blocks, records and variants, family -borrows, block borrows, the delay type with `fixD`, the stream type with `unfoldS`, and the -integer types with the host operations. It is the library's implementation of `SPEC.md` §3–§8 +borrows, block borrows, the delay type with `fixD`, the stream type with `unfoldS`, the +integer types with the host operations, and arrays. It is the library's implementation of `SPEC.md` §3–§8 and §10; code an entry adds is tagged `[spec §N]` (each file's heading, and the main definitions, rules and theorems). @@ -19,7 +19,7 @@ axioms among `[propext, Classical.choice, Quot.sound]` (`scripts/CheckAxioms.lea ### Types A ::= 1 | A ⊗ A | 0 | A ⊕ A | U C | Ref A | Imm @a A | Mut @a A | [@a] A | !A | Unk | X | μX. A - | I32 | I64 + | I32 | I64 | [A] C ::= F A | A ⊸ C | C & C | ⊤ | ∀'a ⊏ @b. C | νX. C `I32` and `I64` are one constructor, `VTy.int w` (`Integers.lean`, `[spec §8]`). The relation @@ -76,6 +76,24 @@ Typing: `CDerives.lit c : I` at a consumed context, and one rule `CDerives.hostO operation sound by one host step, using only that `Host.call` answers at the stated type (`host_call_sound`). +### Arrays `[spec §9]` + +`[L]` (`VTy.fam L`, `Types.lean`) is the run-time family `⊛_{i reborrow_pure α True ρ hs + | _, _ + 1, ρ, ⟨ρ₁, ρ₂, hc, h₁, h₂⟩, ho => by + obtain ⟨o₁, o₂⟩ := (outlives_comp hc α).mp ho + exact BoLo.reborrow_star α _ _ ρ ⟨ρ₁, ρ₂, hc, h _ _ h₁ o₁, reborrow_sepRange h h₂ o₂⟩ + /-! ### Lemma 6.131 (↺V₁) at the typed world; record in `Paper/S6_6_ReborrowingEntailments/Lemmas.lean` -/ /-- `[TR]` Lemma 6.131 (`↺V₁`, p. 34) at the shape relation: `[α] vShape⟦T⟧δ(v) ⊨ ↺_α vShape⟦Imm̲ 'a T⟧δ(v)` where `δ('a) = α`, at every `T`. @@ -111,6 +122,12 @@ theorem reborrow_vShape {α : Life} {x : LifeVar} {δ : LSub} | unk => exact reborrow_emp_box α _ ρ hρ | tvar _ => exact hρ.1.elim + | fam L => + obtain ⟨ℓ, n, σ₀, σ, hc, hp, hs⟩ := h + obtain ⟨-, o₂⟩ := (outlives_comp hc α).mp hout + obtain ⟨χ, hr, hχ⟩ := reborrow_star_pure α _ _ ρ ⟨σ₀, σ, hc, hp, + reborrow_sepRange (fun m σ' hm ho => hm.2 ho) hs o₂⟩ + exact ⟨χ, hr, ℓ, n, hχ⟩ | mu A => obtain ⟨hcl, hG⟩ := h exact reborrow_mono (fun σ hσ => (vShape_immReborrow_mu_iff (show A.Cl 1 from hcl) _ δ v σ).mpr hσ) diff --git a/Core/Records.lean b/Core/Records.lean index 3f0552d46..2c331918a 100644 --- a/Core/Records.lean +++ b/Core/Records.lean @@ -332,13 +332,14 @@ theorem dropT_wf {Δ : LifeCtx} {A : VTy} (hA : Affine A) : | bang => intro Γ hΓ; exact .forgetBang hΓ | int => intro Γ hΓ; exact .lamV (.unitI (.drop rfl hΓ.toP)) -/-- The spine of `⊗`, `⊕` and `[@a]` holds no `μ` and no type variable. -/ +/-- The spine of `⊗`, `⊕` and `[@a]` holds no `μ`, no type variable and no array. -/ def VTy.SpineMuFree : VTy → Prop | .tensor A B => A.SpineMuFree ∧ B.SpineMuFree | .sum A B => A.SpineMuFree ∧ B.SpineMuFree | .box _ A => A.SpineMuFree | .tvar _ => False | .mu _ => False + | .fam _ => False | _ => True /-- `Imm̲ 'b A` is affine at a `μ`-free spine: `Imm̲` sends `Ref` and `Mut` to `Imm`, `U C` to @@ -357,6 +358,7 @@ theorem affine_immReborrow (b : Lifetime.Life) : ∀ A : VTy, A.SpineMuFree → | .box _ A, h => affine_immReborrow b A h | .bang _, _ => .bang | .unk, _ => .unk + | .fam _, h => h.elim theorem affine_valTy : ∀ σ : Shape, σ.All Affine → Affine σ.valTy | .leaf _, h => h diff --git a/Core/RelMu.lean b/Core/RelMu.lean index 93b2ab0f6..860cc3b99 100644 --- a/Core/RelMu.lean +++ b/Core/RelMu.lean @@ -78,6 +78,7 @@ theorem vCE_some (η : Option (Nat → TEnt)) : | sum A B ihA ihB => intro b; funext ls δ v; simp only [vCE, ihA, ihB] | thunk _ => intro b; cases b <;> rfl | ref A ih => intro b; funext ls δ v; simp only [vCE, ih] + | fam L ih => intro b; funext ls δ v; simp only [vCE, ih] | imm a A ih => intro b; funext ls δ v have e : tClose η A = tClose (some (tEnv η)) A := by rw [tClose_eq]; rfl @@ -111,6 +112,7 @@ theorem vCE_rename : ∀ (A : VTy) (ρ : Nat → TEnt) (r : Nat → Nat) (b : Bo | sum A B ihA ihB => intro ρ r b; funext ls δ v; simp only [VTy.rename, vCE, ihA, ihB] | thunk _ => intro ρ r b; cases b <;> rfl | ref A ih => intro ρ r b; funext ls δ v; simp only [VTy.rename, vCE, ih] + | fam L ih => intro ρ r b; funext ls δ v; simp only [VTy.rename, vCE, ih] | imm a A ih => intro ρ r b; funext ls δ v have e : tClose (some ρ) (A.rename r) = tClose (some (ρ ∘ r)) A := by @@ -145,6 +147,7 @@ theorem vCE_subst : ∀ (A : VTy) (ρ : Nat → TEnt) (σ : Nat → VTy) (δ : L | sum A B ihA ihB => intro ρ σ δ b ls; funext v; simp only [VTy.subst, vCE, ihA, ihB] | thunk _ => intro ρ σ δ b ls; cases b <;> rfl | ref A ih => intro ρ σ δ b ls; funext v; simp only [VTy.subst, vCE, ih] + | fam L ih => intro ρ σ δ b ls; funext v; simp only [VTy.subst, vCE, ih] | imm a A ih => intro ρ σ δ b ls; funext v have e : tClose (some ρ) (A.subst σ) = tClose (some (substEnv ρ σ δ)) A := by @@ -207,6 +210,7 @@ theorem vCE_congr_occ : ∀ (A : VTy) (η₁ η₂ : Nat → TEnt), (∀ n, A.Oc ihB η₁ η₂ (fun n hn => h n (Or.inr hn))] | thunk _ => intro _ _ _ b; cases b <;> rfl | ref A ih => intro η₁ η₂ h b; funext ls δ v; simp only [vCE, ih η₁ η₂ h] + | fam L ih => intro η₁ η₂ h b; funext ls δ v; simp only [vCE, ih η₁ η₂ h] | imm a A ih => intro η₁ η₂ h b; funext ls δ v have e : tClose (some η₁) A = tClose (some η₂) A := @@ -258,6 +262,11 @@ theorem vCE_mono_at : ∀ (A : VTy) (k : Nat) (η₁ η₂ : Nat → TEnt), A.No intro k η₁ η₂ hk ht hP hPk b ls δ v ρ h obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := h exact ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, ih k η₁ η₂ hk ht hP hPk b ls δ v' ρ₂ h₂⟩ + | fam L ih => + intro k η₁ η₂ hk ht hP hPk b ls δ v ρ h + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := h + exact ⟨ℓ, c, ρ₀, ρ₁, hc, hp, + sepRange_mono' (fun m σ hm => ih k η₁ η₂ hk ht hP hPk b ls δ _ σ hm) hs⟩ | imm a A ih => intro k η₁ η₂ hk ht hP hPk b ls δ v ρ h have e : tClose (some η₁) A = tClose (some η₂) A := by @@ -373,6 +382,8 @@ def vF (S : Bool → VTy → List SRec → Val → WProp) (δ : LSub) : | _, .bang A, ls, v => fun ρ => ρ = PMap.empty ∧ S true A ls v ρ | b, .box a A, ls, v => atLife δ a fun α => box α (S b A ls v) | b, .ref A, ls, v => ex fun ℓ => ex fun v' => ⌜v = .loc ℓ⌝ ⋆ ptoOwn ℓ v' ⋆ S b A ls v' + | b, .fam L, ls, v => ex fun ℓ => ex fun c => ⌜v = Val.arr ℓ c⌝ ⋆ + sepRange (fun m => S b L ls (.loc m)) ℓ c.toNat | _, .imm a A, ls, v => atLife δ a fun α => ex fun ℓ => ⌜v = .loc ℓ ∧ CohE (rsOf ls) ℓ A δ⌝ ⋆ ptoImmS ℓ α ls (fun ls₀ => S false A ls₀) | b, .mut a A, ls, v => fun ρ => vC b (.mut a A) ls δ v ρ ∧ @@ -392,6 +403,7 @@ theorem vF_vC (δ : LSub) : ∀ (b : Bool) (T : VTy) (ls : List SRec) (v : Val) | _, .thunk _, _, _, _ => Iff.rfl | _, .box _ _, _, _, _ => Iff.rfl | _, .ref _, _, _, _ => Iff.rfl + | _, .fam _, _, _, _ => Iff.rfl | _, .bang _, _, _, _ => Iff.rfl | _, .imm _ _, _, _, _ => Iff.rfl | _, .mut _ _, _, _, _ => ⟨fun h => ⟨h, fun _ _ _ h => h⟩, And.left⟩ @@ -420,6 +432,9 @@ theorem vF_mono {S₁ S₂ : Bool → VTy → List SRec → Val → WProp} | b, .ref T, ls, _, _, hs => by obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := hs exact ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h b T ls v' ρ₂ h₂⟩ + | b, .fam L, ls, _, _, hs => by + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := hs + exact ⟨ℓ, c, ρ₀, ρ₁, hc, hp, sepRange_mono' (fun m σ hm => h b L ls _ σ hm) hs⟩ | _, .bang T, ls, _, _, hs => ⟨hs.1, h true T ls _ _ hs.2⟩ | _, .imm _ T, _, _, _, hs => by obtain ⟨α, hα, ℓ, ρ₀, ρ₁, hc, hp, hpto⟩ := hs @@ -467,6 +482,11 @@ theorem vC_ind (δ : LSub) (Φ : Bool → VTy → List SRec → Val → WRes → obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := hr exact hG b _ ls v r ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, ih η₀ σ ht hP hex b ls v' ρ₂ h₂⟩ + | fam L ih => + intro η₀ σ ht hP hex b ls v r hr + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := hr + exact hG b _ ls v r ⟨ℓ, c, ρ₀, ρ₁, hc, hp, + sepRange_mono' (fun m σ' hm => ih η₀ σ ht hP hex b ls _ σ' hm) hs⟩ | imm a A ih => intro η₀ σ ht hP hex b ls v r hr have e : tClose (some η₀) A = A.subst σ := by diff --git a/Core/Relation.lean b/Core/Relation.lean index 5a0e95c82..4db83e923 100644 --- a/Core/Relation.lean +++ b/Core/Relation.lean @@ -142,6 +142,8 @@ def vCE : Option (Nat → TEnt) → Bool → VTy → List SRec → LSub → Val (∀ b' ls' v' ρ', vCE (some (VTy.scons ⟨tClose η (.mu A), P⟩ (tEnv η))) b' A ls' δ v' ρ' → P b' ls' v' ρ') → P b ls v ρ + | η, b, .fam L, ls, δ, v => ex fun ℓ => ex fun c => ⌜v = Val.arr ℓ c⌝ ⋆ + sepRange (fun m => vCE η b L ls δ (.loc m)) ℓ c.toNat /-- `𝒞⟦C⟧(ls)δ(e)`, curried on the connective. At `νX. C` the greatest fixed point `⋃{R | R ⊆ cB (nuEnv C R) C}`, written in `Prop` as `∃ R` (`docs/scope-recursion.md` §3.1), and trivial unless `C` is positive (`CTy.NuOk`). `[spec §3, §7]` -/ diff --git a/Core/RelationFacts.lean b/Core/RelationFacts.lean index 5c59c55fa..e2e5f35a9 100644 --- a/Core/RelationFacts.lean +++ b/Core/RelationFacts.lean @@ -83,6 +83,10 @@ theorem TyEq.immG {c c' : Lifetime.Life} {δ δ' : LSub} (hc : c.interp δ = c'. intro A' pay pay' h hp; cases A' <;> simp only [TyEq] at h simp only [VTy.immG] exact ih pay pay' h.2 hp + | fam A ih => + intro A' pay pay' h hp; cases A' <;> simp only [TyEq] at h + simp only [VTy.immG] + exact ih pay pay' h hp | mu A ih => intro A' pay pay' h hp; cases A' <;> simp only [TyEq] at h refine ih _ _ h (fun n => ?_) @@ -108,6 +112,7 @@ theorem TyEq.applyLSub : ∀ (T : VTy) (σ : Lifetime.LSubst) (δ δ' : LSub), | .unk, _, _, _, _ => trivial | .ref T, σ, δ, δ', h => TyEq.applyLSub T σ δ δ' h | .bang T, σ, δ, δ', h => TyEq.applyLSub T σ δ δ' h + | .fam T, σ, δ, δ', h => TyEq.applyLSub T σ δ δ' h | .thunk C, σ, δ, δ', h => CTyEq.applyLSub C σ δ δ' h | .sum T₁ T₂, σ, δ, δ', h => ⟨TyEq.applyLSub T₁ σ δ δ' (fun y hy => h y (Or.inl hy)), @@ -308,6 +313,11 @@ theorem vC_local : ∀ (b : Bool) (T : VTy) {ls ls' : List SRec} {δ : LSub} {v | unk => exact h | tvar _ => exact h.elim | mu A => exact vC_roll h.1 h.2.1 (h.2.2.2 _ hk) + | fam L => + obtain ⟨ℓ, n, ρ₀, ρ₃, hc, hp, hs⟩ := h + exact ⟨ℓ, n, ρ₀, ρ₃, hc, hp, sepRange_mono_down (R := fun σ => Ext ls ls' σ) + (fun _ _ _ hc' hk' => ⟨hk'.left hc', hk'.right hc'⟩) (fun m σ hkσ hm => hm.2 _ hkσ) + (hk.right hc) hs⟩ /-- The program's relation implies the observable view. -/ theorem vO_obs : ∀ (T : VTy) {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes}, @@ -341,6 +351,9 @@ theorem vO_obs : ∀ (T : VTy) {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes | unk => exact h | tvar _ => exact h.elim | mu A => exact vC_roll h.1 h.2.1 h.2.2.2 + | fam L => + obtain ⟨ℓ, n, ρ₀, ρ₃, hc, hp, hs⟩ := h + exact ⟨ℓ, n, ρ₀, ρ₃, hc, hp, sepRange_mono' (fun m σ hm => hm.2) hs⟩ /-- `vC ⊆ vShape`, both modes. -/ theorem vC_vShape : ∀ (b : Bool) (T : VTy) {ls : List SRec} {δ : LSub} {v : Val} {ρ : WRes}, @@ -379,6 +392,9 @@ theorem vC_vShape : ∀ (b : Bool) (T : VTy) {ls : List SRec} {δ : LSub} {v : V | unk => exact h | tvar _ => exact h.elim | mu A => exact vShape_roll h.2.1 h.2.2.2 + | fam L => + obtain ⟨ℓ, n, ρ₀, ρ₃, hc, hp, hs⟩ := h + exact ⟨ℓ, n, ρ₀, ρ₃, hc, hp, sepRange_mono' (fun m σ hm => hm.2) hs⟩ /-- Reading an `imm` cell at the observable view: the payload is observable-valid at the current list. -/ @@ -522,6 +538,13 @@ theorem vC_outlives {Δ : LifeCtx} {δ : LSub} {a : Lifetime.Life} {α : Life} | unk => cases hT | tvar _ => exact h.elim | mu A => exact h.2.2.2 hT.unroll + | fam L => + cases hT with + | fam hL => + obtain ⟨ℓ, n, hs⟩ := h + obtain ⟨-, hs⟩ := pure_sep_iff.mp hs + exact sepRange_closed (R := fun σ => BoLo.Outlives σ α) (ResU.inStratum_empty α) + (fun _ _ _ hc o₁ o₂ => (outlives_comp hc α).mpr ⟨o₁, o₂⟩) (fun m σ hm => hm.2 hL) hs end BoCa.Fig16.LogRel.Core @@ -600,6 +623,9 @@ theorem vCE_congr : ∀ (A : VTy) (η : Option (Nat → TEnt)) {δ₁ δ₂ : LS | .bang A, η, _, _, h, hη, b, ls => by have ih := vCE_congr A η h hη funext v; cases b <;> simp only [vCE, ih] + | .fam A, η, _, _, h, hη, b, ls => by + have ih := vCE_congr A η h hη + funext v; cases b <;> simp only [vCE, ih] | .box a A, η, δ₁, δ₂, h, hη, b, ls => by have ha : a.interp δ₁ = a.interp δ₂ := interp_congr a (fun y hy => h y (Or.inl hy)) have ih := vCE_congr A η (fun y hy => h y (Or.inr hy)) hη @@ -706,7 +732,7 @@ theorem TyEq.poly_iff : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub}, TyEq A δ A' | .tvar _, A', _, _, h => by cases A' <;> simp only [TyEq] at h; subst h; exact Iff.rfl | .unit, A', _, _, h | .zero, A', _, _, h | .thunk _, A', _, _, h | .unk, A', _, _, h | .int _, A', _, _, h | .ref _, A', _, _, h | .imm _ _, A', _, _, h | .mut _ _, A', _, _, h - | .box _ _, A', _, _, h | .bang _, A', _, _, h | .mu _, A', _, _, h => by + | .box _ _, A', _, _, h | .bang _, A', _, _, h | .mu _, A', _, _, h | .fam _, A', _, _, h => by have ho := TyEq.occ_iff _ 0 h cases A' <;> simp only [TyEq] at h simp only [VTy.Poly, ho] @@ -773,6 +799,10 @@ theorem vCE_tyEq : ∀ (S : VTy) {S' : VTy} {δ δ' : LSub} (η η' : Option (Na cases S' <;> simp only [TyEq] at h have ih := vCE_tyEq A η η' h hη intro b ls; funext v; cases b <;> simp only [vCE, ih] + | .fam A, S', _, _, η, η', h, hη => by + cases S' <;> simp only [TyEq] at h + have ih := vCE_tyEq A η η' h hη + intro b ls; funext v; cases b <;> simp only [vCE, ih] | .box a A, S', _, _, η, η', h, hη => by cases S' <;> simp only [TyEq] at h have ih := vCE_tyEq A η η' h.2 hη @@ -1076,6 +1106,16 @@ theorem obs_transfer {R : WRes} {ls : List SRec} (hLB : WitLB ls R) : | mu M => obtain ⟨hnm, hcl, hG⟩ := h exact vC_roll hnm hcl (hG.2 rfl rfl _ hA hB' (vShape_unfold hs).2) + | fam L => + obtain ⟨ℓ, n, hk⟩ := h + obtain ⟨he, hsA⟩ := pure_sep_iff.mp hk + obtain ⟨ℓ', n', hk'⟩ := hs + obtain ⟨he', hsB⟩ := pure_sep_iff.mp hk' + obtain ⟨rfl, rfl⟩ := Val.arr_inj (he.symm.trans he') + exact ⟨ℓ, n, pure_sep_mk he (sepRange_zip (RA := CellMatch R) (RB := CellMatch R) (f := id) + (fun _ _ _ hc h => ⟨h.left hc, h.right hc⟩) (fun _ _ _ hc h => ⟨h.left hc, h.right hc⟩) + (fun _ _ _ hc => hc) rfl + (fun m _ _ a b ha hb hPa hQb => hPa.2 rfl rfl _ ha hb hQb) hA hB' hsA hsB)⟩ end BoCa.Fig16.LogRel.Core @@ -1338,6 +1378,28 @@ theorem relabel_obs {R : WRes} {ls : List SRec} {β β₀ : Life} {δ : LSub} {x exact hG.2 rfl rfl (fun h => (vFree_inst h).elim hx hx) (fun h => (vFree_inst h).elim hx₀ hx₀) _ ((vShape_immReborrow_mu_iff hM _ _ v c).mp hc) hRl hcR hs (fun m S u h => hcr m S u (RefPos.mu hcl h)) (fun m S h => hcm m S (MutPos.mu hcl h)) + | fam L => + obtain ⟨ℓ, n, hk₁⟩ := hc + obtain ⟨he, hs₁⟩ := pure_sep_iff.mp hk₁ + obtain ⟨ℓ', n', hk₂⟩ := hR + obtain ⟨he', hs₂⟩ := pure_sep_iff.mp hk₂ + subst he + obtain ⟨rfl, rfl⟩ := Val.arr_inj he' + refine ⟨ℓ, n, pure_sep_mk rfl ?_⟩ + exact sepRange_zip (RA := CellMatch R) + (RB := fun c => (∀ m ψ, c.get m = some ψ → ∃ ζ, R.get m = some ζ ∧ ψ.erase = ζ.erase ∧ + (ζ.kind ≠ Kind.own → ψ.wit = ζ.wit) ∧ (ζ.kind = Kind.own → CellMatch R ψ.wit)) ∧ + (∀ m ψ, c.get m = some ψ → (∃ ζ, R.get m = some ζ ∧ ζ.kind ≠ Kind.imm) → + ψ.wit.InStratum (LSet.singleton β).join)) + (f := relRes R β) + (fun _ _ _ hc h => ⟨h.left hc, h.right hc⟩) + (fun _ _ _ hc h => ⟨⟨rebMatch_left hc h.1, strat_left hc h.2⟩, + ⟨rebMatch_left hc.comm h.1, strat_left hc.comm h.2⟩⟩) + (fun _ _ _ hc => relRes_compS hc) (relRes_empty R β) + (fun m hm hm' a b ha hb hPa hQb => hPa.2 rfl rfl hx hx₀ b hQb ha hb.1 hb.2 + (fun m' S u h => hcr m' S u (RefPos.fam_at hm hm' h)) + (fun m' S h => hcm m' S (MutPos.fam_at hm hm' h))) + hRl ⟨hcR, hs⟩ hs₂ hs₁ /-- The chooser at a record's cell, at the program's relation, from the payload the program holds at the cell (`hRobs`, the observable view of the escrow, read by `ptoImmO_read`), `WitLB` at the escrow, and `β` below every tag. If no root of the escrow diff --git a/Core/SepRange.lean b/Core/SepRange.lean new file mode 100644 index 000000000..40b5e4eee --- /dev/null +++ b/Core/SepRange.lean @@ -0,0 +1,182 @@ +import Paper.S5_Model.Definitions +import Support.Model.Notation +import Support.Model.Propositions +import Support.Model.UpdateFrame +import Support.Model.Update +import Support.Model.Algebra + +/-! +# Core — the iterated separating conjunction over a range of locations `[spec §9]` + +`sepRange P ℓ n = P ℓ ⋆ P (ℓ+1) ⋆ … ⋆ P (ℓ+n−1)`, the resource of an array `(ℓ, n)` whose leaf +at `ℓ + i` satisfies `P`. The lemmas are those the relation's clause at `[L]` needs: monotonicity, +properties of resources closed under `●` and holding at `∅`, and the split of a range at one +index. +-/ + +noncomputable section + +namespace BoCa.Fig16.BoLo +open BoCa + +/-- `⊛_{i emp + | ℓ, n + 1 => sep (P ℓ) (sepRange P (ℓ + 1) n) + +@[simp] theorem sepRange_zero (P : Loc → WProp) (ℓ : Loc) : sepRange P ℓ 0 = emp := rfl + +theorem sepRange_succ (P : Loc → WProp) (ℓ : Loc) (n : Nat) : + sepRange P ℓ (n + 1) = sep (P ℓ) (sepRange P (ℓ + 1) n) := rfl + +/-- Pointwise entailment on the range lifts to the range. -/ +theorem sepRange_mono {P Q : Loc → WProp} : + ∀ {ℓ : Loc} {n : Nat}, (∀ m : Nat, ℓ ≤ m → m < ℓ + n → ∀ ρ, P m ρ → Q m ρ) → + ∀ ρ, sepRange P ℓ n ρ → sepRange Q ℓ n ρ + | _, 0, _, _, h => h + | ℓ, n + 1, hPQ, _, ⟨ρ₁, ρ₂, hc, h₁, h₂⟩ => by + have hl : (ℓ : Nat) < ℓ + (n + 1) := Nat.lt_add_of_pos_right (Nat.succ_pos n) + refine ⟨ρ₁, ρ₂, hc, hPQ ℓ (Nat.le_refl _) hl ρ₁ h₁, + sepRange_mono (fun m hm hm' => hPQ m ?_ ?_) ρ₂ h₂⟩ + · exact Nat.le_of_succ_le hm + · have : (ℓ : Nat) + 1 + n = ℓ + (n + 1) := by ring + exact this ▸ hm' + +/-- Pointwise entailment everywhere lifts to the range. -/ +theorem sepRange_mono' {P Q : Loc → WProp} (h : ∀ m ρ, P m ρ → Q m ρ) {ℓ : Loc} {n : Nat} + {ρ : WRes} (hP : sepRange P ℓ n ρ) : sepRange Q ℓ n ρ := + sepRange_mono (fun m _ _ => h m) ρ hP + +/-- Pointwise entailment under a property of resources inherited by parts. -/ +theorem sepRange_mono_down {R : WRes → Prop} + (hR : ∀ a₁ a₂ a, ResU.CompS a₁ a₂ a → R a → R a₁ ∧ R a₂) {P Q : Loc → WProp} + (h : ∀ m σ, R σ → P m σ → Q m σ) : + ∀ {ℓ : Loc} {n : Nat} {ρ : WRes}, R ρ → sepRange P ℓ n ρ → sepRange Q ℓ n ρ + | _, 0, _, _, hP => hP + | _, _ + 1, _, hρ, ⟨ρ₁, ρ₂, hc, h₁, h₂⟩ => + ⟨ρ₁, ρ₂, hc, h _ _ (hR _ _ _ hc hρ).1 h₁, sepRange_mono_down hR h (hR _ _ _ hc hρ).2 h₂⟩ + +/-- Two ranges over the same locations, read leaf by leaf: a leaf of the first and the +corresponding leaf of the second give a leaf of the output, at the image of the second +resource under a map that preserves `●` and `∅`. Properties inherited by parts may be assumed +of each leaf. -/ +theorem sepRange_zip {P Q Z : Loc → WProp} {RA RB : WRes → Prop} {f : WRes → WRes} + (hRA : ∀ a₁ a₂ a, ResU.CompS a₁ a₂ a → RA a → RA a₁ ∧ RA a₂) + (hRB : ∀ a₁ a₂ a, ResU.CompS a₁ a₂ a → RB a → RB a₁ ∧ RB a₂) + (hf : ∀ a₁ a₂ a, ResU.CompS a₁ a₂ a → ResU.CompS (f a₁) (f a₂) (f a)) + (hf0 : f PMap.empty = PMap.empty) : + ∀ {ℓ : Loc} {n : Nat} {A B : WRes}, + (∀ m : Nat, ℓ ≤ m → m < ℓ + n → ∀ a b, RA a → RB b → P m a → Q m b → Z m (f b)) → + RA A → RB B → sepRange P ℓ n A → sepRange Q ℓ n B → sepRange Z ℓ n (f B) + | _, 0, _, _, _, _, _, _, ⟨e, _⟩ => by subst e; rw [hf0]; exact ⟨rfl, trivial⟩ + | ℓ, n + 1, _, _, h, hA, hB, ⟨a₁, a₂, hca, ha₁, ha₂⟩, ⟨b₁, b₂, hcb, hb₁, hb₂⟩ => by + have hl : (ℓ : Nat) < ℓ + (n + 1) := Nat.lt_add_of_pos_right (Nat.succ_pos n) + refine ⟨f b₁, f b₂, hf _ _ _ hcb, + h ℓ (Nat.le_refl _) hl a₁ b₁ (hRA _ _ _ hca hA).1 (hRB _ _ _ hcb hB).1 ha₁ hb₁, ?_⟩ + refine sepRange_zip hRA hRB hf hf0 (fun m hm hm' => h m ?_ ?_) + (hRA _ _ _ hca hA).2 (hRB _ _ _ hcb hB).2 ha₂ hb₂ + · exact Nat.le_of_succ_le hm + · have : (ℓ : Nat) + 1 + n = ℓ + (n + 1) := by ring + exact this ▸ hm' + +/-- A property of resources that holds at `∅` and is closed under `●` holds of a range whose +every leaf has it. -/ +theorem sepRange_closed {R : WRes → Prop} (h0 : R PMap.empty) + (hc : ∀ ρ₁ ρ₂ ρ, ResU.CompS ρ₁ ρ₂ ρ → R ρ₁ → R ρ₂ → R ρ) {P : Loc → WProp} + (hP : ∀ m ρ, P m ρ → R ρ) : ∀ {ℓ : Loc} {n : Nat} {ρ : WRes}, sepRange P ℓ n ρ → R ρ + | _, 0, _, ⟨rfl, _⟩ => h0 + | _, _ + 1, _, ⟨_, _, hcρ, h₁, h₂⟩ => hc _ _ _ hcρ (hP _ _ h₁) (sepRange_closed h0 hc hP h₂) + +/-- Two adjacent ranges make one. -/ +theorem sepRange_append {P : Loc → WProp} : + ∀ {ℓ : Loc} {i k : Nat} {ρ₁ ρ₂ ρ : WRes}, ResU.CompS ρ₁ ρ₂ ρ → + sepRange P ℓ i ρ₁ → sepRange P (ℓ + i) k ρ₂ → sepRange P ℓ (i + k) ρ + | ℓ, 0, k, ρ₁, ρ₂, ρ, hc, ⟨e, _⟩, h₂ => by + subst e + rw [eq_of_compS_empty_left hc, Nat.zero_add] + simpa using h₂ + | ℓ, i + 1, k, ρ₁, ρ₂, ρ, hc, ⟨σ₁, σ₂, hσ, h₁, h₂⟩, h₃ => by + obtain ⟨τ, hτ, hρ⟩ := compS_reassoc hσ hc + have e : ℓ + (i + 1) = ℓ + 1 + i := by ring + rw [e] at h₃ + have := sepRange_append hτ h₂ h₃ + rw [show i + 1 + k = (i + k) + 1 by ring] + exact ⟨σ₁, τ, hρ, h₁, this⟩ + +/-- A range splits at a length `i ≤ n`. -/ +theorem sepRange_split {P : Loc → WProp} : + ∀ {ℓ : Loc} {i k : Nat} {ρ : WRes}, sepRange P ℓ (i + k) ρ → + ∃ ρ₁ ρ₂, ResU.CompS ρ₁ ρ₂ ρ ∧ sepRange P ℓ i ρ₁ ∧ sepRange P (ℓ + i) k ρ₂ + | ℓ, 0, k, ρ, h => ⟨PMap.empty, ρ, compS_empty_left ρ, ⟨rfl, trivial⟩, by simpa using h⟩ + | ℓ, i + 1, k, ρ, h => by + rw [show i + 1 + k = (i + k) + 1 by ring] at h + obtain ⟨σ₁, σ₂, hσ, h₁, h₂⟩ := h + obtain ⟨τ₁, τ₂, hτ, h₃, h₄⟩ := sepRange_split (ℓ := ℓ + 1) (i := i) h₂ + obtain ⟨x, hx, hρ⟩ := compS_reassoc' hτ hσ + refine ⟨x, τ₂, hρ, ⟨σ₁, τ₁, hx, h₁, h₃⟩, ?_⟩ + rwa [show ℓ + (i + 1) = ℓ + 1 + i by ring] + +/-- The leaf at `ℓ + i`, with the rest of the range framed: the range is `P (ℓ+i)` beside the +two ranges on either side. -/ +theorem sepRange_pick {P : Loc → WProp} {ℓ : Loc} {n i : Nat} (hi : i < n) {ρ : WRes} + (h : sepRange P ℓ n ρ) : + ∃ ρi ρr, ResU.CompS ρi ρr ρ ∧ P (ℓ + i) ρi ∧ + ∀ σ ρ', ResU.CompS σ ρr ρ' → P (ℓ + i) σ → sepRange P ℓ n ρ' := by + have hn : n = i + ((n - i - 1) + 1) := by omega + rw [hn] at h + obtain ⟨ρa, ρb, hab, ha, ρi, ρc, hic, hi', hc⟩ := sepRange_split h + obtain ⟨ρr, hr, hρ⟩ := compS_lcomm hic hab + refine ⟨ρi, ρr, hρ, hi', fun σ ρ' hσ hP => ?_⟩ + obtain ⟨σc, hσc, hρ'⟩ := compS_lcomm hr hσ + rw [hn] + exact sepRange_append hρ' ha ⟨σ, ρc, hσc, hP, hc⟩ + +/-- The leaf at `ℓ + i` is a part of the range. -/ +theorem sepRange_le {P : Loc → WProp} {ℓ : Loc} {n i : Nat} (hi : i < n) {ρ : WRes} + (h : sepRange P ℓ n ρ) : ∃ ρi, P (ℓ + i) ρi ∧ ResU.Le ρi ρ := by + obtain ⟨ρi, ρr, hc, hP, -⟩ := sepRange_pick hi h + exact ⟨ρi, hP, ρr, hc⟩ + +/-- A cell of a range is a cell of one of its leaves. -/ +theorem sepRange_get {P : Loc → WProp} : + ∀ {ℓ : Loc} {n : Nat} {ρ : WRes}, sepRange P ℓ n ρ → ∀ {m : Loc} {ψ : CellU Loc Val}, + ρ.get m = some ψ → + ∃ i, i < n ∧ ∃ ρi ψi, P (ℓ + i) ρi ∧ ρi.get m = some ψi ∧ ResU.Le ρi ρ + | _, 0, _, ⟨e0, _⟩, m, ψ, e => by subst e0; simp at e + | ℓ, n + 1, ρ, ⟨ρ₁, ρ₂, hc, h₁, h₂⟩, m, ψ, e => by + rcases ResU.Comp.get hc m with ⟨-, -, e'⟩ | ⟨ζ, e₁, -, -⟩ | ⟨ζ, -, e₂, -⟩ | + ⟨ζ₁, ζ₂, ζ, e₁, -, -, -⟩ + · rw [e'] at e; cases e + · exact ⟨0, Nat.succ_pos n, ρ₁, _, by simpa using h₁, e₁, ρ₂, hc⟩ + · obtain ⟨i, hi, ρi, ψi, hP, hg, hle⟩ := sepRange_get h₂ e₂ + refine ⟨i + 1, Nat.succ_lt_succ hi, ρi, ψi, ?_, hg, + hle.trans ⟨ρ₁, ResU.CompS.comm hc⟩⟩ + rwa [show ℓ + (i + 1) = ℓ + 1 + i by ring] + · exact ⟨0, Nat.succ_pos n, ρ₁, _, by simpa using h₁, e₁, ρ₂, hc⟩ + +/-- Two distinct leaves of a range compose, within the range. -/ +theorem sepRange_pick2 {P : Loc → WProp} {ℓ : Loc} {n i j : Nat} (hij : i < j) (hj : j < n) + {ρ : WRes} (h : sepRange P ℓ n ρ) : + ∃ ρi ρj X, P (ℓ + i) ρi ∧ P (ℓ + j) ρj ∧ ResU.CompS ρi ρj X ∧ ResU.Le X ρ := by + obtain ⟨k, rfl⟩ : ∃ k, n = (i + 1) + k := ⟨n - (i + 1), by omega⟩ + obtain ⟨j', rfl⟩ : ∃ j', j = (i + 1) + j' := ⟨j - (i + 1), by omega⟩ + have hj' : j' < k := by omega + obtain ⟨ρa, ρb, hab, ha, hb⟩ := sepRange_split h + obtain ⟨ρa₁, ρa₂, ha₁₂, -, ρi, ρe, hie, hPi, he⟩ := sepRange_split (i := i) (k := 1) ha + obtain ⟨ρj, hPj, Y₂, hY₂⟩ := sepRange_le hj' hb + have hρe : ρe = PMap.empty := he.1 + rw [hρe] at hie + have hie' : ρa₂ = ρi := ResU.CompS.functional hie (ResU.comp_empty_right ρi) + rw [hie'] at ha₁₂ + have hY₁ : ResU.CompS ρi ρa₁ ρa := ResU.CompS.comm ha₁₂ + obtain ⟨bc, hbc, hB₁⟩ := compS_reassoc hY₁ hab + obtain ⟨yb, hyb, hbc'⟩ := compS_reassoc' hY₂ hbc + obtain ⟨z, hz, hbc''⟩ := compS_reassoc (ResU.CompS.comm hyb) hbc' + obtain ⟨X, hX, hR⟩ := compS_reassoc' hbc'' hB₁ + refine ⟨ρi, ρj, X, hPi, ?_, hX, z, hR⟩ + have e : ℓ + (i + 1) + j' = ℓ + (i + 1 + j') := by ring + rwa [e] at hPj + +end BoCa.Fig16.BoLo + +end diff --git a/Core/ShapeMu.lean b/Core/ShapeMu.lean index 68efcdfaf..2c18051ee 100644 --- a/Core/ShapeMu.lean +++ b/Core/ShapeMu.lean @@ -54,6 +54,7 @@ theorem vShapeE_some (η : Option (Nat → SEnt)) : | sum A B ihA ihB => funext δ v; simp only [vShapeE, ihA, ihB] | thunk _ => rfl | ref A ih => funext δ v; simp only [vShapeE, ih] + | fam L ih => funext δ v; simp only [vShapeE, ih] | imm a A ih => funext δ v; simp only [vShapeE, ih] | «mut» a A ih => funext δ v; simp only [vShapeE, ih] | box a A ih => funext δ v; simp only [vShapeE, ih] @@ -91,6 +92,7 @@ theorem vShapeE_rename : ∀ (A : VTy) (ρ : Nat → SEnt) (r : Nat → Nat), | sum A B ihA ihB => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ihA, ihB] | thunk _ => intro ρ r; rfl | ref A ih => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ih] + | fam L ih => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ih] | imm a A ih => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ih] | «mut» a A ih => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ih] | box a A ih => intro ρ r; funext δ v; simp only [VTy.rename, vShapeE, ih] @@ -117,6 +119,7 @@ theorem vShapeE_subst : ∀ (A : VTy) (ρ : Nat → SEnt) (σ : Nat → VTy) (δ | sum A B ihA ihB => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ihA, ihB] | thunk _ => intro ρ σ δ; rfl | ref A ih => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ih] + | fam L ih => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ih] | imm a A ih => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ih] | «mut» a A ih => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ih] | box a A ih => intro ρ σ δ; funext v; simp only [VTy.subst, vShapeE, ih] @@ -184,6 +187,10 @@ theorem vShapeE_mono : ∀ (A : VTy) (η₁ η₂ : Nat → SEnt), (∀ n, (η intro η₁ η₂ ht hP δ v ρ h obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := h exact ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, ih η₁ η₂ ht hP δ v' ρ₂ h₂⟩ + | fam L ih => + intro η₁ η₂ ht hP δ v ρ h + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := h + exact ⟨ℓ, c, ρ₀, ρ₁, hc, hp, sepRange_mono' (fun m σ hm => ih η₁ η₂ ht hP δ _ σ hm) hs⟩ | imm a A ih => intro η₁ η₂ ht hP δ v ρ h obtain ⟨α, hα, ℓ, ρ₀, ρ₁, hc, hp, hpto⟩ := h @@ -277,6 +284,7 @@ def sF (S : VTy → Val → WProp) (δ : LSub) : VTy → Val → WProp | .thunk _, _ => fun _ => True | .box a T, v => atLife δ a fun α => box α (S T v) | .ref T, v => ex fun ℓ => ex fun v' => ⌜v = .loc ℓ⌝ ⋆ ptoOwn ℓ v' ⋆ S T v' + | .fam L, v => ex fun ℓ => ex fun c => ⌜v = Val.arr ℓ c⌝ ⋆ sepRange (fun m => S L (.loc m)) ℓ c.toNat | .bang _, _ => emp | .imm a T, v => atLife δ a fun α => ex fun ℓ => ⌜v = .loc ℓ⌝ ⋆ ptoImm ℓ α (S T) | .mut a T, v => atLife δ a fun α => ex fun ℓ => ⌜v = .loc ℓ⌝ ⋆ @@ -296,6 +304,7 @@ theorem sF_vShape (δ : LSub) : ∀ (T : VTy) (v : Val) (ρ : WRes), | .thunk _, _, _ => Iff.rfl | .box _ _, _, _ => Iff.rfl | .ref _, _, _ => Iff.rfl + | .fam _, _, _ => Iff.rfl | .bang _, _, _ => Iff.rfl | .imm _ _, _, _ => Iff.rfl | .mut _ _, _, _ => Iff.rfl @@ -322,6 +331,9 @@ theorem sF_mono {S₁ S₂ : VTy → Val → WProp} (h : ∀ T v ρ, S₁ T v ρ | .ref T, _, _, hs => by obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := hs exact ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h T v' ρ₂ h₂⟩ + | .fam L, _, _, hs => by + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := hs + exact ⟨ℓ, c, ρ₀, ρ₁, hc, hp, sepRange_mono' (fun m σ hm => h L _ σ hm) hs⟩ | .bang _, _, _, hs => hs | .imm _ T, _, _, hs => by obtain ⟨α, hα, ℓ, ρ₀, ρ₁, hc, hp, hpto⟩ := hs @@ -380,6 +392,11 @@ theorem vShape_ind (δ : LSub) (Φ : VTy → Val → WRes → Prop) intro ρe σ ht hP v r hr obtain ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, h₂⟩ := hr exact hG _ v r ⟨ℓ, v', ρ₀, ρ₃, hc, hp, ρ₁, ρ₂, hc', ho, ih ρe σ ht hP v' ρ₂ h₂⟩ + | fam L ih => + intro ρe σ ht hP v r hr + obtain ⟨ℓ, c, ρ₀, ρ₁, hc, hp, hs⟩ := hr + exact hG _ v r ⟨ℓ, c, ρ₀, ρ₁, hc, hp, + sepRange_mono' (fun m σ' hm => ih ρe σ ht hP _ σ' hm) hs⟩ | imm a A ih => intro ρe σ ht hP v r hr obtain ⟨α, hα, ℓ, ρ₀, ρ₁, hc, hp, hpto⟩ := hr @@ -438,6 +455,7 @@ theorem vShapeE_congr : ∀ (T : VTy) (η : Option (Nat → SEnt)) {δ₁ δ₂ simp only [vShapeE, ihA η (fun y hy => h y (Or.inl hy)), ihB η (fun y hy => h y (Or.inr hy))] | thunk _ => intros; rfl | ref A ih => intro η δ₁ δ₂ h; funext v; simp only [vShapeE, ih η h] + | fam L ih => intro η δ₁ δ₂ h; funext v; simp only [vShapeE, ih η h] | imm a A ih => intro η δ₁ δ₂ h; funext v simp only [vShapeE, ih η (fun y hy => h y (Or.inr hy))] @@ -482,6 +500,9 @@ theorem tyEq_symm : ∀ (S : VTy) {S' : VTy} {δ δ' : LSub}, TyEq S δ S' δ' | .ref A, S', _, _, h => by cases S' <;> simp only [TyEq] at h ⊢ exact tyEq_symm A h + | .fam A, S', _, _, h => by + cases S' <;> simp only [TyEq] at h ⊢ + exact tyEq_symm A h | .bang A, S', _, _, h => by cases S' <;> simp only [TyEq] at h ⊢ exact tyEq_symm A h @@ -546,6 +567,10 @@ theorem tyEq_trans : ∀ (S : VTy) {S' S'' : VTy} {δ δ' δ'' : LSub}, TyEq S cases S' <;> simp only [TyEq] at h cases S'' <;> simp only [TyEq] at h' ⊢ exact tyEq_trans A h h' + | .fam A, S', S'', _, _, _, h, h' => by + cases S' <;> simp only [TyEq] at h + cases S'' <;> simp only [TyEq] at h' ⊢ + exact tyEq_trans A h h' | .bang A, S', S'', _, _, _, h, h' => by cases S' <;> simp only [TyEq] at h cases S'' <;> simp only [TyEq] at h' ⊢ @@ -611,6 +636,7 @@ theorem TyEq.of_agree : ∀ (T : VTy) {δ₁ δ₂ : LSub}, TyEq.of_agree B (fun y hy => h y (Or.inr hy))⟩ | .thunk C, _, _, h => CTyEq.of_agree C h | .ref A, _, _, h => TyEq.of_agree A h + | .fam A, _, _, h => TyEq.of_agree A h | .bang A, _, _, h => TyEq.of_agree A h | .box a A, _, _, h => ⟨interp_congr a (fun y hy => h y (Or.inl hy)), TyEq.of_agree A (fun y hy => h y (Or.inr hy))⟩ @@ -661,6 +687,7 @@ theorem TyEq.rename : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (r : Nat → Nat) intro A' _ _ r h; cases A' <;> simp only [TyEq, VTy.rename] at h ⊢ exact ⟨ihA r h.1, ihB r h.2⟩ | ref A ih => intro A' _ _ r h; cases A' <;> simp only [TyEq, VTy.rename] at h ⊢; exact ih r h + | fam A ih => intro A' _ _ r h; cases A' <;> simp only [TyEq, VTy.rename] at h ⊢; exact ih r h | bang A ih => intro A' _ _ r h; cases A' <;> simp only [TyEq, VTy.rename] at h ⊢; exact ih r h | imm a A ih => intro A' _ _ r h; cases A' <;> simp only [TyEq, VTy.rename] at h ⊢; exact ⟨h.1, ih r h.2⟩ @@ -691,6 +718,8 @@ theorem TyEq.subst : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (σ σ' : Nat → exact ⟨ihA σ σ' h.1 hσ, ihB σ σ' h.2 hσ⟩ | ref A ih => intro A' _ _ σ σ' h hσ; cases A' <;> simp only [TyEq, VTy.subst] at h ⊢; exact ih σ σ' h hσ + | fam A ih => + intro A' _ _ σ σ' h hσ; cases A' <;> simp only [TyEq, VTy.subst] at h ⊢; exact ih σ σ' h hσ | bang A ih => intro A' _ _ σ σ' h hσ; cases A' <;> simp only [TyEq, VTy.subst] at h ⊢; exact ih σ σ' h hσ | imm a A ih => @@ -726,6 +755,7 @@ theorem TyEq.Cl_iff : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (k : Nat), intro A' _ _ k h; cases A' <;> simp only [TyEq] at h exact and_congr (ihA k h.1) (ihB k h.2) | ref A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Cl]; exact ih k h + | fam A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Cl]; exact ih k h | bang A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Cl]; exact ih k h | imm a A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Cl]; exact ih k h.2 | «mut» a A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Cl]; exact ih k h.2 @@ -749,6 +779,7 @@ theorem TyEq.occ_iff : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (k : Nat), intro A' _ _ k h; cases A' <;> simp only [TyEq] at h exact or_congr (ihA k h.1) (ihB k h.2) | ref A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Occ]; exact ih k h + | fam A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Occ]; exact ih k h | bang A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Occ]; exact ih k h | imm a A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Occ]; exact ih k h.2 | «mut» a A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.Occ]; exact ih k h.2 @@ -772,6 +803,7 @@ theorem TyEq.noMut_iff : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (k : Nat), intro A' _ _ k h; cases A' <;> simp only [TyEq] at h exact and_congr (ihA k h.1) (ihB k h.2) | ref A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.NoMut]; exact ih k h + | fam A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.NoMut]; exact ih k h | bang A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.NoMut]; exact ih k h | imm a A ih => intro A' _ _ k h; cases A' <;> simp only [TyEq] at h; simp only [VTy.NoMut]; exact ih k h.2 | «mut» a A _ => @@ -813,6 +845,9 @@ theorem vShapeE_tyEq : ∀ (A : VTy) {A' : VTy} {δ δ' : LSub} (η η' : Nat | ref A ih => intro A' _ _ η η' h hη; cases A' <;> simp only [TyEq] at h funext v; simp only [vShapeE, ih η η' h hη] + | fam A ih => + intro A' _ _ η η' h hη; cases A' <;> simp only [TyEq] at h + funext v; simp only [vShapeE, ih η η' h hη] | bang A _ => intro A' _ _ η η' h _; cases A' <;> first | rfl | simp_all [TyEq] | imm a A ih => intro A' _ _ η η' h hη; cases A' <;> simp only [TyEq] at h diff --git a/Core/Types.lean b/Core/Types.lean index 53e94b69c..874026ab8 100644 --- a/Core/Types.lean +++ b/Core/Types.lean @@ -57,6 +57,9 @@ inductive VTy where | mu (A : VTy) /-- `I32` (`w32`) and `I64` (`w64`): opaque integers, holding no resource. `[spec §8]` -/ | int (w : Width) + /-- `[L]`: the run-time family `⊛_{i unit - | .tensor A B => tensor A B (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int B) + | .tensor A B => tensor A B (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam B) | .zero => zero - | .sum A B => sum A B (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int B) + | .sum A B => sum A B (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam B) | .thunk C => thunk C - | .ref A => ref A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - | .imm a A => imm a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - | .mut a A => «mut» a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - | .box a A => box a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) - | .bang A => bang A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) + | .ref A => ref A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + | .imm a A => imm a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + | .mut a A => «mut» a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + | .box a A => box a A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + | .bang A => bang A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) + | .fam A => fam A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) | .unk => unk | .int w => int w | .tvar n => tvar n - | .mu A => mu A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int A) + | .mu A => mu A (VTy.recV unit tensor zero sum thunk ref imm «mut» box bang unk tvar mu int fam A) /-- `A ⊸ B` at value type: `U (A ⊸ F B)`, BoLo's arrow read call-by-value. -/ abbrev VTy.lolli (A B : VTy) : VTy := .thunk (.arr A (.ret B)) @@ -127,6 +132,7 @@ def VFree (x : LifeVar) : VTy → Prop | .thunk C => CFree x C | .ref A => VFree x A | .bang A => VFree x A + | .fam A => VFree x A | .imm a A => a.mentions x = true ∨ VFree x A | .mut a A => a.mentions x = true ∨ VFree x A | .box a A => a.mentions x = true ∨ VFree x A @@ -164,6 +170,7 @@ def rename (r : Nat → Nat) : VTy → VTy | .mut a A => .mut a (rename r A) | .box a A => .box a (rename r A) | .bang A => .bang (rename r A) + | .fam A => .fam (rename r A) | .unk => .unk | .int w => .int w | .tvar n => .tvar (r n) @@ -187,6 +194,7 @@ def subst (σ : Nat → VTy) : VTy → VTy | .mut a A => .mut a (subst σ A) | .box a A => .box a (subst σ A) | .bang A => .bang (subst σ A) + | .fam A => .fam (subst σ A) | .unk => .unk | .int w => .int w | .tvar n => σ n @@ -207,6 +215,7 @@ theorem rename_rename : ∀ (A : VTy) (r r' : Nat → Nat), | .mut _ A, r, r' => by simp [rename, rename_rename A] | .box _ A, r, r' => by simp [rename, rename_rename A] | .bang A, r, r' => by simp [rename, rename_rename A] + | .fam A, r, r' => by simp [rename, rename_rename A] | .unk, _, _ => rfl | .int _, _, _ => rfl | .tvar _, _, _ => rfl @@ -227,6 +236,7 @@ theorem subst_rename : ∀ (A : VTy) (σ : Nat → VTy) (r : Nat → Nat), | .mut _ A, σ, r => by simp [rename, subst, subst_rename A] | .box _ A, σ, r => by simp [rename, subst, subst_rename A] | .bang A, σ, r => by simp [rename, subst, subst_rename A] + | .fam A, σ, r => by simp [rename, subst, subst_rename A] | .unk, _, _ => rfl | .int _, _, _ => rfl | .tvar _, _, _ => rfl @@ -247,6 +257,7 @@ theorem rename_subst : ∀ (A : VTy) (r : Nat → Nat) (σ : Nat → VTy), | .mut _ A, r, σ => by simp [rename, subst, rename_subst A] | .box _ A, r, σ => by simp [rename, subst, rename_subst A] | .bang A, r, σ => by simp [rename, subst, rename_subst A] + | .fam A, r, σ => by simp [rename, subst, rename_subst A] | .unk, _, _ => rfl | .int _, _, _ => rfl | .tvar _, _, _ => rfl @@ -272,6 +283,7 @@ theorem subst_subst : ∀ (A : VTy) (σ τ : Nat → VTy), | .mut _ A, σ, τ => by simp [subst, subst_subst A] | .box _ A, σ, τ => by simp [subst, subst_subst A] | .bang A, σ, τ => by simp [subst, subst_subst A] + | .fam A, σ, τ => by simp [subst, subst_subst A] | .unk, _, _ => rfl | .int _, _, _ => rfl | .tvar _, _, _ => rfl @@ -297,6 +309,7 @@ theorem subst_id : ∀ (A : VTy), subst .tvar A = A | .mut _ A => by simp [subst, subst_id A] | .box _ A => by simp [subst, subst_id A] | .bang A => by simp [subst, subst_id A] + | .fam A => by simp [subst, subst_id A] | .unk => rfl | .int _ => rfl | .tvar _ => rfl @@ -317,6 +330,7 @@ def Cl : Nat → VTy → Prop | k, .mut _ A => Cl k A | k, .box _ A => Cl k A | k, .bang A => Cl k A + | k, .fam A => Cl k A | _, .unk => True | _, .int _ => True | k, .tvar n => n < k @@ -334,6 +348,7 @@ theorem Cl_rename : ∀ (A : VTy) {k k' : Nat} (r : Nat → Nat), Cl k A → | .mut _ A, _, _, r, h, hr => Cl_rename A r h hr | .box _ A, _, _, r, h, hr => Cl_rename A r h hr | .bang A, _, _, r, h, hr => Cl_rename A r h hr + | .fam A, _, _, r, h, hr => Cl_rename A r h hr | .unk, _, _, _, _, _ => trivial | .int _, _, _, _, _, _ => trivial | .tvar n, _, _, _, h, hr => hr n h @@ -354,6 +369,7 @@ theorem Cl_substB : ∀ (A : VTy) {k j : Nat} (σ : Nat → VTy), Cl j A → ( | .mut _ A, _, _, σ, hA, h => Cl_substB A σ hA h | .box _ A, _, _, σ, hA, h => Cl_substB A σ hA h | .bang A, _, _, σ, hA, h => Cl_substB A σ hA h + | .fam A, _, _, σ, hA, h => Cl_substB A σ hA h | .unk, _, _, _, _, _ => trivial | .int _, _, _, _, _, _ => trivial | .tvar n, _, _, _, hA, h => h n hA @@ -376,6 +392,7 @@ theorem Cl_subst : ∀ (A : VTy) {k : Nat} (σ : Nat → VTy), (∀ n, Cl k (σ | .mut _ A, _, σ, h => Cl_subst A σ h | .box _ A, _, σ, h => Cl_subst A σ h | .bang A, _, σ, h => Cl_subst A σ h + | .fam A, _, σ, h => Cl_subst A σ h | .unk, _, _, _ => trivial | .int _, _, _, _ => trivial | .tvar n, _, _, h => h n @@ -395,6 +412,7 @@ theorem Cl_mono : ∀ (A : VTy) {k k' : Nat}, Cl k A → k ≤ k' → Cl k' A | .mut _ A, _, _, h, hk => Cl_mono A h hk | .box _ A, _, _, h, hk => Cl_mono A h hk | .bang A, _, _, h, hk => Cl_mono A h hk + | .fam A, _, _, h, hk => Cl_mono A h hk | .unk, _, _, _, _ => trivial | .int _, _, _, _, _ => trivial | .tvar _, _, _, h, hk => Nat.lt_of_lt_of_le h hk @@ -412,6 +430,7 @@ theorem rename_closed : ∀ (A : VTy) {k : Nat} (r : Nat → Nat), Cl k A → | .mut _ A, _, r, h, hr => by simp [rename, rename_closed A r h hr] | .box _ A, _, r, h, hr => by simp [rename, rename_closed A r h hr] | .bang A, _, r, h, hr => by simp [rename, rename_closed A r h hr] + | .fam A, _, r, h, hr => by simp [rename, rename_closed A r h hr] | .unk, _, _, _, _ => rfl | .int _, _, _, _, _ => rfl | .tvar n, _, _, h, hr => by simp [rename, hr n h] @@ -434,6 +453,7 @@ theorem subst_closed : ∀ (A : VTy) {k : Nat} (σ : Nat → VTy), Cl k A → | .mut _ A, _, σ, h, hσ => by simp [subst, subst_closed A σ h hσ] | .box _ A, _, σ, h, hσ => by simp [subst, subst_closed A σ h hσ] | .bang A, _, σ, h, hσ => by simp [subst, subst_closed A σ h hσ] + | .fam A, _, σ, h, hσ => by simp [subst, subst_closed A σ h hσ] | .unk, _, _, _, _ => rfl | .int _, _, _, _, _ => rfl | .tvar n, _, _, h, hσ => hσ n h @@ -457,6 +477,7 @@ theorem subst_congr : ∀ (A : VTy) {k : Nat} {σ τ : Nat → VTy}, Cl k A → | .mut _ A, _, _, _, h, hσ => by simp [subst, subst_congr A h hσ] | .box _ A, _, _, _, h, hσ => by simp [subst, subst_congr A h hσ] | .bang A, _, _, _, h, hσ => by simp [subst, subst_congr A h hσ] + | .fam A, _, _, _, h, hσ => by simp [subst, subst_congr A h hσ] | .unk, _, _, _, _, _ => rfl | .int _, _, _, _, _, _ => rfl | .tvar n, _, _, _, h, hσ => hσ n h @@ -495,6 +516,7 @@ def Occ : Nat → VTy → Prop | k, .mut _ A => Occ k A | k, .box _ A => Occ k A | k, .bang A => Occ k A + | k, .fam A => Occ k A | _, .unk => False | _, .int _ => False | k, .tvar n => n = k @@ -514,6 +536,7 @@ def NoMut : Nat → VTy → Prop | k, .mut _ A => ¬ Occ k A | k, .box _ A => NoMut k A | k, .bang A => NoMut k A + | k, .fam A => NoMut k A | _, .unk => True | _, .int _ => True | _, .tvar _ => True @@ -532,6 +555,7 @@ theorem noMut_of_not_occ : ∀ (A : VTy) {k : Nat}, ¬ Occ k A → NoMut k A | .mut _ A, _, h => h | .box _ A, _, h => noMut_of_not_occ A h | .bang A, _, h => noMut_of_not_occ A h + | .fam A, _, h => noMut_of_not_occ A h | .unk, _, _ => trivial | .int _, _, _ => trivial | .tvar _, _, _ => trivial @@ -548,6 +572,7 @@ theorem not_occ_of_Cl : ∀ (A : VTy) {j k : Nat}, Cl j A → j ≤ k → ¬ Occ | .mut _ A, _, _, hc, hk, h => not_occ_of_Cl A hc hk h | .box _ A, _, _, hc, hk, h => not_occ_of_Cl A hc hk h | .bang A, _, _, hc, hk, h => not_occ_of_Cl A hc hk h + | .fam A, _, _, hc, hk, h => not_occ_of_Cl A hc hk h | .unk, _, _, _, _, h => h | .int _, _, _, _, _, h => h | .tvar n, _, _, hc, hk, h => by simp only [Cl] at hc; simp only [Occ] at h; omega @@ -568,6 +593,7 @@ theorem occ_rename : ∀ (A : VTy) {k : Nat} (r : Nat → Nat), Occ k (rename r | .mut _ A, _, r, h => occ_rename A r h | .box _ A, _, r, h => occ_rename A r h | .bang A, _, r, h => occ_rename A r h + | .fam A, _, r, h => occ_rename A r h | .unk, _, _, h => h.elim | .int _, _, _, h => h.elim | .tvar n, _, _, h => ⟨n, h, rfl⟩ @@ -595,6 +621,7 @@ theorem occ_subst : ∀ (A : VTy) {k : Nat} (τ : Nat → VTy), Occ k (subst τ | .mut _ A, _, τ, h => occ_subst A τ h | .box _ A, _, τ, h => occ_subst A τ h | .bang A, _, τ, h => occ_subst A τ h + | .fam A, _, τ, h => occ_subst A τ h | .unk, _, _, h => h.elim | .int _, _, _, h => h.elim | .tvar n, _, _, h => ⟨n, rfl, h⟩ @@ -624,6 +651,7 @@ theorem subst_congr_occ : ∀ (A : VTy) {σ τ : Nat → VTy}, (∀ n, Occ n A | .mut _ A, _, _, h => by simp [subst, subst_congr_occ A h] | .box _ A, _, _, h => by simp [subst, subst_congr_occ A h] | .bang A, _, _, h => by simp [subst, subst_congr_occ A h] + | .fam A, _, _, h => by simp [subst, subst_congr_occ A h] | .unk, _, _, _ => rfl | .int _, _, _, _ => rfl | .tvar n, _, _, h => h n rfl @@ -647,6 +675,7 @@ theorem occ_rename_of : ∀ (A : VTy) {j : Nat} (r : Nat → Nat), Occ j A → O | .mut _ A, _, r, h => occ_rename_of A r h | .box _ A, _, r, h => occ_rename_of A r h | .bang A, _, r, h => occ_rename_of A r h + | .fam A, _, r, h => occ_rename_of A r h | .unk, _, _, h => h | .int _, _, _, h => h | .tvar n, _, r, h => by simp only [Occ] at h; subst h; rfl @@ -666,6 +695,7 @@ theorem occ_subst_of : ∀ (A : VTy) {n k : Nat} (τ : Nat → VTy), Occ n A → | .mut _ A, _, _, τ, h, hk => occ_subst_of A τ h hk | .box _ A, _, _, τ, h, hk => occ_subst_of A τ h hk | .bang A, _, _, τ, h, hk => occ_subst_of A τ h hk + | .fam A, _, _, τ, h, hk => occ_subst_of A τ h hk | .unk, _, _, _, h, _ => h.elim | .int _, _, _, _, h, _ => h.elim | .tvar m, _, _, τ, h, hk => by simp only [Occ] at h; subst h; exact hk @@ -684,6 +714,7 @@ theorem noMut_of_subst : ∀ (A : VTy) {k : Nat} (τ : Nat → VTy), NoMut k (su | .mut _ A, k, τ, h, hk => fun ho => h (occ_subst_of A τ ho (by rw [hk]; rfl)) | .box _ A, _, τ, h, hk => noMut_of_subst A τ h hk | .bang A, _, τ, h, hk => noMut_of_subst A τ h hk + | .fam A, _, τ, h, hk => noMut_of_subst A τ h hk | .unk, _, _, _, _ => trivial | .int _, _, _, _, _ => trivial | .tvar _, _, _, _, _ => trivial @@ -705,6 +736,7 @@ theorem noMut_subst : ∀ (A : VTy) {k : Nat} (τ : Nat → VTy), NoMut k A → · exact hτ n e ho | .box _ A, _, τ, h, hk, hτ => noMut_subst A τ h hk hτ | .bang A, _, τ, h, hk, hτ => noMut_subst A τ h hk hτ + | .fam A, _, τ, h, hk, hτ => noMut_subst A τ h hk hτ | .unk, _, _, _, _, _ => trivial | .int _, _, _, _, _, _ => trivial | .tvar n, k, τ, _, hk, hτ => by @@ -734,6 +766,7 @@ theorem rename_eq_subst : ∀ (A : VTy) (r : Nat → Nat), rename r A = subst (f | .mut _ A, r => by simp [rename, subst, rename_eq_subst A] | .box _ A, r => by simp [rename, subst, rename_eq_subst A] | .bang A, r => by simp [rename, subst, rename_eq_subst A] + | .fam A, r => by simp [rename, subst, rename_eq_subst A] | .unk, _ => rfl | .int _, _ => rfl | .tvar _, _ => rfl @@ -781,6 +814,7 @@ def immG (b : Lifetime.Life) (pay : Nat → VTy) : VTy → VTy | .mut _ A => .imm b (subst pay A) | .box _ A => immG b pay A | .bang A => .bang (subst pay A) + | .fam A => .fam (immG b pay A) | .unk => .unk | .int w => .int w | .tvar n => .tvar n @@ -826,6 +860,7 @@ theorem immG_rename : ∀ (A : VTy) (pay : Nat → VTy) (r : Nat → Nat), (∀ | .bang A, pay, r, h => by simp only [immG, rename, subst_rename] rw [rename_closed0 (Cl_subst A (pay ∘ r) (fun n => h (r n)))] + | .fam A, pay, r, h => by simp [immG, rename, immG_rename A pay r h] | .unk, _, _, _ => rfl | .int _, _, _, _ => rfl | .tvar _, _, _, _ => rfl @@ -869,6 +904,7 @@ theorem immG_subst : ∀ (A : VTy) (pay σ : Nat → VTy), (∀ n, Cl 0 (pay n)) simp only [immG, subst] rw [subst_closed0 (Cl_subst A (fun n => subst pay (σ n)) (fun n => Cl_subst (σ n) pay h)), subst_subst] + | .fam A, pay, σ, h => by simp [immG, subst, immG_subst A pay σ h] | .unk, _, _, _ => rfl | .int _, _, _, _ => rfl | .tvar _, _, _, _ => rfl @@ -913,6 +949,7 @@ theorem immG_congr : ∀ (A : VTy) {k : Nat} {pay pay' : Nat → VTy}, Cl k A | .mut _ A, _, _, _, h, hp => by simp [immG, subst_congr A h hp] | .box _ A, _, _, _, h, hp => by simp [immG, immG_congr A h hp] | .bang A, _, _, _, h, hp => by simp [immG, subst_congr A h hp] + | .fam A, _, _, _, h, hp => by simp [immG, immG_congr A h hp] | .unk, _, _, _, _, _ => rfl | .int _, _, _, _, _, _ => rfl | .tvar _, _, _, _, _, _ => rfl @@ -935,6 +972,7 @@ theorem Cl_immG : ∀ (A : VTy) {k : Nat} (pay : Nat → VTy), (∀ n, Cl 0 (pay | .mut _ A, _, pay, hp, _ => Cl_mono _ (Cl_subst A pay hp) (Nat.zero_le _) | .box _ A, _, pay, hp, h => Cl_immG A pay hp h | .bang A, _, pay, hp, _ => Cl_mono _ (Cl_subst A pay hp) (Nat.zero_le _) + | .fam A, _, pay, hp, h => Cl_immG A pay hp h | .unk, _, _, _, _ => trivial | .int _, _, _, _, _ => trivial | .tvar _, _, _, _, h => h @@ -954,6 +992,7 @@ theorem noMut_immG : ∀ (A : VTy) {k : Nat} (pay : Nat → VTy), (∀ n, Cl 0 ( | .mut _ A, _, pay, hp => noMut_of_not_occ _ (not_occ_of_Cl _ (Cl_subst A pay hp) (Nat.zero_le _)) | .box _ A, _, pay, hp => noMut_immG A pay hp | .bang A, _, pay, hp => noMut_of_not_occ _ (not_occ_of_Cl _ (Cl_subst A pay hp) (Nat.zero_le _)) + | .fam A, _, pay, hp => noMut_immG A pay hp | .unk, _, _, _ => trivial | .int _, _, _, _ => trivial | .tvar _, _, _, _ => trivial @@ -1001,7 +1040,7 @@ theorem VTy.noMut_of_poly : ∀ {A : VTy}, A.Poly → A.NoMut 0 | .sum _ _, h => ⟨noMut_of_poly h.1, noMut_of_poly h.2⟩ | .tvar _, _ => trivial | .unit, _ | .zero, _ | .thunk _, _ | .unk, _ | .int _, _ => trivial - | .ref _, h | .imm _ _, h | .mut _ _, h | .box _ _, h | .bang _, h | .mu _, h => + | .ref _, h | .imm _ _, h | .mut _ _, h | .box _ _, h | .bang _, h | .fam _, h | .mu _, h => VTy.noMut_of_not_occ _ h namespace CTy @@ -1062,7 +1101,7 @@ theorem VTy.poly_of_not_occ : ∀ {A : VTy}, ¬ A.Occ 0 → A.Poly poly_of_not_occ (fun h' => h (Or.inr h'))⟩ | .tvar _, _ => trivial | .unit, h | .zero, h | .thunk _, h | .unk, h | .int _, h => h - | .ref _, h | .imm _ _, h | .mut _ _, h | .box _ _, h | .bang _, h | .mu _, h => h + | .ref _, h | .imm _ _, h | .mut _ _, h | .box _ _, h | .bang _, h | .fam _, h | .mu _, h => h theorem VTy.not_occ_rename_succ (A : VTy) : ¬ (A.rename Nat.succ).Occ 0 := fun h => by obtain ⟨n, hn, -⟩ := VTy.occ_rename A Nat.succ h @@ -1135,6 +1174,7 @@ theorem vFree_rename {y : LifeVar} : ∀ (A : VTy) (r : Nat → Nat), VFree y (A | .mut _ A, r => or_congr Iff.rfl (vFree_rename A r) | .box _ A, r => or_congr Iff.rfl (vFree_rename A r) | .bang A, r => vFree_rename A r + | .fam A, r => vFree_rename A r | .unk, _ => Iff.rfl | .int _, _ => Iff.rfl | .tvar _, _ => Iff.rfl @@ -1159,6 +1199,7 @@ theorem vFree_subst {y : LifeVar} : ∀ (A : VTy) (σ : Nat → VTy), | .box _ A, σ, h => h.elim (fun h => Or.inl (Or.inl h)) (fun h => (vFree_subst A σ h).elim (fun h => Or.inl (Or.inr h)) Or.inr) | .bang A, σ, h => vFree_subst A σ h + | .fam A, σ, h => vFree_subst A σ h | .unk, _, h => h.elim | .int _, _, h => h.elim | .tvar n, _, h => Or.inr ⟨n, h⟩ @@ -1182,6 +1223,7 @@ theorem vFree_subst_of {y : LifeVar} : ∀ (A : VTy) (σ : Nat → VTy), VFree y | .mut _ A, σ, h => h.elim Or.inl (fun h => Or.inr (vFree_subst_of A σ h)) | .box _ A, σ, h => h.elim Or.inl (fun h => Or.inr (vFree_subst_of A σ h)) | .bang A, σ, h => vFree_subst_of A σ h + | .fam A, σ, h => vFree_subst_of A σ h | .unk, _, h => h | .int _, _, h => h | .tvar _, _, h => h.elim @@ -1221,6 +1263,8 @@ theorem vFree_immG {y : LifeVar} (b : Lifetime.Life) : ∀ (A : VTy) (pay : Nat (fun h => h.elim (fun h => Or.inr (Or.inl (Or.inr h))) (fun h => Or.inr (Or.inr h))) | .bang A, pay, h => (vFree_subst A pay h).elim (fun h => Or.inr (Or.inl h)) (fun h => Or.inr (Or.inr h)) + | .fam A, pay, h => (vFree_immG b A pay h).elim Or.inl + (fun h => h.elim (fun h => Or.inr (Or.inl h)) (fun h => Or.inr (Or.inr h))) | .unk, _, h => h.elim | .int _, _, h => h.elim | .tvar _, _, h => h.elim @@ -1240,7 +1284,8 @@ theorem vFree_immG {y : LifeVar} (b : Lifetime.Life) : ∀ (A : VTy) (pay : Nat /-- `Imm̲ 'b A`: the view a load through an immutable borrow at `'b` gives of a payload of type `A`. `1`, `0`, `⊗`, `⊕` structurally; `U C ↦ Unk` (codata has no safe view, as `⊸` and `∀` in BoLo); `Ref A ↦ Imm 'b A`; `Imm @a A` kept; `Mut @a A ↦ Imm 'b A`; -`[@a] A ↦ Imm̲ 'b A`; `!A` kept whole (it holds no resource); `Unk ↦ Unk`. At `μX. A` the +`[@a] A ↦ Imm̲ 'b A`; `!A` kept whole (it holds no resource); `Unk ↦ Unk`; `[L] ↦ [Imm̲ 'b L]` +(`[spec §9]`). At `μX. A` the clause substitutes inside the payloads it keeps (`VTy.immG`, `docs/scope-recursion.md` §2.4): `Imm̲ 'b (μX. A) = μY. Imm̲ᵡ 'b A`, a head `X` becoming `Y` and a payload `X` becoming `μX. A`. `[spec §6]` -/ def VTy.immReborrow (b : Lifetime.Life) : VTy → VTy @@ -1254,6 +1299,7 @@ def VTy.immReborrow (b : Lifetime.Life) : VTy → VTy | .mut _ A => .imm b A | .box _ A => A.immReborrow b | .bang A => .bang A + | .fam A => .fam (A.immReborrow b) | .unk => .unk | .int w => .int w | .tvar n => .tvar n @@ -1273,6 +1319,7 @@ theorem VTy.immReborrow_eq_immG (b : Lifetime.Life) : ∀ A : VTy, A.immReborrow | .mut _ A => by simp only [VTy.immReborrow, VTy.immG, VTy.subst_id] | .box _ A => by simp only [VTy.immReborrow, VTy.immG, VTy.immReborrow_eq_immG b A] | .bang A => by simp only [VTy.immReborrow, VTy.immG, VTy.subst_id] + | .fam A => by simp only [VTy.immReborrow, VTy.immG, VTy.immReborrow_eq_immG b A] | .unk => rfl | .int _ => rfl | .tvar _ => rfl @@ -1327,6 +1374,7 @@ def VTy.lifeBound : VTy → Nat | .thunk C => C.lifeBound | .ref A => A.lifeBound | .bang A => A.lifeBound + | .fam A => A.lifeBound | .imm a A => max a.varBound A.lifeBound | .mut a A => max a.varBound A.lifeBound | .box a A => max a.varBound A.lifeBound @@ -1354,6 +1402,7 @@ def VTy.applyLSub (σ : LSubst) : VTy → VTy | .thunk C => .thunk (C.applyLSub σ) | .ref A => .ref (A.applyLSub σ) | .bang A => .bang (A.applyLSub σ) + | .fam A => .fam (A.applyLSub σ) | .imm a A => .imm (a.applySub σ) (A.applyLSub σ) | .mut a A => .mut (a.applySub σ) (A.applyLSub σ) | .box a A => .box (a.applySub σ) (A.applyLSub σ) @@ -1413,6 +1462,7 @@ theorem lt_vLifeBound {x : LifeVar} : ∀ {A : VTy}, VFree x A → x < A.lifeBou | .thunk C, h => lt_cLifeBound (C := C) h | .ref A, h => lt_vLifeBound (A := A) h | .bang A, h => lt_vLifeBound (A := A) h + | .fam A, h => lt_vLifeBound (A := A) h | .imm a A, h => by rcases h with h | h · exact Nat.lt_of_lt_of_le (lt_varBound h) (Nat.le_max_left _ _) @@ -1489,6 +1539,7 @@ theorem vFree_immReborrow {x y : LifeVar} : ∀ (A : VTy), · exact Or.inl h · exact Or.inr (Or.inr h) | bang A _ => intro h; exact Or.inr h + | fam L ih => intro h; exact ih h | tvar _ => intro h; exact h.elim | mu A _ => intro h @@ -1521,6 +1572,8 @@ inductive VOutlives (Δ : LifeCtx) (a : Lifetime.Life) : VTy → Prop where | mu {A} : VOutlives Δ a A → VOutlives Δ a (.mu A) /-- `I32`/`I64` outlive every lifetime. `[spec §8]` -/ | int {w} : VOutlives Δ a (.int w) + /-- `Δ ⊢ [L] ⊐ @a` from `Δ ⊢ L ⊐ @a`. `[spec §9]` -/ + | fam {L} : VOutlives Δ a L → VOutlives Δ a (.fam L) theorem VOutlives.rename {Δ : LifeCtx} {a : Lifetime.Life} {A : VTy} (h : VOutlives Δ a A) : ∀ r : Nat → Nat, VOutlives Δ a (A.rename r) := by @@ -1531,6 +1584,7 @@ theorem VOutlives.rename {Δ : LifeCtx} {a : Lifetime.Life} {A : VTy} (h : VOutl | sum _ _ ih₁ ih₂ => intro r; exact .sum (ih₁ r) (ih₂ r) | ref _ ih => intro r; exact .ref (ih r) | bang => intro _; exact .bang + | fam _ ih => intro r; exact .fam (ih r) | box h => intro _; exact .box h | imm h => intro _; exact .imm h | «mut» h => intro _; exact .mut h @@ -1547,6 +1601,7 @@ theorem VOutlives.subst {Δ : LifeCtx} {a : Lifetime.Life} {A : VTy} (h : VOutli | sum _ _ ih₁ ih₂ => intro σ hσ; exact .sum (ih₁ σ hσ) (ih₂ σ hσ) | ref _ ih => intro σ hσ; exact .ref (ih σ hσ) | bang => intro _ _; exact .bang + | fam _ ih => intro σ hσ; exact .fam (ih σ hσ) | box h => intro _ _; exact .box h | imm h => intro _ _; exact .imm h | «mut» h => intro _ _; exact .mut h diff --git a/Core/Weaken.lean b/Core/Weaken.lean index df9545841..f6810e300 100644 --- a/Core/Weaken.lean +++ b/Core/Weaken.lean @@ -104,6 +104,14 @@ theorem CDerives.append_dead (h' : Ctx.Dead Γ') {Δ : LifeCtx} {Γ : Ctx VTy} { | forgetUnk hΓ => exact .forgetUnk (deadP_append h' hΓ) | lit c hΓ => exact .lit c (deadP_append h' hΓ) | hostOp op hΓ => exact .hostOp op (deadP_append h' hΓ) + | arrNew A hΓ => exact .arrNew A (deadP_append h' hΓ) + | arrFree A hΓ => exact .arrFree A (deadP_append h' hΓ) + | arrSize A hΓ => exact .arrSize A (deadP_append h' hΓ) + | arrReplace A hΓ => exact .arrReplace A (deadP_append h' hΓ) + | arrLen a A hΓ => exact .arrLen a A (deadP_append h' hΓ) + | arrGet a A hΓ => exact .arrGet a A (deadP_append h' hΓ) + | arrCopy a A hΓ => exact .arrCopy a A (deadP_append h' hΓ) + | arrForget a A hΓ => exact .arrForget a A (deadP_append h' hΓ) | withbor1 x hΓ hx hsc hf₁ hf₂ => exact .withbor1 x (deadP_append h' hΓ) hx hsc hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact .withbor2 x (deadP_append h' hΓ) hx hside hf₁ hf₂ | withbor3 x hΓ hx hf₁ hf₂ => exact .withbor3 x (deadP_append h' hΓ) hx hf₁ hf₂ diff --git a/Core/World.lean b/Core/World.lean index 391fafe32..acd7308b6 100644 --- a/Core/World.lean +++ b/Core/World.lean @@ -8,6 +8,7 @@ import Support.Lifetimes.Interpretation import Support.LogicalRelation.ClosingSubstitutions import Support.Model.Notation import Core.FrameRecords +import Core.SepRange /-! # Core — World `[spec §3, §4]` @@ -75,6 +76,8 @@ def vShapeE : Option (Nat → SEnt) → VTy → LSub → Val → WProp ∀ P : Val → WProp, (∀ v' ρ', vShapeE (some (VTy.scons ⟨sClose η (.mu A), P⟩ (sEnv η))) A δ v' ρ' → P v' ρ') → P v ρ + | η, .fam L, δ, v => ex fun ℓ => ex fun c => ⌜v = Val.arr ℓ c⌝ ⋆ + sepRange (fun m => vShapeE η L δ (.loc m)) ℓ c.toNat /-- `𝒱⟦A⟧`'s shape at a type read with no environment. -/ notation "vShape" => vShapeE none diff --git a/Frontend/Check/Consts.lean b/Frontend/Check/Consts.lean index 04db2ca8e..b4f93456a 100644 --- a/Frontend/Check/Consts.lean +++ b/Frontend/Check/Consts.lean @@ -258,6 +258,34 @@ def synConst (E : Env) (Δ : LifeCtx) (T : List CVTy) (sp : Span) : s!"`{ppVTy T'}` must outlive the family's outer borrow" pure (cStrict _ fun hd => withborM2_wf s hA (outlivesB_sound _ _ _ hBo) (outlivesB_sound _ _ _ hTo) hd) + | .arrayNew A => do + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrNew A hd) + | .arrayFree A => do + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrFree A hd) + | .arraySize A => do + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrSize A hd) + | .arrayReplace A => do + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrReplace A hd) + | .arrayLen a A => do + let a ← E.elabL Δ a + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrLen a A hd) + | .arrayGet a A => do + let a ← E.elabL Δ a + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrGet a A hd) + | .arrayCopy a A => do + let a ← E.elabL Δ a + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrCopy a A hd) + | .arrayForget a A => do + let a ← E.elabL Δ a + let A ← E.elabV Δ A + pure (cDead _ fun hd => CDerives.arrForget a A hd) | .withborF2Imm _ _ _ A B T' => do let A ← E.elabV Δ A let B ← E.elabV Δ B diff --git a/Frontend/Check/Decide.lean b/Frontend/Check/Decide.lean index 92fb9d7c5..c5937f2e6 100644 --- a/Frontend/Check/Decide.lean +++ b/Frontend/Check/Decide.lean @@ -48,6 +48,7 @@ def VTy.clB : Nat → VTy → Bool | k, .mut _ A => VTy.clB k A | k, .box _ A => VTy.clB k A | k, .bang A => VTy.clB k A + | k, .fam A => VTy.clB k A | _, .unk => true | _, .int _ => true | k, .tvar n => decide (n < k) @@ -68,6 +69,7 @@ theorem VTy.clB_sound : ∀ (k : Nat) (A : VTy), VTy.clB k A = true → A.Cl k | k, .mut _ A, h => clB_sound k A h | k, .box _ A, h => clB_sound k A h | k, .bang A, h => clB_sound k A h + | k, .fam A, h => clB_sound k A h | _, .unk, _ => trivial | _, .int _, _ => trivial | k, .tvar n, h => by simpa [VTy.clB, VTy.Cl] using h @@ -85,6 +87,7 @@ def VTy.occB : Nat → VTy → Bool | k, .mut _ A => VTy.occB k A | k, .box _ A => VTy.occB k A | k, .bang A => VTy.occB k A + | k, .fam A => VTy.occB k A | _, .unk => false | _, .int _ => false | k, .tvar n => n == k @@ -105,6 +108,7 @@ theorem VTy.not_occ_of_occB : ∀ (k : Nat) (A : VTy), VTy.occB k A = false → | k, .mut _ A, hb, h => not_occ_of_occB k A hb h | k, .box _ A, hb, h => not_occ_of_occB k A hb h | k, .bang A, hb, h => not_occ_of_occB k A hb h + | k, .fam A, hb, h => not_occ_of_occB k A hb h | _, .unk, _, h => h | _, .int _, _, h => h | k, .tvar n, hb, h => by @@ -124,6 +128,7 @@ def VTy.noMutB : Nat → VTy → Bool | k, .mut _ A => !VTy.occB k A | k, .box _ A => VTy.noMutB k A | k, .bang A => VTy.noMutB k A + | k, .fam A => VTy.noMutB k A | _, .unk => true | _, .int _ => true | _, .tvar _ => true @@ -146,6 +151,7 @@ theorem VTy.noMutB_sound : ∀ (k : Nat) (A : VTy), VTy.noMutB k A = true → A. exact VTy.not_occ_of_occB k A h | k, .box _ A, h => noMutB_sound k A h | k, .bang A, h => noMutB_sound k A h + | k, .fam A, h => noMutB_sound k A h | _, .unk, _ => trivial | _, .int _, _ => trivial | _, .tvar _, _ => trivial @@ -169,7 +175,7 @@ theorem VTy.polyB_sound : ∀ A : VTy, VTy.polyB A = true → A.Poly exact ⟨polyB_sound A h.1, polyB_sound B h.2⟩ | .tvar _, _ => trivial | .unit, h | .zero, h | .thunk _, h | .unk, h | .int _, h | .ref _, h | .imm _ _, h - | .mut _ _, h | .box _ _, h | .bang _, h | .mu _, h => + | .mut _ _, h | .box _ _, h | .bang _, h | .mu _, h | .fam _, h => VTy.not_occ_of_occB 0 _ (by simpa [VTy.polyB] using h) /-- `CTy.NuLeaf`, decided. -/ @@ -224,6 +230,7 @@ def vfreeB (x : LifeVar) : VTy → Bool | .thunk C => cfreeB x C | .ref A => vfreeB x A | .bang A => vfreeB x A + | .fam A => vfreeB x A | .imm a A => a.mentions x || vfreeB x A | .mut a A => a.mentions x || vfreeB x A | .box a A => a.mentions x || vfreeB x A @@ -250,6 +257,7 @@ theorem vfreeB_iff (x : LifeVar) : ∀ A : VTy, vfreeB x A = true ↔ VFree x A | .thunk C => by simp [vfreeB, VFree, cfreeB_iff x C] | .ref A => by simp [vfreeB, VFree, vfreeB_iff x A] | .bang A => by simp [vfreeB, VFree, vfreeB_iff x A] + | .fam A => by simp [vfreeB, VFree, vfreeB_iff x A] | .imm a A => by simp [vfreeB, VFree, vfreeB_iff x A] | .mut a A => by simp [vfreeB, VFree, vfreeB_iff x A] | .box a A => by simp [vfreeB, VFree, vfreeB_iff x A] @@ -416,6 +424,7 @@ def outlivesB (Δ : LifeCtx) (a : Lifetime.Life) : VTy → Bool | .zero => true | .int _ => true | .bang _ => true + | .fam A => outlivesB Δ a A | .tvar _ => true | .tensor A B => outlivesB Δ a A && outlivesB Δ a B | .sum A B => outlivesB Δ a A && outlivesB Δ a B @@ -433,6 +442,7 @@ theorem outlivesB_sound (Δ : LifeCtx) (a : Lifetime.Life) : | .zero, _ => .zero | .int _, _ => .int | .bang _, _ => .bang + | .fam A, h => .fam (outlivesB_sound Δ a A h) | .tvar _, _ => .tvar | .tensor A B, h => by simp only [outlivesB, Bool.and_eq_true] at h @@ -456,6 +466,7 @@ def outlivesAllB : VTy → Bool | .zero => true | .int _ => true | .bang _ => true + | .fam A => outlivesAllB A | .tvar _ => true | .tensor A B => outlivesAllB A && outlivesAllB B | .sum A B => outlivesAllB A && outlivesAllB B @@ -472,6 +483,7 @@ theorem outlivesAllB_sound : ∀ R : VTy, outlivesAllB R = true → ∀ Δ' a, V | .zero, _, _, _ => .zero | .int _, _, _, _ => .int | .bang _, _, _, _ => .bang + | .fam A, h, Δ', a => .fam (outlivesAllB_sound A h Δ' a) | .tvar _, _, _, _ => .tvar | .tensor A B, h, Δ', a => by simp only [outlivesAllB, Bool.and_eq_true] at h @@ -523,6 +535,7 @@ def vScopedB (Δ : LifeCtx) (ex : List LifeVar) : VTy → Bool | .thunk C => cScopedB Δ ex C | .ref A => vScopedB Δ ex A | .bang A => vScopedB Δ ex A + | .fam A => vScopedB Δ ex A | .mu A => vScopedB Δ ex A | .imm a A => lifeScopedB Δ ex a && vScopedB Δ ex A | .mut a A => lifeScopedB Δ ex a && vScopedB Δ ex A @@ -554,6 +567,7 @@ theorem vScopedB_sound {Δ : LifeCtx} {y : LifeVar} : ∀ {ex : List LifeVar} (A | _, .thunk C, h, hf => cScopedB_sound C h hf | _, .ref A, h, hf => vScopedB_sound A h hf | _, .bang A, h, hf => vScopedB_sound A h hf + | _, .fam A, h, hf => vScopedB_sound A h hf | _, .mu A, h, hf => vScopedB_sound A h hf | _, .imm a A, h, hf => by simp only [vScopedB, Bool.and_eq_true] at h diff --git a/Frontend/Check/Elab.lean b/Frontend/Check/Elab.lean index cbc5a6f9c..a4efd31ec 100644 --- a/Frontend/Check/Elab.lean +++ b/Frontend/Check/Elab.lean @@ -120,6 +120,9 @@ def ppV (nm : List (String × LifeVar)) (d p : Nat) : CVTy → String | .int .w64 => "i64" | .tvar n => if n < d then tvName (d - 1 - n) else s!"?{n}" | .mu A => parenIf (p > 0) s!"mu {tvName d}. {ppV nm (d + 1) 0 A}" + | .fam (.ref A) => parenIf (p > 5) s!"array {ppV nm d 5 A}" + | .fam (.imm a A) => parenIf (p > 5) s!"array_imm {ppLifeP nm 1 a} {ppV nm d 5 A}" + | .fam L => parenIf (p > 5) s!"[{ppV nm d 0 L}]" /-- A computation type in the surface syntax, at precedence `p`. -/ def ppC (nm : List (String × LifeVar)) (p : Nat) : CCTy → String | .ret A => parenIf (p > 5) s!"F {ppV nm 0 5 A}" @@ -237,6 +240,8 @@ def elabV : Nat → TyEnv → VTy → Except Error CVTy | .mut => s.famTy BoCa.Fig16.LogRel.Core.VTy.mut al | .imm => s.famTy BoCa.Fig16.LogRel.Core.VTy.imm al) | .view _ a A => do pure ((← elabV n E A).immReborrow (← elabLife E.Δ E.lenv a)) + | .array _ A => do pure (.fam (.ref (← elabV n E A))) + | .arrayImm _ a A => do pure (.fam (.imm (← elabLife E.Δ E.lenv a) (← elabV n E A))) /-- A computation type. -/ def elabC : Nat → TyEnv → CTy → Except Error CCTy | 0, _, C => err C.span "type abbreviations nested too deeply" diff --git a/Frontend/Check/Steps.lean b/Frontend/Check/Steps.lean index bf989e593..7e196b43d 100644 --- a/Frontend/Check/Steps.lean +++ b/Frontend/Check/Steps.lean @@ -64,6 +64,9 @@ def dtorB : Bool → CVTy → Bool CTy.nuOkB (.amp C (.ret R)) && VTy.clB 0 R && dtorB false R | _, .thunk _ => false | _, .unit | _, .zero | _, .int _ | _, .unk | _, .bang _ | _, .imm _ _ | _, .mut _ _ => true + | _, .fam (.ref A) => dtorB false A + | _, .fam (.imm _ _) => true + | _, .fam _ => false theorem dtorB_sound : ∀ (alg : Bool) (A : CVTy), dtorB alg A = true → HasDtor (cond alg ((toFShape A).resO .unit) A) @@ -122,7 +125,8 @@ theorem dtorB_sound : ∀ (alg : Bool) (A : CVTy), dtorB alg A = true → | _, .thunk (.amp _ (.ret (.mut _ _))), h | _, .thunk (.amp _ (.ret (.box _ _))), h | _, .thunk (.amp _ (.ret (.bang _))), h | _, .thunk (.amp _ (.ret .unk)), h | _, .thunk (.amp _ (.ret (.tvar _))), h | _, .thunk (.amp _ (.ret (.mu _))), h - | _, .thunk (.amp _ (.ret (.int _))), h | _, .thunk (.amp _ (.arr _ _)), h + | _, .thunk (.amp _ (.ret (.int _))), h | _, .thunk (.amp _ (.ret (.fam _))), h + | _, .thunk (.amp _ (.arr _ _)), h | _, .thunk (.amp _ (.amp _ _)), h | _, .thunk (.amp _ .top), h | _, .thunk (.amp _ (.all _ _ _)), h | _, .thunk (.amp _ (.nu _)), h => by simp [dtorB] at h | alg, .unit, _ => by cases alg <;> exact .unit @@ -132,6 +136,14 @@ theorem dtorB_sound : ∀ (alg : Bool) (A : CVTy), dtorB alg A = true → | alg, .bang _, _ => by cases alg <;> exact .bang | alg, .imm _ _, _ => by cases alg <;> exact .imm | alg, .mut _ _, _ => by cases alg <;> exact .mut + | alg, .fam (.ref A), h => by + have := HasDtor.array (dtorB_sound false A (by simpa [dtorB] using h)) + cases alg <;> exact this + | alg, .fam (.imm _ _), _ => by cases alg <;> exact .arrayImm + | _, .fam .unit, h | _, .fam (.tensor _ _), h | _, .fam .zero, h | _, .fam (.sum _ _), h + | _, .fam (.thunk _), h | _, .fam (.mut _ _), h | _, .fam (.box _ _), h + | _, .fam (.bang _), h | _, .fam .unk, h | _, .fam (.tvar _), h | _, .fam (.mu _), h + | _, .fam (.int _), h | _, .fam (.fam _), h => by simp [dtorB] at h /-- `A` has a destructor. -/ def hasDtorB (A : CVTy) : Bool := dtorB false A diff --git a/Frontend/Consts.lean b/Frontend/Consts.lean index b97810823..20ffe8308 100644 --- a/Frontend/Consts.lean +++ b/Frontend/Consts.lean @@ -24,6 +24,8 @@ inductive CKw where | foldImm | foldOwn | foldReuse | fixd | unfoldS | unfold | i32 | i64 | host | allocS | freeS | withborSMut | withborSImm | withborF2Mut | withborF2Imm + | arrayNew | arrayFree | arraySize | arrayReplace | arrayLen | arrayGet | arrayCopy + | arrayForget deriving DecidableEq, Repr /-- A constant keyword's spelling. -/ @@ -39,12 +41,17 @@ def CKw.str : CKw → String | .allocS => "alloc_s" | .freeS => "free_s" | .withborSMut => "withbor_s_mut" | .withborSImm => "withbor_s_imm" | .withborF2Mut => "withbor_f2_mut" | .withborF2Imm => "withbor_f2_imm" + | .arrayNew => "array_new" | .arrayFree => "array_free" | .arraySize => "array_size" + | .arrayReplace => "array_replace" | .arrayLen => "array_len" | .arrayGet => "array_get" + | .arrayCopy => "array_copy" | .arrayForget => "array_forget" def CKw.all : List CKw := [.alloc, .free, .swap, .copy, .copyBang, .forgetImm, .forgetMut, .forgetUnk, .forgetBang, .withbor1, .withbor2, .withbor3, .withload, .withswap, .foldImm, .foldOwn, .foldReuse, .fixd, .unfoldS, .unfold, .i32, .i64, .host, - .allocS, .freeS, .withborSMut, .withborSImm, .withborF2Mut, .withborF2Imm] + .allocS, .freeS, .withborSMut, .withborSImm, .withborF2Mut, .withborF2Imm, + .arrayNew, .arrayFree, .arraySize, .arrayReplace, .arrayLen, .arrayGet, .arrayCopy, + .arrayForget] def CKw.ofString? (s : String) : Option CKw := CKw.all.find? (·.str == s) @@ -53,6 +60,8 @@ open ArgKind in without brackets. -/ def CKw.sig : CKw → List ArgKind | .alloc | .free | .copyBang | .forgetBang => [ty] + | .arrayNew | .arrayFree | .arraySize | .arrayReplace => [ty] + | .arrayLen | .arrayGet | .arrayCopy | .arrayForget => [life, ty] | .swap | .foldOwn | .foldReuse | .fixd | .unfoldS => [ty, ty] | .unfold => [cty, ty] | .copy | .forgetImm | .forgetMut => [life, ty] @@ -102,6 +111,14 @@ def Const.ofArgs : CKw → List Arg → Option Const some (.withborF2Mut c a b w A B T) | .withborF2Imm, [.binder c, .binder a, .binder b, .ty A, .ty B, .ty T] => some (.withborF2Imm c a b A B T) + | .arrayNew, [.ty A] => some (.arrayNew A) + | .arrayFree, [.ty A] => some (.arrayFree A) + | .arraySize, [.ty A] => some (.arraySize A) + | .arrayReplace, [.ty A] => some (.arrayReplace A) + | .arrayLen, [.life a, .ty A] => some (.arrayLen a A) + | .arrayGet, [.life a, .ty A] => some (.arrayGet a A) + | .arrayCopy, [.life a, .ty A] => some (.arrayCopy a A) + | .arrayForget, [.life a, .ty A] => some (.arrayForget a A) | _, _ => none /-- A constant's keyword and bracket arguments. -/ @@ -137,6 +154,14 @@ def Const.toArgs : Const → CKw × List Arg (.withborF2Mut, [.binder c, .binder a, .binder b, .life w, .ty A, .ty B, .ty T]) | .withborF2Imm c a b A B T => (.withborF2Imm, [.binder c, .binder a, .binder b, .ty A, .ty B, .ty T]) + | .arrayNew A => (.arrayNew, [.ty A]) + | .arrayFree A => (.arrayFree, [.ty A]) + | .arraySize A => (.arraySize, [.ty A]) + | .arrayReplace A => (.arrayReplace, [.ty A]) + | .arrayLen a A => (.arrayLen, [.life a, .ty A]) + | .arrayGet a A => (.arrayGet, [.life a, .ty A]) + | .arrayCopy a A => (.arrayCopy, [.life a, .ty A]) + | .arrayForget a A => (.arrayForget, [.life a, .ty A]) theorem Const.ofArgs_toArgs (c : Const) : Const.ofArgs c.toArgs.1 c.toArgs.2 = some c := by cases c with diff --git a/Frontend/Lexer.lean b/Frontend/Lexer.lean index 3f8b0de44..4baf38bd5 100644 --- a/Frontend/Lexer.lean +++ b/Frontend/Lexer.lean @@ -55,13 +55,14 @@ def keywords : List String := -- types, lifetimes, shapes "ref", "imm", "mut", "box", "unk", "i32", "i64", "mu", "all", "forall", "top", "dom", "block", "val", "fam", "view", "nu", "delay", "stream", "str", "iter", "leaf", - "node", + "node", "array", "array_imm", -- constants and derived forms "alloc", "free", "swap", "copy", "copy_bang", "forget_imm", "forget_mut", "forget_unk", "forget_bang", "withbor1", "withbor2", "withbor3", "withload", "withswap", "fold_imm", "fold_own", "fold_reuse", "fixd", "unfold_s", "unfold", "host", "alloc_s", "free_s", "withbor_s_mut", "withbor_s_imm", "withbor_f2_mut", "withbor_f2_imm", - "shorten_mut", "shorten_imm" ] + "shorten_mut", "shorten_imm", "array_new", "array_free", "array_size", "array_replace", + "array_len", "array_get", "array_copy", "array_forget" ] /-- The reserved upper-case words. -/ def upperKeywords : List String := ["U", "F", "Fun"] diff --git a/Frontend/Parser.lean b/Frontend/Parser.lean index a94211a85..4cbdb2934 100644 --- a/Frontend/Parser.lean +++ b/Frontend/Parser.lean @@ -55,6 +55,8 @@ inductive RTy where | val (sp : Span) (σ : Shape) | fam (sp : Span) (m : Mode) (a : Life) (σ : Shape) | view (sp : Span) (a : Life) (A : RTy) + | array (sp : Span) (A : RTy) + | arrayImm (sp : Span) (a : Life) (A : RTy) | nu (sp : Span) (X : Ident) (C : RTy) | delay (sp : Span) (A : RTy) | stream (sp : Span) (A : RTy) @@ -65,7 +67,7 @@ def RTy.span : RTy → Span | .imm sp _ _ | .mut sp _ _ | .box sp _ _ | .bang sp _ | .unk sp | .int sp _ | .mu sp _ _ | .name sp _ | .lolli sp _ _ | .arr sp _ _ | .all sp _ _ _ | .forall_ sp _ _ _ | .amp sp _ _ | .top sp | .block sp _ | .val sp _ | .fam sp _ _ _ | .view sp _ _ | .nu sp _ _ | .delay sp _ - | .stream sp _ | .iter sp _ _ _ => sp + | .stream sp _ | .iter sp _ _ _ | .array sp _ | .arrayImm sp _ _ => sp /-- What a raw type is, for sort errors. -/ def RTy.former : RTy → String @@ -103,6 +105,8 @@ def RTy.toV : RTy → Except Error VTy | .val sp σ => pure (.val sp σ) | .fam sp m a σ => pure (.fam sp m a σ) | .view sp a A => return .view sp a (← A.toV) + | .array sp A => return .array sp (← A.toV) + | .arrayImm sp a A => return .arrayImm sp a (← A.toV) | t@(.F ..) | t@(.arr ..) | t@(.forall_ ..) | t@(.amp ..) | t@(.top ..) | t@(.nu ..) | t@(.delay ..) | t@(.stream ..) | t@(.iter ..) => sortErr t true @@ -339,6 +343,8 @@ def rtyPrefix : Nat → P RTy | .kw "U" => unary .U | .kw "F" => unary .F | .kw "ref" => unary .ref + | .kw "array" => unary .array + | .kw "array_imm" => withLife .arrayImm | .sym "!" => unary .bang | .kw "delay" => unary .delay | .kw "stream" | .kw "str" => unary .stream diff --git a/Frontend/Print.lean b/Frontend/Print.lean index fab6db2e2..bd7cdc36e 100644 --- a/Frontend/Print.lean +++ b/Frontend/Print.lean @@ -38,7 +38,7 @@ def VTy.level : VTy → Nat | .sum .. => 2 | .tensor .. => 3 | .thunk .. | .ref .. | .imm .. | .mut .. | .box .. | .bang .. | .block .. | .val .. - | .fam .. | .view .. => 4 + | .fam .. | .view .. | .array .. | .arrayImm .. => 4 | _ => 5 def CTy.level : CTy → Nat @@ -80,6 +80,8 @@ def VTy.pp (A : VTy) (lvl : Nat := 0) : String := | .val _ σ => "val " ++ ppShapeAtom σ σ.pp | .fam _ m a σ => "fam " ++ m.pp ++ " " ++ a.pp 2 ++ " " ++ ppShapeAtom σ σ.pp | .view _ a A => "view " ++ a.pp 2 ++ " " ++ A.pp 4 + | .array _ A => "array " ++ A.pp 4 + | .arrayImm _ a A => "array_imm " ++ a.pp 2 ++ " " ++ A.pp 4 def CTy.pp (C : CTy) (lvl : Nat := 0) : String := paren (C.level < lvl) <| match C with diff --git a/Frontend/README.md b/Frontend/README.md index a56632a3a..dd8d00d39 100644 --- a/Frontend/README.md +++ b/Frontend/README.md @@ -1,7 +1,7 @@ # Frontend: the core syntax A lexer, parser, printer and type checker for the textual core language of -`docs/syntax-core.md` (draft 1, with §8a integers). The checker elaborates a program to a BoLo +`docs/syntax-core.md` (draft 1, with §8a integers and §8b arrays). The checker elaborates a program to a BoLo `Expr` together with a `CDerives` derivation of it (`Frontend/Check/`, below). lake exe bcc parse parse, then print, re-parse and compare the ASTs up to spans @@ -15,7 +15,7 @@ A lexer, parser, printer and type checker for the textual core language of |---|---| | `Syntax.lean` | the AST: `Life`, `VTy`/`CTy`/`Shape`, `Const`, `Val`/`Comp`, `Decl`, `Program`; every node has a `Span`; `strip` erases spans | | `Lexer.lean` | tokens of §2, with line:column spans; Unicode spellings read as their ASCII tokens | -| `Consts.lean` | bracket signatures of the constants (§8, §8a, §9) and host-operation names, with `Const.ofArgs_toArgs` and `hostOpOfName_name` | +| `Consts.lean` | bracket signatures of the constants (§8, §8a, §8b, §9) and host-operation names, with `Const.ofArgs_toArgs` and `hostOpOfName_name` | | `Parser.lean` | recursive descent for §3–§6 and §10; types parsed to `RTy` then sorted into `VTy`/`CTy` | | `Print.lean` | the printer, parenthesizing by the precedence levels of §4 and §6.5 | | `Tests.lean` | precedence cases and round trips checked by the kernel (`decide +kernel`) | @@ -112,7 +112,7 @@ judgment, for the derived forms of syntax-core §9), so what the checker returns | `Check/Elab.lean` | surface lifetimes, types and shapes to `Core`'s; a printer back | | `Check/Rules.lean` | one builder per `CDerives` constructor, and `rDrop` (SPEC §12) | | `Check/Steps.lean` | the environment, messages, the deciders of fold shapes and destructors, the drops, and the steps shared by the checker's cases | -| `Check/Consts.lean` | constants (§8, §8a) and derived forms (§9) | +| `Check/Consts.lean` | constants (§8, §8a, §8b) and derived forms (§9) | | `Check/Checker.lean` | the bidirectional checker, `synV`/`chkV`/`synC`/`chkC` | | `Check/Program.lean` | declarations, `main`, and the soundness theorems | | `Check/CrossCheck.lean` | erasures compared with `Core/`'s hand-written terms, by kernel evaluation | diff --git a/Frontend/Syntax.lean b/Frontend/Syntax.lean index 9bae7d7e4..7d0de78e9 100644 --- a/Frontend/Syntax.lean +++ b/Frontend/Syntax.lean @@ -101,6 +101,10 @@ inductive VTy where | fam (sp : Span) (m : Mode) (a : Life) (σ : Shape) /-- `view @ A`, that is `Imm̲ @ A`. -/ | view (sp : Span) (a : Life) (A : VTy) + /-- `array A`: an owned array, the family of its cells (SPEC §9). -/ + | array (sp : Span) (A : VTy) + /-- `array_imm @ A`: the family of leaf views of an array (SPEC §9). -/ + | arrayImm (sp : Span) (a : Life) (A : VTy) /-- Computation types (syntax-core §4). -/ inductive CTy where @@ -184,6 +188,17 @@ inductive Const where | withborF2Mut (c a b : Ident) (w : Life) (A B T : VTy) /-- `withbor_f2_imm['c, 'a, 'b, A, B, T]`. -/ | withborF2Imm (c a b : Ident) (A B T : VTy) + /-- The array constants of SPEC §9: `array_new[A]`, `array_free[A]`, `array_size[A]`, + `array_replace[A]`, and on a family of leaf views `array_len[@, A]`, `array_get[@, A]`, + `array_copy[@, A]`, `array_forget[@, A]`. -/ + | arrayNew (A : VTy) + | arrayFree (A : VTy) + | arraySize (A : VTy) + | arrayReplace (A : VTy) + | arrayLen (a : Life) (A : VTy) + | arrayGet (a : Life) (A : VTy) + | arrayCopy (a : Life) (A : VTy) + | arrayForget (a : Life) (A : VTy) deriving DecidableEq, Repr mutual @@ -311,6 +326,8 @@ def VTy.strip : VTy → VTy | .val _ σ => .val .none σ.strip | .fam _ m a σ => .fam .none m a.strip σ.strip | .view _ a A => .view .none a.strip A.strip + | .array _ A => .array .none A.strip + | .arrayImm _ a A => .arrayImm .none a.strip A.strip def CTy.strip : CTy → CTy | .ret _ A => .ret .none A.strip @@ -359,6 +376,14 @@ def Const.strip : Const → Const | .withborF2Mut c a b w A B T => .withborF2Mut c.strip a.strip b.strip w.strip A.strip B.strip T.strip | .withborF2Imm c a b A B T => .withborF2Imm c.strip a.strip b.strip A.strip B.strip T.strip + | .arrayNew A => .arrayNew A.strip + | .arrayFree A => .arrayFree A.strip + | .arraySize A => .arraySize A.strip + | .arrayReplace A => .arrayReplace A.strip + | .arrayLen a A => .arrayLen a.strip A.strip + | .arrayGet a A => .arrayGet a.strip A.strip + | .arrayCopy a A => .arrayCopy a.strip A.strip + | .arrayForget a A => .arrayForget a.strip A.strip mutual def Val.strip : Val → Val @@ -434,7 +459,8 @@ def Life.span : Life → Span def VTy.span : VTy → Span | .unit sp | .zero sp | .tensor sp _ _ | .sum sp _ _ | .thunk sp _ | .ref sp _ | .imm sp _ _ | .mut sp _ _ | .box sp _ _ | .bang sp _ | .unk sp | .int sp _ | .mu sp _ _ | .name sp _ - | .lolli sp _ _ | .all sp _ _ _ | .block sp _ | .val sp _ | .fam sp _ _ _ | .view sp _ _ => sp + | .lolli sp _ _ | .all sp _ _ _ | .block sp _ | .val sp _ | .fam sp _ _ _ | .view sp _ _ + | .array sp _ | .arrayImm sp _ _ => sp def CTy.span : CTy → Span | .ret sp _ | .arr sp _ _ | .amp sp _ _ | .top sp | .all sp _ _ _ | .nu sp _ _ | .name sp _ diff --git a/Frontend/Tests.lean b/Frontend/Tests.lean index cb092870a..0c5fc37a7 100644 --- a/Frontend/Tests.lean +++ b/Frontend/Tests.lean @@ -93,6 +93,10 @@ theorem rt_ty : roundTrips parseVTy VTy.strip (VTy.pp · 0) "!U (Nat -o F (all 'y < top. imm 'y Nat => Nat) & top) * mu X. 1 + ref X" = true := by decide +kernel +theorem rt_array : roundTrips parseVTy VTy.strip (VTy.pp · 0) + "array (ref i32) * array_imm 'a i32 => ref array i32" = true := by + decide +kernel + theorem rt_program : roundTrips parseProgram Program.strip Program.pp "type Nat = mu X. 1 + X shape Node = node (leaf Nat) (leaf (ref Nat)) diff --git a/Paper/LiteralReadings/S6_8_FundamentalProperty.lean b/Paper/LiteralReadings/S6_8_FundamentalProperty.lean index cb79509f6..23f0ffb66 100644 --- a/Paper/LiteralReadings/S6_8_FundamentalProperty.lean +++ b/Paper/LiteralReadings/S6_8_FundamentalProperty.lean @@ -493,6 +493,9 @@ theorem step_beta_inv {μ μ' : Heap} {b : Expr} {v : Val} {e' : Expr} | case₁ => simp at hE | case₂ => simp at hE | host => simp at hE + | tab => simp at hE + | idx => simp at hE + | drop => simp at hE · exact absurd ha (no_step_isVal v.2) · exact absurd hf (no_step_isVal .lam) @@ -516,6 +519,9 @@ theorem step_load_inv {μ μ' : Heap} {ℓ : Loc} {e' : Expr} | case₁ => simp at hE | case₂ => simp at hE | host => simp at hE + | tab => simp at hE + | idx => simp at hE + | drop => simp at hE · exact absurd ha (no_step_isVal .loc) · exact absurd hf (no_step_isVal .prim) @@ -540,6 +546,9 @@ theorem step_store_inv {μ μ' : Heap} {ℓ : Loc} {v : Val} {e' : Expr} | case₁ => simp at hE | case₂ => simp at hE | host => simp at hE + | tab => simp at hE + | idx => simp at hE + | drop => simp at hE · exact absurd ha (no_step_isVal v.2) · exact absurd hf (no_step_isVal (.storeV .loc)) diff --git a/Paper/S1_Syntax/Definitions.lean b/Paper/S1_Syntax/Definitions.lean index 62d9d7a6b..dfd9f306a 100644 --- a/Paper/S1_Syntax/Definitions.lean +++ b/Paper/S1_Syntax/Definitions.lean @@ -41,13 +41,21 @@ abbrev Loc := Nat ### 1.14 · `Prim ::= … ∣ store` · [TR] p. 1 · `[as printed]` -/ /-- The four nullary productions of `[TR]` p. 1's `Prim`; `store v` is row 1.15. - `lit` and `host` are the integer constants of `SPEC.md` §8. -/ + `lit` and `host` are the integer constants of `SPEC.md` §8; `tab`, `idx` and `drop` the + array primitives of §9. -/ inductive Prim where | alloc | free | load | store /-- An integer literal. `[spec §8]` -/ | lit (c : IntLit) /-- A host operation, applied by the machine's host step. `[spec §8]` -/ | host (op : HostOp) + /-- Array creation: `tab (c, f)` places the cells and fills them from `f`. `[spec §9]` -/ + | tab + /-- Array indexing: `idx ((ℓ, n), i)`, bounds-checked. `[spec §9]` -/ + | idx + /-- Array disposal: `drop (f, (ℓ, n))` frees each cell and hands its contents to `f`. + `[spec §9]` -/ + | drop deriving DecidableEq, Repr /-! diff --git a/Paper/S3_Dynamics/Definitions.lean b/Paper/S3_Dynamics/Definitions.lean index 2790357d3..5a849ef6f 100644 --- a/Paper/S3_Dynamics/Definitions.lean +++ b/Paper/S3_Dynamics/Definitions.lean @@ -2,6 +2,7 @@ import Mathlib import Paper.S1_Syntax.Definitions import Support.Syntax.Terms import Support.Dynamics.Host +import Support.Dynamics.Arrays /-! # [TR] §3 Dynamics (physical pp. 3–4) @@ -182,6 +183,10 @@ def upd (μ : Heap) (ℓ : Loc) (v : Val) : Heap := fun k => if k = ℓ then som @[simp] theorem upd_other {μ : Heap} {ℓ k : Loc} {v : Val} (h : k ≠ ℓ) : upd μ ℓ v k = μ k := by simp [upd, h] +/-- `μ ⊎ {ℓ+i ↦ v | i < n}`, the result of the array step `tab`. `[spec §9]` -/ +def fill (μ : Heap) (ℓ : Loc) (n : Nat) (v : Val) : Heap := + fun k => if ℓ ≤ k ∧ k < ℓ + n then some v else μ k + end BoCa.BoLo.Heap namespace BoCa.TR3 @@ -281,10 +286,16 @@ As row 3.22. `(µ, h v) ↦ (µ, r)` when the host applies the operation `h` to `v` with result `r` (`Host.call`). The machine is parametric in the host: every definition over it takes -`{ℍ : Host} [HostIs ℍ]`, and at each host it is deterministic. +`{ℍ : Host} [HostIs ℍ]`, and at each host it is deterministic up to the choice of fresh +locations. + +### — no printed counterpart — the array steps · `[spec §9]` + +`tab` places `len(c)` cells at fresh consecutive locations and continues as `tabFill`; `idx` +answers `idxAns`; `drop` continues as `dropFill` (`Support/Dynamics/Arrays.lean`). -/ -/-- The head reductions of `[TR]` p. 4's `↦` box: `TR3.Head` under other names, and the host - step of `SPEC.md` §8. -/ +/-- The head reductions of `[TR]` p. 4's `↦` box: `TR3.Head` under other names, the host + step of `SPEC.md` §8, and the array steps of §9. -/ inductive Head {ℍ : Host} [HostIs ℍ] : Heap → Expr → Heap → Expr → Prop where | beta (μ : Heap) (b : Expr) (v : Val) : Head μ (.app (.val (.lam b)) (.val v)) μ (b.subst 0 v) @@ -309,6 +320,19 @@ inductive Head {ℍ : Host} [HostIs ℍ] : Heap → Expr → Heap → Expr → P /-- The host step: `Host.call op v = some r` ⟹ `(μ, op v) ↦ (μ, r)`. `[spec §8]` -/ | host (μ : Heap) (op : HostOp) (v r : Val) (h : ℍ.call op v = some r) : Head μ (.app (.val (.prim (.host op))) (.val v)) μ (.val r) + /-- `(μ, tab (c, f)) ↦ (μ ⊎ {ℓ+i ↦ ()}_{i .val (Val.arr ℓ n) + | i, k + 1 => + .seq (.app (.app (.val (.prim .store)) (.val (.loc (ℓ + i)))) + (.app (.val f) (.val (Val.i32 (UInt32.ofNat i))))) + (tabFill f ℓ n (i + 1) k) + +/-- `f (free (ℓ+i)); …; f (free (ℓ+i+k−1)); ()`. `[spec §9]` -/ +def dropFill (f : Val) (ℓ : Loc) : Nat → Nat → Expr + | _, 0 => .val .unit + | i, k + 1 => + .seq (.app (.val f) (.app (.val (.prim .free)) (.val (.loc (ℓ + i))))) + (dropFill f ℓ (i + 1) k) + +/-- The answer of `idx ((ℓ, n), i)`: the `i`-th leaf when `i < n`, unsigned; else `inj₂ ()`. + `[spec §9]` -/ +def idxAns (ℓ : Loc) (n i : UInt32) : Val := + if i < n then .inj₁ (.loc (ℓ + i.toNat)) else .inj₂ .unit + +end BoCa diff --git a/Support/Dynamics/Interpreter.lean b/Support/Dynamics/Interpreter.lean index 521c26019..3f04c02f6 100644 --- a/Support/Dynamics/Interpreter.lean +++ b/Support/Dynamics/Interpreter.lean @@ -1,6 +1,7 @@ import Paper.S1_Syntax.Definitions import Support.Syntax.Terms import Support.Dynamics.Host +import Support.Dynamics.Arrays /-! # Support — Dynamics — Interpreter @@ -30,6 +31,10 @@ def Mem.get? (μ : Mem) (ℓ : Loc) : Option Val := def Mem.alloc (μ : Mem) (v : Val) : Loc × Mem := (μ.next, ⟨(μ.next, v) :: μ.cells, μ.next + 1⟩) +/-- `n` cells holding `v` at `next, …, next + n − 1`: the block `tab` places. `[spec §9]` -/ +def Mem.allocN (μ : Mem) (n : Nat) (v : Val) : Loc × Mem := + (μ.next, ⟨(List.range n).map (fun i => (μ.next + i, v)) ++ μ.cells, μ.next + n⟩) + def Mem.free (μ : Mem) (ℓ : Loc) : Mem := { μ with cells := μ.cells.filter (·.1 ≠ ℓ) } @@ -61,7 +66,8 @@ A deterministic executable machine where the print gives a relation, with the `K application, so `delta` returns it as a value rather than stepping to one. Every value whose head is a primitive appears on the left, so `step` needs no arity test: the grammar already says how many arguments each production - still wants. A host operation is applied by `H.call` (`[spec §8]`). -/ + still wants. A host operation is applied by `H.call` (`[spec §8]`); the array steps + allocate a block at `next` (`[spec §9]`). -/ def delta (H : Host) (μ : Mem) : Expr → Val → Step | .prim .alloc, v => let (ℓ, μ') := μ.alloc v; .next μ' (.loc ℓ) | .prim .free, ⟨.loc ℓ, _⟩ => @@ -81,6 +87,13 @@ def delta (H : Host) (μ : Mem) : Expr → Val → Step match H.call op v with | some r => .next μ r.val | none => .stuck .badHostArg + | .prim .tab, ⟨.pair (.prim (.lit (.i32 c))) f, h⟩ => + let (ℓ, μ') := μ.allocN (arrLen c).toNat .unit + .next μ' (tabFill ⟨f, h.pair_right⟩ ℓ (arrLen c) 0 (arrLen c).toNat) + | .prim .idx, ⟨.pair (.pair (.loc ℓ) (.prim (.lit (.i32 n)))) (.prim (.lit (.i32 i))), _⟩ => + .next μ (idxAns ℓ n i).val + | .prim .drop, ⟨.pair f (.pair (.loc ℓ) (.prim (.lit (.i32 n)))), h⟩ => + .next μ (dropFill ⟨f, h.pair_left⟩ ℓ 0 n.toNat) | _, _ => .stuck .badPrimArg /-- One step of call-by-value reduction. diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 851a734f1..89c789b40 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -12,6 +12,7 @@ - [Recursive data and folds](guide/recursive-data.md) - [Codata: iterators, streams, delay](guide/codata.md) - [Integers](guide/integers.md) +- [Arrays](guide/arrays.md) - [Implicit destructors](guide/implicit-destructors.md) - [Planned features](guide/planned/README.md) - [Pipelines and `for`](guide/planned/pipelines.md) diff --git a/book/src/guide/arrays.md b/book/src/guide/arrays.md new file mode 100644 index 000000000..d4b64c481 --- /dev/null +++ b/book/src/guide/arrays.md @@ -0,0 +1,201 @@ +# Arrays + +An array is a run of places whose number is known only when the program runs. `array A` owns +`n` cells, each holding an `A`; its value is the pair of a base location and the length `n`, and +element `i` is the cell at base + `i`. It is the run-time counterpart of a struct's block +([Structs as blocks](blocks-and-families.md)): a block has a fixed shape, an array has a length. + +`array A` is linear, like `ref A`: it owns memory, so it is used once and freed once. + +## Making and freeing + +`array_new[A]` takes a length and a *tabulator*, a `!`-function from the index to the element, +and calls it once per index, in order. A negative length makes the empty array. + +`array_size[A]` returns the array with its length, and `array_replace[A]` puts a new element at +an index and returns the old one. An index out of range is not an error that stops the program: +the result is `inr`, carrying the new element back. + +```bc +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return ((unbang host[mul32]) (i, i32[10])) + +def replace : A * (i32 * i32) => A * (i32 + i32) = unbang array_replace[i32] + +main : F (i32 * ((i32 + i32) * (i32 + i32))) = + a <- return ((unbang array_new[i32]) (i32[4], tab)); + q <- return ((unbang array_size[i32]) a); + let (a1, n) = q; + q1 <- return (replace (a1, (i32[2], i32[42]))); + let (a2, old) = q1; + q2 <- return (replace (a2, (i32[9], i32[5]))); + let (a3, oob) = q2; + return (n, (old, oob)) +-- answer: (i32[4], (inl i32[20], inr i32[5])) +``` + +The array `a3` is not used again, so the checker frees it at the end of `main` +([Implicit destructors](implicit-destructors.md)). `array A` has a destructor whenever `A` has +one: every element goes to `A`'s destructor, and then the cells are freed. The explicit form is +`array_free[A]`, which takes the consumer to call on each element: + +```bc +type R = ref i32 + +def mk : !(i32 => R) = !thunkv fun (i : i32) -> return (alloc[i32] i) + +def consume : !(R => 1) = !thunkv fun (c : R) -> x <- return (free[i32] c); return () + +main : F i32 = + a <- return ((unbang array_new[R]) (i32[3], mk)); + u <- return ((unbang array_free[R]) (consume, a)); + let () = u; + return i32[3] +-- answer: i32[3] +``` + +`live 0` after the value is the check that all three cells and the array itself were freed. + +## Borrowing + +An array is a value, so it is borrowed the way any value is: in a cell. `withbor1` on a +`ref (array A)` lends `imm 'x (array A)`, a borrow of the cell that holds the array. + +To reach the elements, `withload` reads through that borrow. Its callback receives +`view 'y (array A)`, which is `array_imm 'y A`: the *family* of views of the array's elements, +at a lifetime `'y` inside `'x`. On a family: + +| constant | type | +|---|---| +| `array_len['y, A]` | `!(array_imm 'y A => i32)` | +| `array_get['y, A]` | `!(array_imm 'y A * i32 => imm 'y A + 1)` | +| `array_copy['y, A]` | `!(array_imm 'y A => array_imm 'y A * array_imm 'y A)` | +| `array_forget['y, A]` | `!(array_imm 'y A => 1)` | + +`array_get` gives the view of one element at the family's own lifetime, or `inr ()` past the +end. The view is read with `withload` like any `imm` borrow. + +```bc +type A = array i32 +type T = i32 * (i32 + 1) + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return ((unbang host[add32]) (i, i32[100])) + +def read : all 'z < top. imm 'z i32 => i32 = + thunkv Fun 'z < top. return (thunkv fun (v : imm 'z i32) -> + return (withload['z, 'w, i32, i32] v + (thunkv Fun 'w < dom. return (thunkv fun (x : i32) -> return (boxed['w] x))))) + +main : F T = + a <- return ((unbang array_new[i32]) (i32[3], tab)); + r <- return (alloc[A] a); + q <- return (withbor1['x, A, T] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + t <- return (withload['x, 'y, A, T] b + (thunkv Fun 'y < dom. return (thunkv fun (f : array_imm 'y i32) -> + c <- return ((unbang array_copy['y, i32]) f); + let (f1, f2) = c; + n <- return ((unbang array_len['y, i32]) f1); + g <- return ((unbang array_get['y, i32]) (f2, i32[1])); + x : i32 + 1 <- case g { + inl v -> return (inl (read ['y] v)) + | inr u -> return (inr u) + }; + return (boxed['y] (n, x))))); + return (boxed['x] t)))); + let (r2, t) = q; + return t +-- answer: (i32[3], inl i32[101]) +``` + +The cell `r2` and the array in it are dropped implicitly when `main` ends. + +To change an array through a borrow, borrow the cell `mut` with `withbor2`, and let `withswap` +lend the array itself to a function that returns it: + +```bc +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +def replace : A * (i32 * i32) => A * (i32 + i32) = unbang array_replace[i32] + +main : F ((i32 + i32) * (i32 + i32)) = + r <- return (alloc[A] ((unbang array_new[i32]) (i32[2], tab))); + q <- return (withbor2['x, top, A, i32 + i32] r + (thunkv Fun 'x < top. return (thunkv fun (m : mut 'x A) -> + s <- return (withswap['x, A, i32 + i32] m + (thunkv fun (a : A) -> return (replace (a, (i32[1], i32[7]))))); + let (m2, o) = s; + return (boxed['x] o)))); + let (r2, first) = q; + q2 <- return (replace (free[A] r2, (i32[1], i32[0]))); + let (a, now) = q2; + return (first, now) +-- answer: (inl i32[1], inl i32[7]) +``` + +## What the checker rejects + +A view of an element lives no longer than its family. Here the callback of `withload` tries to +return the view of element 0 at the outer lifetime `'x`: + +```bc,reject +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +main : F 1 = + a <- return ((unbang array_new[i32]) (i32[2], tab)); + r <- return (alloc[A] a); + q <- return (withbor1['x, A, 1] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + v <- return (withload['x, 'y, A, imm 'x i32] b + (thunkv Fun 'y < dom. return (thunkv fun (f : array_imm 'y i32) -> + g <- return ((unbang array_get['y, i32]) (f, i32[0])); + case g { + inl e -> return (boxed['y] e) + | inr u -> absurd u + }))); + return (boxed['x] ())))); + let (r2, u) = q; + return u +-- expect: 14:52: type mismatch: expected `imm 'x i32`, but this has type `imm 'y i32` +``` + +And an array cannot be changed while it is borrowed: the cell is lent to the first borrow, so a +second borrow of it, `mut`, uses it twice. + +```bc,reject +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +main : F 1 = + r <- return (alloc[A] ((unbang array_new[i32]) (i32[2], tab))); + q <- return (withbor1['x, A, 1] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + q2 <- return (withbor2['z, top, A, 1] r + (thunkv Fun 'z < dom. return (thunkv fun (m : mut 'z A) -> + return (boxed['z] ())))); + return (boxed['x] ())))); + let (r2, u) = q; + return u +-- expect: 7:16: `r` is used twice here, and its type `ref array i32` is not persistent +``` + +## Why an element's view can live as long as the family + +A borrow of a cell cannot hand out a view of something the cell *owns* at the borrow's own +lifetime: that is a view through a pointer, and the model excludes it +([decision 5](../decisions.md)). That is why `withload` gives the family at a shorter lifetime +`'y`. The family itself is not a pointer: it is the views of the elements, side by side, and +nothing in it owns anything. `array_get` takes one of those views out and forgets the rest, so a +view of an element at `'y` is sound (`SPEC.md` §9, `Core/Arrays.lean`). + +## Not yet + +Splitting an array at a run-time index, iterating over one, a growable `Vec`, and strings are +planned. An array cannot yet be borrowed without a cell, and there is no `mut` view of a single +element. diff --git a/book/src/introduction.md b/book/src/introduction.md index 90e78e80e..4d95ad75e 100644 --- a/book/src/introduction.md +++ b/book/src/introduction.md @@ -30,11 +30,13 @@ one typing rule. A more pleasant surface syntax that desugars to it is being des - **Codata.** One former, `nu X. C`, with coiteration. Loops that may not terminate (`delay`), streams and finite iterators are abbreviations over it, and each has a mandatory `close`. - **Integers.** `i32` and `i64` with WebAssembly's operations. +- **Arrays.** `array A`, a run of `n` places with `n` known at run time, with bounds-checked + access and views of single elements. - **Implicit destructors.** An owned value that a program leaves unused is freed or closed by a destructor that the checker inserts. -Buffers and strings (`SPEC.md` §9) and externs with standard input and output (§11) are -specified but not implemented. +Externs with standard input and output (`SPEC.md` §11) are specified but not implemented; a +growable `Vec` and strings are planned as library code over arrays. ## What is proved, and what is trusted diff --git a/book/src/proved.md b/book/src/proved.md index 0bf7b34ba..f48401679 100644 --- a/book/src/proved.md +++ b/book/src/proved.md @@ -32,6 +32,14 @@ inserts ([Implicit destructors](guide/implicit-destructors.md)) are part of the **Arithmetic is a parameter.** The machine performs integer operations through a *host*, and every theorem above holds for every host. Nothing about arithmetic is assumed. +**Arrays are covered by the same two theorems.** Each array constant is a typing rule with its +compatibility lemma in +[`Core/Arrays.lean`](https://github.com/empath-nirvana/borrow-core/blob/main/Core/Arrays.lean): +`array_new` places the cells at fresh consecutive locations and fills them, `array_free` frees +them one by one, and `array_get` hands out one element's view. The family of an array's element +views comes from `withload`, whose rule was already sound; what it needs of the new type is that +every lemma about the model proved by induction on types has its array case, and each has. + **The baseline is preserved.** The paper's calculus (BoCa) and its logic (BoLo) embed into the core, and the paper's Fundamental Property and adequacy hold as before. [`Paper/INDEX.md`](https://github.com/empath-nirvana/borrow-core/blob/main/Paper/INDEX.md) indexes each result of the paper to its Lean declaration, @@ -54,5 +62,4 @@ text. - A `delay` loop is not claimed to terminate; its type says it may not. - The checker is not complete: a well-typed program may be rejected (`Frontend/README.md`, *Incompleteness*). -- Buffers, strings and externs are specified (`SPEC.md` §9, §11) but not implemented, so no - theorem covers them yet. +- Externs are specified (`SPEC.md` §11) but not implemented, so no theorem covers them yet. diff --git a/book/src/reference/constants.md b/book/src/reference/constants.md index adfdd071f..59f5c9556 100644 --- a/book/src/reference/constants.md +++ b/book/src/reference/constants.md @@ -8,6 +8,10 @@ {{#include ../../../docs/syntax-core.md:integers}} +## Arrays + +{{#include ../../../docs/syntax-core.md:arrays}} + ## Derived forms {{#include ../../../docs/syntax-core.md:derived}} diff --git a/corpus/array-empty.bc b/corpus/array-empty.bc new file mode 100644 index 000000000..8f7cc51f9 --- /dev/null +++ b/corpus/array-empty.bc @@ -0,0 +1,18 @@ +-- A negative length (SPEC §9): `array_new` at i32[0xFFFFFFFF], which is -1 as a signed +-- integer, makes the empty array rather than 4294967295 cells. `array_size` reads the length, +-- and index 0 is out of range. The tabulator is never called. +-- +-- Answer: (i32[0], inr i32[5]). A length read as unsigned would try to allocate 2^32 - 1 cells; +-- a bounds check that lets index 0 through gives `inl`. + +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +main : F (i32 * (i32 + i32)) = + a <- return ((unbang array_new[i32]) (i32[0xFFFFFFFF], tab)); + q <- return ((unbang array_size[i32]) a); + let (a1, n) = q; + q2 <- return ((unbang array_replace[i32]) (a1, (i32[0], i32[5]))); + let (a2, o) = q2; + return (n, o) diff --git a/corpus/array-get.bc b/corpus/array-get.bc new file mode 100644 index 000000000..0fe6e1185 --- /dev/null +++ b/corpus/array-get.bc @@ -0,0 +1,49 @@ +-- Borrowing an array (SPEC §9). The array is a value, so it is borrowed in a cell: `withbor1` +-- lends `imm 'x (array i32)`, and `withload` turns that borrow into the family of leaf views +-- `array_imm 'y i32` at a shorter lifetime. The family is copied; `array_len` reads its +-- length; `array_get` takes the view of one leaf at the family's lifetime, which `withload` +-- reads; and `array_get` past the end takes the error branch. The cell and the array are +-- dropped implicitly (SPEC §12): the cell is freed, then the array, cell by cell. +-- +-- Answer: (i32[5], (i32[30], inr ())): the length, the element at index 3 of +-- [0, 10, 20, 30, 40], and the error branch at index 7. A get at the wrong index gives 20 or +-- 40; a bounds check that is signed or off by one would not give `inr ()` at 7, and 5 elements +-- would print as 4 or 6. + +type A = array i32 +type T = i32 * (i32 * (1 + 1)) + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return ((unbang host[mul32]) (i, i32[10])) + +-- The leaf behind a view. +def read : all 'z < top. imm 'z i32 => i32 = + thunkv Fun 'z < top. return (thunkv fun (v : imm 'z i32) -> + return (withload['z, 'w, i32, i32] v + (thunkv Fun 'w < dom. return (thunkv fun (x : i32) -> return (boxed['w] x))))) + +main : F T = + a <- return ((unbang array_new[i32]) (i32[5], tab)); + r <- return (alloc[A] a); + q <- return (withbor1['x, A, T] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + t <- return (withload['x, 'y, A, T] b + (thunkv Fun 'y < dom. return (thunkv fun (f : array_imm 'y i32) -> + c <- return ((unbang array_copy['y, i32]) f); + let (f1, f2) = c; + c2 <- return ((unbang array_copy['y, i32]) f2); + let (f3, f4) = c2; + n <- return ((unbang array_len['y, i32]) f1); + g <- return ((unbang array_get['y, i32]) (f3, i32[3])); + x : i32 <- case g { + inl v -> return (read ['y] v) + | inr u -> return i32[9999] + }; + h <- return ((unbang array_get['y, i32]) (f4, i32[7])); + o : 1 + 1 <- case h { + inl v -> return (inl ()) + | inr u -> return (inr ()) + }; + return (boxed['y] (n, (x, o)))))); + return (boxed['x] t)))); + let (r2, t) = q; + return t diff --git a/corpus/array-refs.bc b/corpus/array-refs.bc new file mode 100644 index 000000000..1831e2dd0 --- /dev/null +++ b/corpus/array-refs.bc @@ -0,0 +1,24 @@ +-- An array of owned cells (SPEC §9, §12): `array_new` allocates a cell per element, holding +-- 7i + 1. Element 1 is replaced by a fresh cell and the old cell is freed, which returns its +-- contents. The array, still owning three cells, is dropped implicitly at the end of `main`: +-- its destructor is `array_free` with the destructor of `ref i32` as the consumer, which frees +-- each element's cell and then the array. +-- +-- Answer: i32[8], the contents of the cell that was element 1 of [1, 8, 15]. A tabulator +-- applied at the wrong index gives 1 or 15. `live 0` checks that the destructor freed the four +-- cells the array owned at the end and the array itself. + +type R = ref i32 + +def mk : !(i32 => R) = + !thunkv fun (i : i32) -> + return (alloc[i32] ((unbang host[add32]) ((unbang host[mul32]) (i, i32[7]), i32[1]))) + +main : F i32 = + a <- return ((unbang array_new[R]) (i32[3], mk)); + q <- return ((unbang array_replace[R]) (a, (i32[1], alloc[i32] i32[100]))); + let (a1, o) = q; + case o { + inl c -> return (free[i32] c) + | inr c -> return (free[i32] c) + } diff --git a/corpus/array-replace.bc b/corpus/array-replace.bc new file mode 100644 index 000000000..a38318ff1 --- /dev/null +++ b/corpus/array-replace.bc @@ -0,0 +1,38 @@ +-- `array_replace` (SPEC §9) on an owned array, then through a `mut` borrow. Index 2 of +-- [0, 10, 20, 30] is replaced by 42 and then read back by replacing it again; index 4 is out of +-- range, so the new element comes back on the error branch. The array is then put in a cell +-- and lent `mut` by `withbor2`: `withswap` takes the array out of the cell, `array_replace` +-- sets index 0 to 99, and the array goes back. Freeing the cell returns the array, whose index +-- 0 is read back. +-- +-- Answer: (inl i32[20], (inl i32[42], (inr i32[5], (inl i32[0], inl i32[99])))). An index off +-- by one gives 10 or 30 in the first component; a replace that does not store gives 20 back in +-- the second and 0 in the last; a bounds check that accepts 4 gives `inl`. + +type A = array i32 +type O = i32 + i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return ((unbang host[mul32]) (i, i32[10])) + +def replace : A * (i32 * i32) => A * O = unbang array_replace[i32] + +main : F (O * (O * (O * (O * O)))) = + a <- return ((unbang array_new[i32]) (i32[4], tab)); + q1 <- return (replace (a, (i32[2], i32[42]))); + let (a1, old) = q1; + q2 <- return (replace (a1, (i32[2], i32[7]))); + let (a2, back) = q2; + q3 <- return (replace (a2, (i32[4], i32[5]))); + let (a3, oob) = q3; + r <- return (alloc[A] a3); + q4 <- return (withbor2['x, top, A, O] r + (thunkv Fun 'x < top. return (thunkv fun (m : mut 'x A) -> + s <- return (withswap['x, A, O] m + (thunkv fun (arr : A) -> return (replace (arr, (i32[0], i32[99]))))); + let (m2, o) = s; + return (boxed['x] o)))); + let (r2, first) = q4; + a4 <- return (free[A] r2); + q5 <- return (replace (a4, (i32[0], i32[1]))); + let (a5, now) = q5; + return (old, (back, (oob, (first, now)))) diff --git a/corpus/array-sum.bc b/corpus/array-sum.bc new file mode 100644 index 000000000..08238bb9a --- /dev/null +++ b/corpus/array-sum.bc @@ -0,0 +1,43 @@ +-- An array of 1000 elements built by `array_new` (SPEC §9) from the tabulator i -> i + 1, and +-- summed by a loop of 1024 steps: `twice` composes a step with itself, ten times over. Each +-- step reads the element at the index by `array_replace` (writing 0, then the element back) +-- and adds it; past the end `array_replace` takes the error branch and the step does nothing. +-- The array is freed implicitly at the end of `main` (SPEC §12): its destructor frees every +-- cell. +-- +-- Answer: (i32[500500], i32[1000]): 1 + 2 + … + 1000, and the number of elements read. An +-- element skipped or read twice changes the sum; a tabulator applied to the wrong index +-- (i instead of i + 1) gives 499500; a length off by one gives 501501 or 499500 with 1001 or +-- 999 reads. + +type S = array i32 * (i32 * i32) + +def plus : i32 * i32 => i32 = unbang host[add32] + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return (plus (i, i32[1])) + +-- One step at (a, (i, acc)): read a[i], and continue at (i + 1, acc + a[i]). +def step : !(S => S) = + !thunkv fun (st : S) -> + let (a, r) = st; + let (i, acc) = r; + q <- return ((unbang array_replace[i32]) (a, (i, i32[0]))); + let (a1, o) = q; + case o { + inl x -> + q2 <- return ((unbang array_replace[i32]) (a1, (i, x))); + let (a2, o2) = q2; + return (a2, (plus (i, i32[1]), plus (acc, x))) + | inr z -> return (a1, (i, acc)) + } + +def twice : !(S => S) => !(S => S) = + thunkv fun (f : !(S => S)) -> return (!thunkv fun (s : S) -> return ((unbang f) ((unbang f) s))) + +main : F (i32 * i32) = + a <- return ((unbang array_new[i32]) (i32[1000], tab)); + f <- return (twice (twice (twice (twice (twice (twice (twice (twice (twice (twice step)))))))))); + s <- return ((unbang f) (a, (i32[0], i32[0]))); + let (a2, r) = s; + let (i, acc) = r; + return (acc, i) diff --git a/corpus/reject/array-mut-while-viewed.bc b/corpus/reject/array-mut-while-viewed.bc new file mode 100644 index 000000000..d3ef9596a --- /dev/null +++ b/corpus/reject/array-mut-while-viewed.bc @@ -0,0 +1,18 @@ +-- Mutating an array while a view of it is held: inside the `imm` borrow of the cell that holds +-- the array, the cell is borrowed again, `mut`, to replace an element. +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +main : F 1 = + a <- return ((unbang array_new[i32]) (i32[2], tab)); + r <- return (alloc[A] a); + q <- return (withbor1['x, A, 1] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + q2 <- return (withbor2['z, top, A, 1] r + (thunkv Fun 'z < dom. return (thunkv fun (m : mut 'z A) -> + return (boxed['z] ())))); + return (boxed['x] ())))); + let (r2, u) = q; + return u +-- expect: 10:16: `r` is used twice here, and its type `ref array i32` is not persistent diff --git a/corpus/reject/array-use-after-free.bc b/corpus/reject/array-use-after-free.bc new file mode 100644 index 000000000..fca53edde --- /dev/null +++ b/corpus/reject/array-use-after-free.bc @@ -0,0 +1,14 @@ +-- An array used after it is freed: `array_free` consumes the array, which is then replaced +-- into. +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +def gone : !(i32 => 1) = !thunkv fun (x : i32) -> return () + +main : F (A * (i32 + i32)) = + a <- return ((unbang array_new[i32]) (i32[2], tab)); + u <- return ((unbang array_free[i32]) (gone, a)); + let () = u; + return ((unbang array_replace[i32]) (a, (i32[0], i32[1]))) +-- expect: 11:3: `a` is used twice here, and its type `array i32` is not persistent diff --git a/corpus/reject/array-view-escape.bc b/corpus/reject/array-view-escape.bc new file mode 100644 index 000000000..566491c4b --- /dev/null +++ b/corpus/reject/array-view-escape.bc @@ -0,0 +1,22 @@ +-- A leaf view escaping its family's lifetime: the callback of `withload` returns the view of +-- element 0, to be read after the family's lifetime `'y` has ended. +type A = array i32 + +def tab : !(i32 => i32) = !thunkv fun (i : i32) -> return i + +main : F 1 = + a <- return ((unbang array_new[i32]) (i32[2], tab)); + r <- return (alloc[A] a); + q <- return (withbor1['x, A, 1] r + (thunkv Fun 'x < top. return (thunkv fun (b : imm 'x A) -> + v <- return (withload['x, 'y, A, imm 'x i32] b + (thunkv Fun 'y < dom. return (thunkv fun (f : array_imm 'y i32) -> + g <- return ((unbang array_get['y, i32]) (f, i32[0])); + case g { + inl e -> return (boxed['y] e) + | inr u -> absurd u + }))); + return (boxed['x] ())))); + let (r2, u) = q; + return u +-- expect: 16:52: type mismatch: expected `imm 'x i32`, but this has type `imm 'y i32` diff --git a/docs/decisions.md b/docs/decisions.md index 8873add71..ae17b393d 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -99,3 +99,14 @@ Each entry: the decision, and why. Newest last. relation would not be monotone. A `ν` outside that class holds everywhere, like `⊤`, and has no eliminator. Delay, stream and finite iterators are abbreviations; iterator adapters are library code over `Iter`, never formers or rules (rule 20). +29. **Arrays are primitive: the run-time-sized family of places.** Places come in a square: + statically sized values (records), run-time-sized values (`μ`), statically sized families of + places (blocks, §5), and run-time-sized families of places, which only a primitive gives. + `[L]` (SPEC §9) is the type-level `⊛_{i A | all 'x < @. A (built-in abbreviations) | block σ | val σ | fam mut @ σ | fam imm @ σ | view @ A + | array A | array_imm @ A (VTy.fam, SPEC §9) comp C ::= F A | A -o C | C & C | top | forall 'x < @. C | nu X. C | delay A | stream A | iter A A A (built-in abbreviations) @@ -102,8 +103,8 @@ lexically, innermost first, and are then discarded. Two consequences: 4. `+` associates to the right. 5. `*` associates to the right. 6. Prefix formers take one atom: `U`, `F`, `ref`, `!`, `imm @`, `mut @`, `box @`, `delay`, - `stream`, `block`, `val`, `fam m @`, `view @`; `iter` takes three. Their lifetime argument is - also an atom. + `stream`, `block`, `val`, `fam m @`, `view @`, `array`, `array_imm @`; `iter` takes three. + Their lifetime argument is also an atom. For example, `S -o F (A + S) & F 1` is `S -o ((F (A + S)) & (F 1))`, and `imm 'a Nat * Nat` is `(imm 'a Nat) * Nat`. `F A * B` is a sort error: write `F (A * B)`. Sorts are checked after @@ -119,6 +120,8 @@ parsing. | `val σ` | `σ.valTy`: `A` at a leaf, `*` at a node | `Bl:57` | | `fam m @ σ` | `σ.famTy M @`: `m @ A` at each leaf, `*` at a node | `BB:111` | | `view @b A` | `Imm̲ @b A` | `VTy.immReborrow`, `Ty:1064` | +| `array A` | `[ref A]`: the owned array, the family of its cells | `VTy.fam`, SPEC §9 | +| `array_imm @a A` | `[imm @a A]`: the family of an array's leaf views | `VTy.fam`, SPEC §9 | | `delay A` | `nu X. F (A + U X) & F 1` | `CTy.delay`, `Ty:1042` | | `stream A` | `nu X. F (A * U X) & F 1` | `CTy.stream`, `Ty:1045` | | `iter A R R'` | `nu X. F (A * U X + R) & F R'` | `CTy.iter`, `Ty:1049` | @@ -399,6 +402,35 @@ Integer literals and operations are constants (`SPEC.md` §8, `Core/Integers.lea - `i32`/`i64` are persistent (section 7), so integer variables are reused and dropped freely. +## 8b. Arrays + + +The array constants (`SPEC.md` §9, `Core/Arrays.lean`), each one `CDerives` constructor at a +dead context, typed at `!(A => B)` like a host operation, so a call is `(unbang c) V`: + +| constant | type | rule | +|---|---|---| +| `array_new[A]` | `!(i32 * !(i32 => A) => array A)` | `arrNew` | +| `array_free[A]` | `!(!(A => 1) * array A => 1)` | `arrFree` | +| `array_size[A]` | `!(array A => array A * i32)` | `arrSize` | +| `array_replace[A]` | `!(array A * (i32 * A) => array A * (A + A))` | `arrReplace` | +| `array_len[@a, A]` | `!(array_imm @a A => i32)` | `arrLen` | +| `array_get[@a, A]` | `!(array_imm @a A * i32 => imm @a A + 1)` | `arrGet` | +| `array_copy[@a, A]` | `!(array_imm @a A => array_imm @a A * array_imm @a A)` | `arrCopy` | +| `array_forget[@a, A]` | `!(array_imm @a A => 1)` | `arrForget` | + +- A negative length makes the empty array; an index is compared unsigned, so a negative index + is out of range and takes `inr`. +- `array_replace` returns the old element, or on the error branch the new one back. +- An array is borrowed in a cell: `withbor1`/`withbor2` on a `ref (array A)` lend + `imm 'x (array A)`/`mut 'x (array A)`. `withload` at the `imm` borrow gives the family + `view 'y (array A) = array_imm 'y A`; `withswap` at the `mut` borrow lends the array itself. +- `array A` has a destructor when `A` has one (`array_free` with `A`'s destructor), and + `array_imm @a A` is forgettable (`array_forget`), so both are dropped implicitly (SPEC §12). +- The erasures: `array_new`, `array_get`, `array_free` are the primitives `tab`, `idx`, `drop`; + the others are closed λ-terms (`arrLenV`, `sizeV`, `replaceV`, `copyV`, `forgetV`). + + ## 9. Derived forms @@ -712,8 +744,9 @@ How this checks: - **Numerals and list literals** (`3`, `[1, 2, 3]`) are sugar and are out for now. The library builds them with Lean functions (`num`, `buildList`). -- **Buffers and strings** (`SPEC.md` §9) are specified but not implemented. Integers are in - (section 8a), and the erasure target stays Paper `Expr`, which gained `Prim.lit`/`Prim.host`. +- **Growable buffers and strings** are library code over arrays (`SPEC.md` §9), not yet written. + Integers (section 8a) and arrays (section 8b) are in, and the erasure target stays Paper + `Expr`, which gained `Prim.lit`/`Prim.host` and the array primitives `tab`, `idx`, `drop`. - **Lifetime-parametric `def`s and `extern`s** are not supported. - **Block provenance** (`SPEC.md` §5 Open) is not enforced: `crossPack` (`Core/BlockPrograms.lean`) typechecks. diff --git a/experiments/FrameTheorems/FrameTheorems/EveryRun.lean b/experiments/FrameTheorems/FrameTheorems/EveryRun.lean index 1bfbe3314..525ec9cbf 100644 --- a/experiments/FrameTheorems/FrameTheorems/EveryRun.lean +++ b/experiments/FrameTheorems/FrameTheorems/EveryRun.lean @@ -5,10 +5,11 @@ import Core.Steps # FrameTheorems — from the exhibited run to every run that does not allocate `wpTS` exhibits one run. The machine is deterministic except at `alloc↦`, which may pick any -location the heap misses. So a run that takes no `alloc↦` step is the only run to a value from -its start (`stepsNA_det`): every other run to a value takes the same steps. The footprint of -the exhibited run is therefore the footprint of every allocation-free run -(`wpTS_footprint_every`). +location the heap misses, and at the array step `tab`, which may place its cells at any run of +such locations. So a run that takes no `alloc↦` step and no array step (`ArrRedex`; `idx` and +`drop` are excluded too, for `TwoRun`'s renaming) is the only run to a value from its start +(`stepsNA_det`): every other run to a value takes the same steps. The footprint of the +exhibited run is therefore the footprint of every such run (`wpTS_footprint_every`). A run that allocates is related to the exhibited one only through a renaming of the locations it allocates. Runs to values need not agree even on the initial heap's domain @@ -32,25 +33,45 @@ open BoCa.BoLo (Heap Steps Step1 Head Kont) /-- The redex `alloc v`. -/ def allocRedex (v : Val) : Expr := .app (.val (.prim .alloc)) (.val v) -/-- Two head steps from one configuration agree, unless the redex is an allocation. -/ +/-- The redex of an array step, `tab v`, `idx v` or `drop v`. `tab` picks a run of fresh +consecutive locations, so it is not a function of the configuration, like `alloc`; `idx` and +`drop` compute locations from a base, so a renaming of `Loc` does not carry them (`TwoRun`). -/ +def ArrRedex (a : Expr) : Prop := + ∃ (p : Prim) (v : Val), (p = .tab ∨ p = .idx ∨ p = .drop) ∧ a = .app (.val (.prim p)) (.val v) + +theorem arrRedex_tab (v : Val) : ArrRedex (.app (.val (.prim .tab)) (.val v)) := + ⟨.tab, v, .inl rfl, rfl⟩ + +theorem arrRedex_idx (v : Val) : ArrRedex (.app (.val (.prim .idx)) (.val v)) := + ⟨.idx, v, .inr (.inl rfl), rfl⟩ + +theorem arrRedex_drop (v : Val) : ArrRedex (.app (.val (.prim .drop)) (.val v)) := + ⟨.drop, v, .inr (.inr rfl), rfl⟩ + +/-- Two head steps from one configuration agree, unless the redex is an allocation or an +array step. -/ theorem head_det_gen {μ μ' μ₁ μ₂ : Heap} {a b a₁ a₂ : Expr} (h₁ : Head μ a μ₁ a₁) (h₂ : Head μ' b μ₂ a₂) (hab : a = b) (hμ : μ = μ') - (hna : ∀ v : Val, a ≠ allocRedex v) : μ₂ = μ₁ ∧ a₂ = a₁ := by + (hna : ∀ v : Val, a ≠ allocRedex v) (hnr : ¬ ArrRedex a) : μ₂ = μ₁ ∧ a₂ = a₁ := by subst hμ cases h₁ with | alloc v _ _ => exact absurd rfl (hna v) + | tab => exact absurd (arrRedex_tab _) hnr + | idx => exact absurd (arrRedex_idx _) hnr + | drop => exact absurd (arrRedex_drop _) hnr | _ => cases h₂ <;> simp_all [allocRedex, Expr.val, Subtype.val_inj] theorem head_det {μ μ₁ μ₂ : Heap} {a a₁ a₂ : Expr} (h₁ : Head μ a μ₁ a₁) - (h₂ : Head μ a μ₂ a₂) (hna : ∀ v : Val, a ≠ allocRedex v) : μ₂ = μ₁ ∧ a₂ = a₁ := - head_det_gen h₁ h₂ rfl rfl hna + (h₂ : Head μ a μ₂ a₂) (hna : ∀ v : Val, a ≠ allocRedex v) (hnr : ¬ ArrRedex a) : + μ₂ = μ₁ ∧ a₂ = a₁ := + head_det_gen h₁ h₂ rfl rfl hna hnr -/-- A step whose redex is not an allocation. -/ +/-- A step whose redex is neither an allocation nor an array step. -/ def Step1NA (μ : Heap) (e : Expr) (μ' : Heap) (e' : Expr) : Prop := ∃ (K : Kont) (a a' : Expr), e = K.plug a ∧ e' = K.plug a' ∧ Head μ a μ' a' ∧ - ∀ v : Val, a ≠ allocRedex v + (∀ v : Val, a ≠ allocRedex v) ∧ ¬ ArrRedex a -/-- A run that takes no `alloc↦` step. -/ +/-- A run that takes no `alloc↦` step and no array step. -/ inductive StepsNA : Heap → Expr → Heap → Expr → Prop where | refl (μ : Heap) (e : Expr) : StepsNA μ e μ e | more {μ μ₁ μ' : Heap} {e e₁ e' : Expr} @@ -70,11 +91,11 @@ theorem StepsNA.toSteps {μ μ' : Heap} {e e' : Expr} (h : StepsNA μ e μ' e') /-- A step that is not an allocation is the only step. -/ theorem step1_det {μ μ₁ μ₂ : Heap} {e e₁ e₂ : Expr} (h₁ : Step1NA μ e μ₁ e₁) (h₂ : Step1 μ e μ₂ e₂) : μ₂ = μ₁ ∧ e₂ = e₁ := by - obtain ⟨K, a, a', rfl, rfl, hh₁, hna⟩ := h₁ + obtain ⟨K, a, a', rfl, rfl, hh₁, hna, hnr⟩ := h₁ obtain ⟨K', b, b', hE, rfl, hh₂⟩ := h₂ obtain ⟨K'', rfl, rfl⟩ := plug_decomp hh₂ K K' hh₁.not_isVal hE obtain rfl : K'' = .hole := head_plug hh₁ K'' hh₂.not_isVal rfl - obtain ⟨rfl, rfl⟩ := head_det hh₁ hh₂ hna + obtain ⟨rfl, rfl⟩ := head_det hh₁ hh₂ hna hnr exact ⟨rfl, by rw [Kont.plug_comp]; rfl⟩ /-- **A run that does not allocate is the only run to a value.** Every run to a value from diff --git a/experiments/FrameTheorems/README.md b/experiments/FrameTheorems/README.md index 3f285ae2c..e43813e9d 100644 --- a/experiments/FrameTheorems/README.md +++ b/experiments/FrameTheorems/README.md @@ -39,11 +39,14 @@ cell. The proof needs no new model fact. `flat_factor` (the flattening of `X ● `⦇X⦈` and `⦇Y⦈` cell by cell, and every location of it is one of theirs) is `ResU.Flat.split_factors` read through the erasure. -**Every run that does not allocate** (`wpTS_footprint_every`). The machine's only -nondeterministic step is `alloc↦`. `step1_det` shows that any other step is the only step -(unique decomposition, `Core/Steps.lean`, and `head_det`). So a run that takes no `alloc↦` step -is the only run to a value from its start (`stepsNA_det`), and it has the exhibited run's -footprint. +**Every run that does not allocate** (`wpTS_footprint_every`). The machine's nondeterministic +steps are `alloc↦` and the array step `tab`, which places a run of fresh cells (SPEC §9). +`step1_det` shows that any step other than these and the array steps `idx`, `drop` is the only +step (unique decomposition, `Core/Steps.lean`, and `head_det`). So a run that takes no `alloc↦` +step and no array step (`ArrRedex`) is the only run to a value from its start (`stepsNA_det`), +and it has the exhibited run's footprint. Here and below, "does not allocate" means such a run. +`idx` and `drop` are deterministic; they are excluded as well so that `TwoRun`, whose renaming +results do not carry address arithmetic, can use `StepsNA` unchanged. **Callbacks** (`call_frame`). For `g` at `∀'x ⊏ @b. F('x) ⊸ ['x] T` in the relation at `ρg` and a borrow `v` at `F('x)` with `'x ↦ α ⊏ @b` at `ρv`, the relation's own clauses give diff --git a/experiments/Grades/Grades/Judgment.lean b/experiments/Grades/Grades/Judgment.lean index 80535393f..2384869b3 100644 --- a/experiments/Grades/Grades/Judgment.lean +++ b/experiments/Grades/Grades/Judgment.lean @@ -126,6 +126,14 @@ inductive GDerives (P : Profile) : LifeCtx → GCtx → Expr → Jdg → Prop wh GDerives P Δ Γ (.val ⟨.prim (.lit c), .prim⟩) (.val (.int c.width)) | hostOp {Δ Γ} (op : HostOp) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val (.prim (.host op))) (.val (.bang (hostSig op))) + | arrNew {Δ Γ} (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val (.prim .tab)) (.val (arrNewTy A)) + | arrFree {Δ Γ} (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val (.prim .drop)) (.val (arrFreeTy A)) + | arrSize {Δ Γ} (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val sizeV) (.val (arrSizeTy A)) + | arrReplace {Δ Γ} (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val replaceV) (.val (arrReplaceTy A)) + | arrLen {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val arrLenV) (.val (arrLenTy a A)) + | arrGet {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val (.prim .idx)) (.val (arrGetTy a A)) + | arrCopy {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val copyV) (.val (arrCopyTy a A)) + | arrForget {Δ Γ} (a : Lifetime.Life) (A : VTy) (hΓ : GDead P Γ) : GDerives P Δ Γ (.val forgetV) (.val (arrForgetTy a A)) | withbor1 {Δ Γ A B} (x : LifeVar) (hΓ : GDead P Γ) (hx : Δ.find? x = none) (hsc₁ : VScoped Δ A) (hfree₁ : ¬ VFree x A) (hfree₂ : ¬ VFree x B) : GDerives P Δ Γ withbor (.val (axWithbor1Ty x Δ.meetOfDom A B)) @@ -230,6 +238,14 @@ theorem GDerives.toCore {Δ : LifeCtx} {Γ : GCtx} {e : Expr} {j : Jdg} | forgetUnk hΓ => exact .forgetUnk hΓ.toDeadP | lit c hΓ => exact .lit c hΓ.toDeadP | hostOp op hΓ => exact .hostOp op hΓ.toDeadP + | arrNew A hΓ => exact .arrNew A hΓ.toDeadP + | arrFree A hΓ => exact .arrFree A hΓ.toDeadP + | arrSize A hΓ => exact .arrSize A hΓ.toDeadP + | arrReplace A hΓ => exact .arrReplace A hΓ.toDeadP + | arrLen a A hΓ => exact .arrLen a A hΓ.toDeadP + | arrGet a A hΓ => exact .arrGet a A hΓ.toDeadP + | arrCopy a A hΓ => exact .arrCopy a A hΓ.toDeadP + | arrForget a A hΓ => exact .arrForget a A hΓ.toDeadP | withbor1 x hΓ hx hsc hf₁ hf₂ => exact .withbor1 x hΓ.toDeadP hx hsc hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact .withbor2 x hΓ.toDeadP hx hside hf₁ hf₂ | withbor3 x hΓ hx hf₁ hf₂ => exact .withbor3 x hΓ.toDeadP hx hf₁ hf₂ @@ -301,6 +317,14 @@ theorem CDerives.toGraded {Δ : LifeCtx} {Γ : Ctx VTy} {e : Expr} {j : Jdg} | forgetUnk hΓ => exact .forgetUnk (DeadP.toGDead hΓ) | lit c hΓ => exact .lit c (DeadP.toGDead hΓ) | hostOp op hΓ => exact .hostOp op (DeadP.toGDead hΓ) + | arrNew A hΓ => exact .arrNew A (DeadP.toGDead hΓ) + | arrFree A hΓ => exact .arrFree A (DeadP.toGDead hΓ) + | arrSize A hΓ => exact .arrSize A (DeadP.toGDead hΓ) + | arrReplace A hΓ => exact .arrReplace A (DeadP.toGDead hΓ) + | arrLen a A hΓ => exact .arrLen a A (DeadP.toGDead hΓ) + | arrGet a A hΓ => exact .arrGet a A (DeadP.toGDead hΓ) + | arrCopy a A hΓ => exact .arrCopy a A (DeadP.toGDead hΓ) + | arrForget a A hΓ => exact .arrForget a A (DeadP.toGDead hΓ) | withbor1 x hΓ hx hsc hf₁ hf₂ => exact .withbor1 x (DeadP.toGDead hΓ) hx hsc hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact .withbor2 x (DeadP.toGDead hΓ) hx hside hf₁ hf₂ | withbor3 x hΓ hx hf₁ hf₂ => exact .withbor3 x (DeadP.toGDead hΓ) hx hf₁ hf₂ @@ -428,6 +452,14 @@ theorem GDerives.mono (hPQ : P.Le Q) {Δ : LifeCtx} {Γ : GCtx} {e : Expr} {j : | forgetUnk hΓ => exact .forgetUnk (hPQ.dead hΓ) | lit c hΓ => exact .lit c (hPQ.dead hΓ) | hostOp op hΓ => exact .hostOp op (hPQ.dead hΓ) + | arrNew A hΓ => exact .arrNew A (hPQ.dead hΓ) + | arrFree A hΓ => exact .arrFree A (hPQ.dead hΓ) + | arrSize A hΓ => exact .arrSize A (hPQ.dead hΓ) + | arrReplace A hΓ => exact .arrReplace A (hPQ.dead hΓ) + | arrLen a A hΓ => exact .arrLen a A (hPQ.dead hΓ) + | arrGet a A hΓ => exact .arrGet a A (hPQ.dead hΓ) + | arrCopy a A hΓ => exact .arrCopy a A (hPQ.dead hΓ) + | arrForget a A hΓ => exact .arrForget a A (hPQ.dead hΓ) | withbor1 x hΓ hx hsc hf₁ hf₂ => exact .withbor1 x (hPQ.dead hΓ) hx hsc hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact .withbor2 x (hPQ.dead hΓ) hx hside hf₁ hf₂ | withbor3 x hΓ hx hf₁ hf₂ => exact .withbor3 x (hPQ.dead hΓ) hx hf₁ hf₂ diff --git a/experiments/Grades/Grades/Soundness.lean b/experiments/Grades/Grades/Soundness.lean index 16ac1f6b8..6a1cdb0c0 100644 --- a/experiments/Grades/Grades/Soundness.lean +++ b/experiments/Grades/Grades/Soundness.lean @@ -379,6 +379,14 @@ theorem fundamentalG {P : Profile} (hP : ∀ T, P.drop T = true → SemForget ( | forgetUnk hΓ => exact viaDead hP hΓ fun _ h => forgetUnk_compatX h | lit c hΓ => exact viaDead hP hΓ fun _ h => lit_compatX c h | hostOp op hΓ => exact viaDead hP hΓ fun _ h => hostOp_compatX op h + | arrNew A hΓ => exact viaDead hP hΓ fun _ h => arrNew_compatX h + | arrFree A hΓ => exact viaDead hP hΓ fun _ h => arrFree_compatX h + | arrSize A hΓ => exact viaDead hP hΓ fun _ h => arrSize_compatX h + | arrReplace A hΓ => exact viaDead hP hΓ fun _ h => arrReplace_compatX h + | arrLen a A hΓ => exact viaDead hP hΓ fun _ h => arrLen_compatX h + | arrGet a A hΓ => exact viaDead hP hΓ fun _ h => arrGet_compatX h + | arrCopy a A hΓ => exact viaDead hP hΓ fun _ h => arrCopy_compatX h + | arrForget a A hΓ => exact viaDead hP hΓ fun _ h => arrForget_compatX h | withbor1 x hΓ hx hsc hf₁ hf₂ => exact viaDead hP hΓ fun _ h => withbor1_compatX x h hx hsc hf₁ hf₂ | withbor2 x hΓ hx hside hf₁ hf₂ => exact viaDead hP hΓ fun _ h => withbor2_compatX x h hx hside hf₁ hf₂ diff --git a/experiments/README.md b/experiments/README.md index 9eb5beb38..d4b6f5fe8 100644 --- a/experiments/README.md +++ b/experiments/README.md @@ -21,8 +21,8 @@ library. None of these libraries is a default target. | `Recursion/` | `Recursion` | `lake build Recursion` | first probe of `docs/scope-recursion.md` §6, over `CBPVWorld` unchanged. P-μ: the boxed list `μX. 1 ⊕ Ref X` as an impredicative least fixed point over `vC`'s clauses; unroll, 6.60's shape, `Ext`-stability, a Z-combinator `freeAll : U(L ⊸ F 1)`, and a 3-cell list built and freed to empty memory (`build3Free_runs`). P-ν: `νX. F(A ⊗ U X) & F 1` as a direct greatest fixed point over `cC`'s clauses; `out`/`roll`, coiteration sound by the `∃S` candidate (`unfold_sem`), take-2-then-`close` runs to empty memory (`take2Close_runs`). P-Imm̲: the substituting clause's unrolling law for every `T` (`immReborrow_unroll`); the naive clause differs at the boxed list (`naive_ne`). No `μ`/`ν` in `VTy`/`CTy`; R4 untouched | | `CBPVMu/` | `CBPVMu` | `lake build CBPVMu` | R4: `μ` (iso-recursive, de Bruijn) in a fork of CBPVWorld's `VTy`. Relation and shapes with a type-variable environment, `μ` as the impredicative least fixed point; unroll equations and induction principles at every type (`vC_mu_iff`, `vC_ind`); `Imm̲` at `μ` by payload substitution; 6.60, `Ext`-stability, 6.131 (`reborrow_vShape`), `relabel_obs` at `μ`. The choosers, world invariants, `Wp`, `FrameRules` and `Compatibility` (`withload_compatX`) build unchanged. FP and adequacy with `roll`/`unroll`. Programs: a list built and freed (Z-combinator `freeAll : U(L ⊸ F 1)`); a borrowed count of a list through nested `withload`s at fresh lifetimes, recursion on a fuel, runs to `3` with empty memory. Fuel-free borrowed fold (`CountFree.lean`): the measure is the loaded witness's cell count, carried across `withload` by `withload_sized` (the view is `reb_β` of the witness, `typed_image_pos`); `countFree : !U(∀'a. Imm 'a L ⊸ F N)`, runs to `3` with empty memory. In-place map through `Mut 'x L′` (`MapMut.lean`, `L′ = μX. 1 ⊕ Ref (N ⊗ X)`): whole-list `withswap`, then `mapInc : U(L′ ⊸ F L′)` swaps each owned cell, fuel-free; runs to `[1, 1, 1]` with empty memory. Folds as typing rules (`FoldImm.lean`, `FoldOwn.lean`): `foldImm : !U(∀'b. A_R ⊸ F R) ⊸ !U(∀'a. Imm 'a (μX. T) ⊸ F R)` and `foldOwn : !U(A′_R ⊸ F R) ⊸ !U(μX. T ⊸ F R)` for `T` built by `⊗`/`⊕` from closed leaves, `Ref X` and guarded `X`, covered by `fundamentalC`; the count, `freeAll`, a search in a list and in a tree, and a numeral copy are derived with no hand proof and run with empty memory (`Folds.lean`). The common list `μX. 1 ⊕ Ref (N ⊗ X)` through LeafPlaces' block view (`Blocks.lean`, `CommonList.lean`): a block's handle is its leaf family, so `Ref (N ⊗ X)` is `Ref N ⊗ Ref X` by definition and the list is in `FShape`. There is no pack rule, and `allocS` makes each node one block (`wpTS_allocS`, `consC_block`). A borrowed sum (`foldImm`, adding through `natCopy` and a `foldOwn` add), `mapInc`, `toPure` and free (`foldOwn`) are derived with no hand proof. `sumProg` returns `6` and `mapProgC` returns `[1, 1, 1]` inside a `Mut` borrow, each with no cell left. `foldOwn`'s map rebuilds the list in fresh blocks. The owned fold with reuse (`FoldReuse.lean`): `foldReuse : !U(A″_R ⊸ F R) ⊸ !U(μX. T ⊸ F R)` hands the algebra, at each `Ref X`, the emptied cell as an owned `Ref 1` with the recursive result, and the algebra refills it by the type-changing `swap`. No new type former, and `fundamentalC` covers it. The in-place map over the common list is derived with no hand proof (`ReuseList.lean`, `mapIncRE`). `MapMut`'s program with it returns `[1, 1, 1]` with no cell left, and the interpreter shows the nodes at the same addresses before and after (`mapIncR_inPlace`; `foldOwn`'s map moves them). An `N → B` map also reuses the cells (`isZeroR_inPlace`). `MapMut`'s `mapInc` over `L′` (outside `FShape`) stays hand-proved. `X` under `U`, and `Ref` over `⊕`: not done. The delay type `D A = νX. F(A ⊕ U X) & F 1` as `CTy.delay`, the greatest fixed point (`cC_delay_iff`); every existing result builds with the case. Guarded recursion `fixD : !U(S ⊸ F(A ⊕ S) & F 1) ⊸ !U(S ⊸ D A)` as a `CDerives` rule, sound by coinduction with the candidate `{fixD step s}` (`fixD_compatX`), covered by `fundamentalC`; `▷` is the step's type (it returns a state, not a `D A`). `f = λd. later (g d)` with `g` typed as any term is refuted: `skip` is well typed and `later (skip N)`'s second `next` diverges (`naive_not_sem`). `⋂ₙ Dₙ = ⟦D A⟧` reduced to `wpTS` commuting with decreasing intersections (`approxLim_le_delayRel`); not derived. The driver (observe `n` times, then `close`) is a derivation at `F(A ⊕ 1)`, adequacy for `D 1`/`D N`; a count-down driven to `3`, a never-finishing loop and a counter in a `Ref` cell, each closed, run with empty memory | | `Optics/` | `Optics` | `lake build Optics` | borrows as optics (#27). One interface `Optic` (continuation form: `S ⊸ (∀'x ⊏ ⊓Δ. F('x) ⊸ ['x] T) ⊸ S ⊗ T`, and the runs), instantiated by `withbor` (forms 1–3), a place-record leaf, `withborF2`, and a variant payload; generic `lift` (through `withswap`) and `comp` (nesting). Every instance is a closed `CDerives` derivation (`fundamentalC` unchanged) and every law is a statement about machine runs: Rezip (the source comes back), `lift_cell` (the lender's cell is unchanged), composition of foci, `over_id`, `over_fuse`, the `Imm` getter's identity put. Decisive test: a variant payload borrowed at a fresh lifetime with the tag frozen on every run, and IterMut over a dynamic list with its shape law on every run, both derived with no new rule. Not derived: the all-at-once borrow of a dynamic list at one lifetime (L2), stated as `AllAtOnceGoal` | -| `FrameTheorems/` | `FrameTheorems` | `lake build FrameTheorems` | frame guarantees as theorems of the relation (#37). `wpTS_footprint`: on the run a `wpTS` exhibits from a tagged typed world `ρf ● ρ`, every location of `⦇ρf⦈` and every existing location `⦇ρ⦈` holds only immutably keeps its value, and `ρ`'s `mut` cells stay allocated. The same holds on every run that takes no `alloc↦` step (`stepsNA_det`: such a run is the only run to a value). Instances: semantically typed callbacks (`call_frame`); `Imm` callbacks keep the lent cell; closed well-typed `Mut` callbacks at a persistent payload change only the lent cell (`ElemFrame`'s heap clauses at typed worlds); one frame law for every optic (`optic_frame`), `over` and IterMut; distinct source and focus cells (`over_fuse_typed`); `PureFn`'s heap clause. Not derived: the footprint on runs that allocate (`EveryRunFootprint`, stated), the laws at heaps outside typed worlds (so `iterMut_shape` keeps `ElemFrame`), and two-run properties (`PureFn`'s value clause, fusion at effectful callbacks) | -| `TwoRun/` | `TwoRun` | `lake build TwoRun` | two-run properties (#39). Runs are invariant under permutations of `Loc` (`steps_rename`); fresh runs (every `alloc` picks a location the configuration does not name) are deterministic up to a permutation (`freshRun_det`), and every run to a value is a fresh run's image along a function injective on its heap (`fresh_run_image`). So the exhibited run's `Footprint` holds on every fresh run of every `wpTS` term, up to that renaming (`wpTS_footprint_fresh`). At terms outside the calculus: `EveryRunFootprint` fails at a fresh run (`not_everyRunFootprint`), and the renamed form at a stale run (`not_everyRunFootprintRen`). The read footprint (a run touches only its resource's locations and new ones) does not follow from `wpTS` (`not_readFootprint`): loads leave no trace in the post-world and allocation is angelic. For typed programs it is stated (`ReadFootprintTyped`) and not derived, so the two-run laws were not attempted. README scopes a unary strengthening (a touch set in `wpTS`) and, failing it, a binary relation on plain data, `Ref`, `Mut`, `withbor` | +| `FrameTheorems/` | `FrameTheorems` | `lake build FrameTheorems` | frame guarantees as theorems of the relation (#37). `wpTS_footprint`: on the run a `wpTS` exhibits from a tagged typed world `ρf ● ρ`, every location of `⦇ρf⦈` and every existing location `⦇ρ⦈` holds only immutably keeps its value, and `ρ`'s `mut` cells stay allocated. The same holds on every run that takes no `alloc↦` step and no array step (`stepsNA_det`: such a run is the only run to a value). Instances: semantically typed callbacks (`call_frame`); `Imm` callbacks keep the lent cell; closed well-typed `Mut` callbacks at a persistent payload change only the lent cell (`ElemFrame`'s heap clauses at typed worlds); one frame law for every optic (`optic_frame`), `over` and IterMut; distinct source and focus cells (`over_fuse_typed`); `PureFn`'s heap clause. Not derived: the footprint on runs that allocate (`EveryRunFootprint`, stated), the laws at heaps outside typed worlds (so `iterMut_shape` keeps `ElemFrame`), and two-run properties (`PureFn`'s value clause, fusion at effectful callbacks) | +| `TwoRun/` | `TwoRun` | `lake build TwoRun` | two-run properties (#39). Runs with no array step are invariant under permutations of `Loc` (`steps_rename`); fresh runs (no array step, and every `alloc` picks a location the configuration does not name) are deterministic up to a permutation (`freshRun_det`), and every run to a value is a fresh run's image along a function injective on its heap (`fresh_run_image`). So the exhibited run's `Footprint` holds on every fresh run of every `wpTS` term, up to that renaming (`wpTS_footprint_fresh`). At terms outside the calculus: `EveryRunFootprint` fails at a fresh run (`not_everyRunFootprint`), and the renamed form at a stale run (`not_everyRunFootprintRen`). The read footprint (a run touches only its resource's locations and new ones) does not follow from `wpTS` (`not_readFootprint`): loads leave no trace in the post-world and allocation is angelic. For typed programs it is stated (`ReadFootprintTyped`) and not derived, so the two-run laws were not attempted. README scopes a unary strengthening (a touch set in `wpTS`) and, failing it, a binary relation on plain data, `Ref`, `Mut`, `withbor` | | `Grades/` | `Grades` | `lake build Grades` | interval grades `[lo,hi]` over `{0,1,∞}` (#23). Five grades, laws by `decide`; endpoint type conditions (droppable ⇒ forgettable, copyable ⇒ persistent). `GDerives` (`CDerives` with graded premises) is equivalent to `CDerives` under the Core profile (`cderives_iff_graded`, and `splitP_iff`/`deadP_iff`/`soloP_iff`/`persistent_iff` at the premises); the letter differs at promotion, where `Core`'s `!Γ` needs `!`-types and the one-principle reading needs only persistence (sound: `bangThunkF_compatG`; `promote_int` over an `I32`). `fundamentalG` reuses every `Core` compatibility lemma plus one drop lemma (`semC_weak`), so `forget` at `Imm`/`Mut` becomes derivable; `copy_bang`, `forget_bang`, `forget_unk` are derivable in `Core` already. Measurement: grades as a context coordinate are **not net-negative** (the relation reads only `erase`; at defaults grades are a bijection with liveness bits); the deletions (≈ −67 lines) come from the type conditions and need no grades. Erasure: grade `0` is the dead slot, with `lam0`/`app0` as its erased positions; a capability token at grade `0` through a borrow-using function runs, and runs the same with any value in its place (`prog_runs_any`). No ω-poison: promotion is the only scaling rule, scaling a non-persistent slot is ill graded, folds/`fixD`/`unfoldS` keep data at `[1,1]` | | `Affine/` | `Affine` | `lake build Affine` | affine owned values by elaboration (#43). Destructors as derived terms: `dtorE T : T ⊸ F 1` is a closed `CDerives` derivation for every droppable type (`dtor_wf`): `forget` at persistent and borrowed types, `free` then drop at `Ref`, componentwise at `⊗`/`⊕`/`[@a]`, `foldOwn` at `μ` in the fold class, `close` at `U(C & F 1)`/`D A`/`Stream A`; `U C` in general has none. `ADerives` (`CDerives` plus a `drop` rule, indexed by the elaborated term) elaborates into `CDerives` (`ADerives.elab`), so soundness and leak freedom are `fundamentalC`/`adequacyC` at the elaborated term: no model change, no invariant, no inversion. Borrows: a lent owner is not in the callback's context (`owner_not_shared`); a borrow's destructor is `forget`. Programs: the i32 list sum with both frees removed (`123`) and a block's unused field and unused block dropped per branch (`7`, `0`), each run by adequacy and the interpreter with no cell left, and the sources leak without the drops. Policy: end of scope on each path; surface default affine for owned types with a destructor | | `Core/` | — | — | promoted: the library target `Core` (`Core/`), `SPEC.md` §3–§7. It was assembled from `CBPVMu`, `CBPVWorld`, `Relaxed`, `Records`, `Places`, `LeafPlaces` and `Recursion` | diff --git a/experiments/TwoRun/README.md b/experiments/TwoRun/README.md index f3efdbfaf..5fff2ae17 100644 --- a/experiments/TwoRun/README.md +++ b/experiments/TwoRun/README.md @@ -12,7 +12,7 @@ Every result's axioms are within `[propext, Classical.choice, Quot.sound]`. | file | contents | |---|---| -| `Rename.lean` | `Expr.rename`, `Heap.rename`; renaming commutes with shifting, substitution and plugging; the host step is location-free (`host_call_rename`); runs are invariant under permutations of `Loc` (`head_rename`, `step1_rename`, `steps_rename`) | +| `Rename.lean` | `Expr.rename`, `Heap.rename`; renaming commutes with shifting, substitution and plugging; the host step is location-free (`host_call_rename`); runs with no array step are invariant under permutations of `Loc` (`head_rename`, `step1_rename`, `steps_rename`) | | `Fresh.lean` | `Named`, fresh steps and runs (`Step1F`, `FreshRun`); a run that does not allocate is fresh (`StepsNA.toFresh`); fresh runs are deterministic up to a permutation (`step1F_sim`, `freshRun_det`) | | `Sim.lean` | every run follows every fresh run along a function (`HeapRel`, `head_sim`, `step_sim`, `run_sim`, `fresh_run_image`); the footprint of every fresh run (`wpTS_footprint_fresh`, `everyFreshRunFootprint`) | | `Refute.lean` | `EveryRunFootprint` does not hold, at a fresh run of `alloc (free 0)` (`not_everyRunFootprint`); its renamed form does not hold, at a stale run of `staleE` (`not_everyRunFootprintRen`) | @@ -21,13 +21,18 @@ Every result's axioms are within `[propext, Classical.choice, Quot.sound]`. ## Step 1: renaming, and the footprint on every run **Renaming** (`steps_rename`). For a permutation `σ` of `Loc`, a run from `(μ, e)` to -`(μ′, e′)` renames to a run from `(σ μ, σ e)` to `(σ μ′, σ e′)`. The machine treats locations -opaquely: no step compares two locations or computes one. The host step's arguments and results -are integer literals and booleans, which renaming fixes. +`(μ′, e′)` that takes no array step renames to a run from `(σ μ, σ e)` to `(σ μ′, σ e′)`. Away +from the array steps the machine treats locations opaquely: no step compares two locations or +computes one. The host step's arguments and results are integer literals and booleans, which +renaming fixes. The array steps (SPEC §9) compute locations: `tab` places consecutive cells, +`idx` and `drop` add an index to a base, and a permutation does not preserve `ℓ + i`. Every +result in this library is therefore stated for runs with no array step (`StepsR`, and `Step1F` +requires it). Whether the results extend to runs with array steps, under a renaming of blocks +rather than of single locations, I have not derived and have not checked. **Fresh runs.** A configuration *names* a location when the heap holds it, the term mentions -it, or a value in the heap mentions it. A *fresh* run allocates only locations its -configuration does not name. Every run that does not allocate is fresh (`StepsNA.toFresh`), and +it, or a value in the heap mentions it. A *fresh* run takes no array step and allocates only +locations its configuration does not name. Every run that does not allocate is fresh (`StepsNA.toFresh`), and so is every run of a bump allocator that starts above every location the initial configuration names. diff --git a/experiments/TwoRun/TwoRun/Fresh.lean b/experiments/TwoRun/TwoRun/Fresh.lean index 2dd3b7cbb..c28a09fe8 100644 --- a/experiments/TwoRun/TwoRun/Fresh.lean +++ b/experiments/TwoRun/TwoRun/Fresh.lean @@ -28,7 +28,9 @@ variable {ℍ : BoCa.Host} [BoCa.HostIs ℍ] namespace BoCa.Fig16.LogRel.Core.TwoRun open BoCa open BoCa.BoLo (Heap Steps Step1 Head Kont) -open BoCa.Fig16.LogRel.Core.FrameTheorems (allocRedex StepsNA Step1NA head_det) +open BoCa.Fig16.LogRel.Core.FrameTheorems (allocRedex StepsNA Step1NA head_det ArrRedex + arrRedex_tab) +open BoCa.BoLo (Step1R not_arrRedex_rename) /-! ### Naming and freshness -/ @@ -38,7 +40,7 @@ def Named (μ : Heap) (e : Expr) (ℓ : Loc) : Prop := /-- A step that allocates only locations its configuration does not name. -/ def Step1F (μ : Heap) (e : Expr) (μ' : Heap) (e' : Expr) : Prop := - Step1 μ e μ' e' ∧ ∀ ℓ, μ' ℓ ≠ none → μ ℓ = none → ¬ Named μ e ℓ + Step1R μ e μ' e' ∧ ∀ ℓ, μ' ℓ ≠ none → μ ℓ = none → ¬ Named μ e ℓ /-- A run whose every allocation is of a location its configuration does not name. -/ inductive FreshRun : Heap → Expr → Heap → Expr → Prop where @@ -50,13 +52,15 @@ theorem FreshRun.toSteps {μ μ' : Heap} {e e' : Expr} (h : FreshRun μ e μ' e' Steps μ e μ' e' := by induction h with | refl => exact .refl _ _ - | more h _ ih => exact .more h.1 ih + | more h _ ih => exact .more h.1.toStep1 ih -/-- A head step that is not an allocation adds no location. -/ +/-- A head step that is neither an allocation nor an array step adds no location. -/ theorem head_dom_of_not_alloc {μ μ' : Heap} {a a' : Expr} (h : Head μ a μ' a') - (hna : ∀ v : Val, a ≠ allocRedex v) {ℓ : Loc} (h' : μ' ℓ ≠ none) : μ ℓ ≠ none := by + (hna : ∀ v : Val, a ≠ allocRedex v) (hnr : ¬ ArrRedex a) {ℓ : Loc} (h' : μ' ℓ ≠ none) : + μ ℓ ≠ none := by cases h with | alloc v k _ => exact absurd rfl (hna v) + | tab => exact absurd (arrRedex_tab _) hnr | free k v _ => intro h0; apply h' by_cases e : ℓ = k @@ -75,9 +79,9 @@ theorem StepsNA.toFresh {μ μ' : Heap} {e e' : Expr} (h : StepsNA μ e μ' e') induction h with | refl => exact .refl _ _ | more h _ ih => - obtain ⟨K, a, a', he, he', hh, hna⟩ := h - exact .more ⟨⟨K, a, a', he, he', hh⟩, fun ℓ h₁ h₂ => absurd h₂ - (head_dom_of_not_alloc hh hna h₁)⟩ ih + obtain ⟨K, a, a', he, he', hh, hna, hnr⟩ := h + exact .more ⟨⟨K, a, a', he, he', hh, hnr⟩, fun ℓ h₁ h₂ => absurd h₂ + (head_dom_of_not_alloc hh hna hnr h₁)⟩ ih /-! ### Renaming and naming -/ @@ -183,9 +187,9 @@ renaming `(σ μ, σ e)` end in configurations related by a permutation. -/ theorem step1F_sim {σ : Equiv.Perm Loc} {μ μ' μB : Heap} {e e' eB : Expr} (hA : Step1F μ e μ' e') (hB : Step1F (μ.rename σ) (e.rename σ) μB eB) : ∃ σ' : Equiv.Perm Loc, μB = μ'.rename σ' ∧ eB = e'.rename σ' := by - obtain ⟨⟨K, a, a', rfl, rfl, hh⟩, hfA⟩ := hA - obtain ⟨⟨K', b, b', hE, rfl, hhB⟩, hfB⟩ := hB - have hh' := BoLo.head_rename σ hh + obtain ⟨⟨K, a, a', rfl, rfl, hh, hnr⟩, hfA⟩ := hA + obtain ⟨⟨K', b, b', hE, rfl, hhB, -⟩, hfB⟩ := hB + have hh' := BoLo.head_rename σ hh hnr rw [BoLo.Kont.rename_plug] at hE obtain ⟨K'', hb, rfl⟩ := Fig16.LogRel.Core.plug_decomp hhB (K.rename σ) K' hh'.not_isVal hE obtain rfl : K'' = .hole := Fig16.LogRel.Core.head_plug hh' K'' hhB.not_isVal hb @@ -218,7 +222,7 @@ theorem step1F_sim {σ : Equiv.Perm Loc} {μ μ' μB : Heap} {e e' eB : Expr} Kont.rename_eq_self _ _ hfix] simp only [Expr.rename, Expr.val, Val.loc, τ, Equiv.swap_apply_left] · push Not at hal - obtain ⟨rfl, rfl⟩ := head_det hh' hhB (not_allocRedex_rename σ hal) + obtain ⟨rfl, rfl⟩ := head_det hh' hhB (not_allocRedex_rename σ hal) (not_arrRedex_rename σ hnr) exact ⟨σ, rfl, (BoLo.Kont.rename_plug σ K a').symm⟩ /-! ### Fresh runs -/ @@ -237,11 +241,11 @@ theorem freshRun_sim {μ μ' : Heap} {e e' : Expr} (hA : FreshRun μ e μ' e') : intro hv σ μB eB hB _ cases hB with | refl => exact ⟨σ, rfl, rfl⟩ - | more h _ => exact absurd h.1 (not_step1_of_isVal (hv.rename σ)) + | more h _ => exact absurd h.1.toStep1 (not_step1_of_isVal (hv.rename σ)) | more hs _ ih => intro hv σ μB eB hB hvB cases hB with - | refl => exact absurd hs.1 (not_step1_of_isVal (isVal_of_rename σ hvB)) + | refl => exact absurd hs.1.toStep1 (not_step1_of_isVal (isVal_of_rename σ hvB)) | more h t => obtain ⟨σ₁, rfl, rfl⟩ := step1F_sim hs h exact ih hv t hvB diff --git a/experiments/TwoRun/TwoRun/Read.lean b/experiments/TwoRun/TwoRun/Read.lean index eceeef63a..396bb91b4 100644 --- a/experiments/TwoRun/TwoRun/Read.lean +++ b/experiments/TwoRun/TwoRun/Read.lean @@ -195,6 +195,7 @@ theorem not_readFootprint : ¬ ReadFootprint := by (Head.beta (fun _ => none) _ (.loc l)) t₁ v.2 obtain ⟨rfl, rfl⟩ := head_det (Head.beta _ _ (.loc l)) hh₂ (by intro w e; simp [allocRedex, Expr.val, Val.lam] at e) + (by rintro ⟨p, w, -, e⟩; simp [Expr.val, Val.lam, Val.prim] at e) -- the load of the frame's cell obtain ⟨-, -, -, ht, -⟩ := runIn_head_inv (K := .appR (.val (.lam (.val (.loc l)))) .hole) (a := loadE (.val (.loc 0))) diff --git a/experiments/TwoRun/TwoRun/Refute.lean b/experiments/TwoRun/TwoRun/Refute.lean index 3c4edc3cb..cf1de3245 100644 --- a/experiments/TwoRun/TwoRun/Refute.lean +++ b/experiments/TwoRun/TwoRun/Refute.lean @@ -114,9 +114,10 @@ theorem reallocE_wp (α : Life) (ls : List SRec) : theorem reallocE_fresh1 : FreshRun heap0 reallocE (BoLo.Heap.upd (BoLo.Heap.del heap0 0) 1 .unit) (.val (.loc 1)) := by refine .more ⟨⟨.appR (.val (.prim .alloc)) .hole, _, _, rfl, rfl, - Head.free heap0 0 .unit (BoLo.Heap.upd_same _ _ _)⟩, fun ℓ h₁ h₂ => ?_⟩ (.more - ⟨Step1.head (Head.alloc _ .unit 1 (by simp [BoLo.Heap.del])), - fun ℓ h₁ h₂ => ?_⟩ (.refl _ _)) + Head.free heap0 0 .unit (BoLo.Heap.upd_same _ _ _), + BoLo.not_arrRedex_prim (by decide) _⟩, fun ℓ h₁ h₂ => ?_⟩ (.more + ⟨⟨.hole, _, _, rfl, rfl, Head.alloc _ .unit 1 (by simp [BoLo.Heap.del]), + BoLo.not_arrRedex_prim (by decide) _⟩, fun ℓ h₁ h₂ => ?_⟩ (.refl _ _)) · exact absurd h₂ (by by_cases hk : ℓ = 0 · subst hk; simp at h₁ diff --git a/experiments/TwoRun/TwoRun/Rename.lean b/experiments/TwoRun/TwoRun/Rename.lean index 73f730d5b..e9eb56d8a 100644 --- a/experiments/TwoRun/TwoRun/Rename.lean +++ b/experiments/TwoRun/TwoRun/Rename.lean @@ -8,9 +8,13 @@ import FrameTheorems every step and every run is carried to a head step, step and run of the renamed configuration (`head_rename`, `step1_rename`, `steps_rename`). -The machine names locations only through `alloc`, `free`, `load` and `store`, and treats them -opaquely: no step compares two locations or computes one. The host step's arguments and results -are integer literals and booleans, and renaming fixes them (`host_call_rename`). +Away from the array steps, the machine names locations only through `alloc`, `free`, `load` and +`store`, and treats them opaquely: no step compares two locations or computes one. The host +step's arguments and results are integer literals and booleans, and renaming fixes them +(`host_call_rename`). The array steps compute locations: `tab` places a run of consecutive +cells, and `idx` and `drop` add an index to a base. A permutation of `Loc` does not preserve +`ℓ + i`, so the results here are stated for steps whose redex is not an array step +(`FrameTheorems.ArrRedex`), and runs of such steps (`StepsR`). -/ noncomputable section @@ -323,10 +327,42 @@ theorem Heap.rename_one (μ : Heap) : μ.rename 1 = μ := by /-! ### Runs are invariant under renaming -/ -/-- **A head step, renamed, is a head step.** -/ -theorem head_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {a a' : Expr} (h : Head μ a μ' a') : +open BoCa.Fig16.LogRel.Core.FrameTheorems (ArrRedex arrRedex_tab arrRedex_idx arrRedex_drop) + +/-- A redex `p v` at a primitive that is not an array step is not an array redex. -/ +theorem not_arrRedex_prim {p : Prim} (hp : p ≠ .tab ∧ p ≠ .idx ∧ p ≠ .drop) (v : Val) : + ¬ ArrRedex (.app (.val (.prim p)) (.val v)) := by + rintro ⟨q, w, hq, h⟩ + simp only [Expr.val, Val.prim, Expr.app.injEq, Expr.prim.injEq] at h + rcases hq with rfl | rfl | rfl + · exact hp.1 h.1 + · exact hp.2.1 h.1 + · exact hp.2.2 h.1 + +/-- Renaming makes no term an array redex. -/ +theorem not_arrRedex_rename (σ : Loc → Loc) {a : Expr} (hnr : ¬ ArrRedex a) : + ¬ ArrRedex (a.rename σ) := by + rintro ⟨p, v, hp, h⟩ + cases a with + | app f x => + simp only [Expr.rename, Expr.app.injEq] at h + obtain ⟨h₁, h₂⟩ := h + cases f with + | prim q => + simp only [Expr.rename, Expr.val, Val.prim, Expr.prim.injEq] at h₁ + have hx : IsVal x := isVal_of_rename σ (h₂ ▸ v.2) + exact hnr ⟨p, ⟨x, hx⟩, hp, by rw [h₁]; rfl⟩ + | _ => simp [Expr.rename, Expr.val, Val.prim] at h₁ + | _ => simp [Expr.rename] at h + +/-- **A head step, renamed, is a head step**, when its redex is not an array step. -/ +theorem head_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {a a' : Expr} (h : Head μ a μ' a') + (hnr : ¬ ArrRedex a) : Head (μ.rename σ) (a.rename σ) (μ'.rename σ) (a'.rename σ) := by cases h with + | tab => exact absurd (arrRedex_tab _) hnr + | idx => exact absurd (arrRedex_idx _) hnr + | drop => exact absurd (arrRedex_drop _) hnr | beta b v => rw [Expr.rename_subst] exact .beta _ (b.rename σ) (v.rename σ) @@ -357,16 +393,31 @@ theorem head_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {a a' : Expr} (h : Hea rw [hr] exact .host _ op (v.rename σ) r (h₁.trans h) -theorem step1_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {e e' : Expr} (h : Step1 μ e μ' e') : - Step1 (μ.rename σ) (e.rename σ) (μ'.rename σ) (e'.rename σ) := by - obtain ⟨K, a, a', rfl, rfl, hh⟩ := h +/-- A step whose redex is not an array step. -/ +def Step1R (μ : Heap) (e : Expr) (μ' : Heap) (e' : Expr) : Prop := + ∃ (K : Kont) (a a' : Expr), e = K.plug a ∧ e' = K.plug a' ∧ Head μ a μ' a' ∧ ¬ ArrRedex a + +theorem Step1R.toStep1 {μ μ' : Heap} {e e' : Expr} (h : Step1R μ e μ' e') : Step1 μ e μ' e' := by + obtain ⟨K, a, a', h₁, h₂, h₃, -⟩ := h + exact ⟨K, a, a', h₁, h₂, h₃⟩ + +/-- A run that takes no array step. -/ +inductive StepsR : Heap → Expr → Heap → Expr → Prop where + | refl (μ : Heap) (e : Expr) : StepsR μ e μ e + | more {μ μ₁ μ' : Heap} {e e₁ e' : Expr} + (h : Step1R μ e μ₁ e₁) (t : StepsR μ₁ e₁ μ' e') : StepsR μ e μ' e' + +theorem step1_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {e e' : Expr} (h : Step1R μ e μ' e') : + Step1R (μ.rename σ) (e.rename σ) (μ'.rename σ) (e'.rename σ) := by + obtain ⟨K, a, a', rfl, rfl, hh, hnr⟩ := h exact ⟨K.rename σ, a.rename σ, a'.rename σ, Kont.rename_plug σ K a, Kont.rename_plug σ K a', - head_rename σ hh⟩ + head_rename σ hh hnr, not_arrRedex_rename σ hnr⟩ -/-- **Runs are invariant under renaming.** For a permutation `σ` of the locations, a run from -`(μ, e)` to `(μ', e')` renames to a run from `(σ μ, σ e)` to `(σ μ', σ e')`. -/ -theorem steps_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {e e' : Expr} (h : Steps μ e μ' e') : - Steps (μ.rename σ) (e.rename σ) (μ'.rename σ) (e'.rename σ) := by +/-- **Runs with no array step are invariant under renaming.** For a permutation `σ` of the +locations, such a run from `(μ, e)` to `(μ', e')` renames to one from `(σ μ, σ e)` to +`(σ μ', σ e')`. -/ +theorem steps_rename (σ : Equiv.Perm Loc) {μ μ' : Heap} {e e' : Expr} (h : StepsR μ e μ' e') : + StepsR (μ.rename σ) (e.rename σ) (μ'.rename σ) (e'.rename σ) := by induction h with | refl => exact .refl _ _ | more h _ ih => exact .more (step1_rename σ h) ih diff --git a/experiments/TwoRun/TwoRun/Sim.lean b/experiments/TwoRun/TwoRun/Sim.lean index ab8e3e040..c0f41bf11 100644 --- a/experiments/TwoRun/TwoRun/Sim.lean +++ b/experiments/TwoRun/TwoRun/Sim.lean @@ -30,7 +30,9 @@ open BoCa open BoCa.Fig16 open BoCa.Fig16.BoLo open BoCa.BoLo (Heap Steps Step1 Head Kont) -open BoCa.Fig16.LogRel.Core.FrameTheorems (allocRedex head_det Footprint) +open BoCa.Fig16.LogRel.Core.FrameTheorems (allocRedex head_det Footprint ArrRedex arrRedex_tab + arrRedex_idx arrRedex_drop) +open BoCa.BoLo (not_arrRedex_rename) /-! ### The relation between the two runs' heaps -/ @@ -75,11 +77,14 @@ theorem HeapRel.injOn {g : Loc → Loc} {μB μA : Heap} (h : HeapRel g μB μA) /-- **A head step that is not an allocation, followed.** If `B` takes it, `A` takes it at the image under `g`, and the heaps stay related. -/ theorem head_sim {g : Loc → Loc} {μB μB' μA : Heap} {b b' : Expr} (hB : Head μB b μB' b') - (hR : HeapRel g μB μA) (hna : ∀ v : Val, b ≠ allocRedex v) : + (hR : HeapRel g μB μA) (hna : ∀ v : Val, b ≠ allocRedex v) (hnr : ¬ ArrRedex b) : ∃ μA', Head μA (b.rename g) μA' (b'.rename g) ∧ HeapRel g μB' μA' := by obtain ⟨h₁, h₂, h₃⟩ := hR cases hB with | alloc v _ _ => exact absurd rfl (hna v) + | tab => exact absurd (arrRedex_tab _) hnr + | idx => exact absurd (arrRedex_idx _) hnr + | drop => exact absurd (arrRedex_drop _) hnr | beta b v => rw [Expr.rename_subst] exact ⟨μA, .beta _ (b.rename g) (v.rename g), h₁, h₂, h₃⟩ @@ -172,7 +177,7 @@ the image, and the resulting configurations are related along an update of `g`. theorem step_sim {g : Loc → Loc} {μB μB' μA μA' : Heap} {eB eB' eA' : Expr} (hB : Step1F μB eB μB' eB') (hR : HeapRel g μB μA) (hA : Step1 μA (eB.rename g) μA' eA') : ∃ g', eA' = eB'.rename g' ∧ HeapRel g' μB' μA' := by - obtain ⟨⟨K, b, b', rfl, rfl, hhB⟩, hfB⟩ := hB + obtain ⟨⟨K, b, b', rfl, rfl, hhB, hnr⟩, hfB⟩ := hB obtain ⟨K', a, a', hE, rfl, hhA⟩ := hA rw [BoLo.Kont.rename_plug] at hE have hnv : ¬ IsVal (b.rename g) := fun h => hhB.not_isVal (isVal_of_rename g h) @@ -233,11 +238,11 @@ theorem step_sim {g : Loc → Loc} {μB μB' μA μA' : Heap} {eB eB' eA' : Expr obtain ⟨x, hx, rfl⟩ := h₃ k hk exact ⟨x, by rwa [Heap.upd_other (ne x hx)], Function.update_of_ne (ne x hx) _ _⟩ · push Not at hal - obtain ⟨μA'', hA'', hR''⟩ := head_sim hhB hR hal + obtain ⟨μA'', hA'', hR''⟩ := head_sim hhB hR hal hnr obtain rfl : K'' = .hole := Fig16.LogRel.Core.head_plug hA'' K'' hhA.not_isVal hb simp only [Kont.plug] at hb subst hb - obtain ⟨rfl, rfl⟩ := head_det hA'' hhA (not_allocRedex_rename g hal) + obtain ⟨rfl, rfl⟩ := head_det hA'' hhA (not_allocRedex_rename g hal) (not_arrRedex_rename g hnr) exact ⟨g, (BoLo.Kont.rename_plug g K b').symm, hR''⟩ /-! ### Runs -/ @@ -258,7 +263,7 @@ theorem run_sim {μB μB' : Heap} {eB eB' : Expr} (hB : FreshRun μB eB μB' eB' | more hs _ ih => intro hv g μA μA' eA' hR hA hvA cases hA with - | refl => exact absurd hs.1 (not_step1_of_isVal (isVal_of_rename g hvA)) + | refl => exact absurd hs.1.toStep1 (not_step1_of_isVal (isVal_of_rename g hvA)) | more h t => obtain ⟨g₁, rfl, hR₁⟩ := step_sim hs hR h exact ih hv hR₁ t hvA diff --git a/scripts/AxiomCheck.lean b/scripts/AxiomCheck.lean index 431980582..ae2f5ff99 100644 --- a/scripts/AxiomCheck.lean +++ b/scripts/AxiomCheck.lean @@ -46,3 +46,15 @@ import Core #print axioms BoCa.Fig16.LogRel.Core.sumListProg_eval #print axioms BoCa.Fig16.LogRel.Core.signProg_runs #print axioms BoCa.Fig16.LogRel.Core.signProg_eval +#print axioms BoCa.Fig16.LogRel.Core.wpTS_tab +#print axioms BoCa.Fig16.LogRel.Core.arrNew_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrFree_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrSize_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrReplace_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrLen_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrGet_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrCopy_compatX +#print axioms BoCa.Fig16.LogRel.Core.arrForget_compatX +#print axioms BoCa.Fig16.LogRel.Core.reborrow_vShape +#print axioms BoCa.Fig16.LogRel.Core.arrProg_runs +#print axioms BoCa.Fig16.LogRel.Core.arrProg_eval