blob: 22bc085ca5d5740e65bef2aed9cccfad7409ebb6 [file] [edit]
/-
# The `ExprOps` twins, differentially (task #97 P2b)
**The module-level differential check.** For every twin that has a
con-leche counterpart on `Expr`, one `#guard`:
> intern a term, run the twin, read the result back with `denoteE`, and
> compare with con-leche's own `ExprOps` function applied to the
> *denotation of the input*.
Nothing here is a proof and nothing here is an `#eval` print: every check is
kernel-reduced, so a wrong answer is a build failure. It is the cheapest
guard against the class of bug the bridge cannot see before P3 lands — an arm
that rebuilds the wrong node, a cursor that is bumped in the wrong place, a
cutoff that fires when it should not.
The expected side is computed, not written out: `E h` is the *denotation* of
a fixture handle, so `chkE (twin …) (Expr.f (E h) …)` is literally "the twin
agrees with con-leche on this term". What IS written out by hand is the
fixture's denotation (§"The fixture denotes what it should"), which is what
makes the computed expectations trustworthy.
DESIGN.md §8.4's "correctness before proofs": (B) must agree with con-leche
before a bridge lemma is written. This is that check at module scope; the
whole-checker one is P2f's `scripts/diff-e2e.sh --bin=`.
-/
module -- shake: keep-all (a `#guard` stores no constant shake can see)
public import ConRon.Arena.ExprOps
public import ConLeche.Kernel.ExprOps
/- The `#guard`s below are EVALUATED, so the constants they name have to be
reachable from meta code too; a module needed at both levels is imported
twice. -/
meta import ConRon.Arena.ExprOps
meta import ConLeche.Kernel.ExprOps
@[expose] public section
namespace ConRon.Arena
open ConLeche
/-! ## The fixture -/
/-- con-leche: none — the handles this module's checks name, in one record so
that the fixture is built by one `let` chain in the order a parser would. -/
private structure Fx where
st : EStore
z : LIdx
one : LIdx
pu : LIdx
anon : NIdx
foo : NIdx
bar : NIdx
uN : NIdx
usZ : LsIdx
usU : LsIdx
s0 : EIdx
s1 : EIdx
su : EIdx
cf : EIdx
cb : EIdx
b0 : EIdx
b1 : EIdx
b2 : EIdx
lit7 : EIdx
fv0 : EIdx
fv1 : EIdx
ap1 : EIdx
pj : EIdx
lamT : EIdx
allT : EIdx
letT : EIdx
big : EIdx
piT : EIdx
piS : EIdx
lamT2 : EIdx
spine : EIdx
/-- con-leche: none — the fixture: two sorts, a parameter sort, two
constants, three loose `bvar`s, two `fvar`s with annotations, a literal, and
the composite terms the checks below walk (a λ over a `let` over a `∀` over a
`proj` over a spine; a two-binder `∀`-telescope and its λ twin; an
application spine). -/
private def fx : Fx :=
let st := EStore.empty
let (st, z) := st.internLevel .zero
let (st, one) := st.internLevel (.succ z)
let (st, anon) := st.internName .anonymous
let (st, foo) := st.internName (.str anon "foo")
let (st, bar) := st.internName (.str anon "bar")
let (st, uN) := st.internName (.str anon "u")
let (st, pu) := st.internLevel (.param uN)
let (st, usZ) := st.internLevels [z]
let (st, usU) := st.internLevels [pu]
let (st, s0) := st.intern (.sort z)
let (st, s1) := st.intern (.sort one)
let (st, su) := st.intern (.sort pu)
let (st, cf) := st.intern (.const foo usZ)
let (st, cb) := st.intern (.const bar usU)
let (st, b0) := st.intern (.bvar 0)
let (st, b1) := st.intern (.bvar 1)
let (st, b2) := st.intern (.bvar 2)
let (st, lit7) := st.intern (.lit (.natVal 7))
let (st, fv0) := st.intern (.fvar 0 s0)
let (st, fv1) := st.intern (.fvar 1 s1)
let (st, ap1) := st.intern (.app cf b0)
let (st, pj) := st.intern (.proj foo 0 ap1)
let (st, lamT) := st.intern (.lam s0 ap1)
let (st, apbf) := st.intern (.app b1 fv0)
let (st, allT) := st.intern (.forallE s0 apbf)
let (st, apbb) := st.intern (.app b0 b1)
let (st, letT) := st.intern (.letE s0 cf apbb)
let (st, apcb) := st.intern (.app cb b0)
let (st, apfv) := st.intern (.app fv1 b0)
let (st, apb2) := st.intern (.app b2 apfv)
let (st, pj2) := st.intern (.proj foo 0 apb2)
let (st, inner) := st.intern (.forallE s1 pj2)
let (st, letB) := st.intern (.letE su apcb inner)
let (st, big) := st.intern (.lam s0 letB)
let (st, apb10) := st.intern (.app b1 b0)
let (st, piIn) := st.intern (.forallE s1 apb10)
let (st, piT) := st.intern (.forallE s0 piIn)
let (st, piS) := st.intern (.forallE s0 s1)
let (st, lamIn) := st.intern (.lam s1 apb10)
let (st, lamT2) := st.intern (.lam s0 lamIn)
let (st, sp1) := st.intern (.app cf fv0)
let (st, spine) := st.intern (.app sp1 b0)
{ st, z, one, pu, anon, foo, bar, uN, usZ, usU,
s0, s1, su, cf, cb, b0, b1, b2, lit7, fv0, fv1,
ap1, pj, lamT, allT, letT, big, piT, piS, lamT2, spine }
/-- con-leche: none — the initial checker state over the fixture. -/
private def S0 : AState := AState.init fx.st
/-- con-leche: none — the fuel every check runs at. The fixture's deepest
term is six nodes deep; 100 is a comfortable margin and the walks all
terminate long before it. -/
private def F : Nat := 100
/-- con-leche: none — the denotation of a fixture handle. Every handle below
is checked to denote (§"The fixture denotes what it should"), so the default
is unreachable. -/
private def E (h : EIdx) : Expr := (denoteE fx.st h).getD (.bvar 999)
/-- con-leche: none — the denotation of a fixture name handle. -/
private def N (h : NIdx) : ConLeche.Name := (denoteN fx.st.ns h).getD .anonymous
/-- con-leche: none — the denotation of a fixture level handle. -/
private def L (h : LIdx) : Level := (denoteL fx.st.ls h).getD .zero
/-! ## The fixture denotes what it should
The one place a con-leche value is written out by hand. Everything after
this section compares the twin's answer against con-leche's function applied
to `E h`, which is only meaningful because these hold. -/
#guard denoteE fx.st fx.s0 == some (Expr.sort .zero)
#guard denoteE fx.st fx.s1 == some (Expr.sort (.succ .zero))
#guard denoteE fx.st fx.su == some (Expr.sort (.param (.str .anonymous "u")))
#guard denoteE fx.st fx.cf == some (Expr.const (.str .anonymous "foo") [.zero])
#guard denoteE fx.st fx.cb ==
some (Expr.const (.str .anonymous "bar") [.param (.str .anonymous "u")])
#guard denoteE fx.st fx.fv1 == some (Expr.fvar 1 (.sort (.succ .zero)))
#guard denoteE fx.st fx.lit7 == some (Expr.lit (.natVal 7))
#guard denoteE fx.st fx.lamT ==
some (Expr.lam (.sort .zero)
(.app (.const (.str .anonymous "foo") [.zero]) (.bvar 0)))
#guard denoteE fx.st fx.big ==
some (Expr.lam (.sort .zero)
(.letE (.sort (.param (.str .anonymous "u")))
(.app (.const (.str .anonymous "bar") [.param (.str .anonymous "u")]) (.bvar 0))
(.forallE (.sort (.succ .zero))
(.proj (.str .anonymous "foo") 0
(.app (.bvar 2) (.app (.fvar 1 (.sort (.succ .zero))) (.bvar 0))))
))
)
#guard denoteE fx.st fx.piT ==
some (Expr.forallE (.sort .zero)
(.forallE (.sort (.succ .zero)) (.app (.bvar 1) (.bvar 0))))
#guard (denoteE fx.st fx.letT).isSome
#guard (denoteE fx.st fx.allT).isSome
#guard (denoteE fx.st fx.pj).isSome
#guard (denoteE fx.st fx.spine).isSome
#guard (denoteE fx.st fx.lamT2).isSome
#guard (denoteE fx.st fx.piS).isSome
#guard denoteN fx.st.ns fx.foo == some (.str .anonymous "foo")
#guard denoteL fx.st.ls fx.pu == some (.param (.str .anonymous "u"))
#guard denoteLs fx.st.lss fx.usZ == some [Level.zero]
/-! ## The checkers
One per result shape. Each runs the twin from `S0`, reads the answer back
against the store the run *ended* in (the arena grows), and compares. -/
/-- con-leche: none — map `denoteE` over a list of handles. -/
private def mapDen (s : EStore) : List EIdx → Option (List Expr)
| [] => some []
| h :: hs =>
match denoteE s h, mapDen s hs with
| some e, some es => some (e :: es)
| _, _ => none
/-- con-leche: none — map `denoteE` over a list of indexed handles. -/
private def mapDenIdx (s : EStore) : List (Nat × EIdx) → Option (List (Nat × Expr))
| [] => some []
| (i, h) :: hs =>
match denoteE s h, mapDenIdx s hs with
| some e, some es => some ((i, e) :: es)
| _, _ => none
/-- con-leche: none — an `EIdx`-valued twin against a con-leche `Expr`. -/
private def chkE (c : AM EIdx) (expect : Expr) : Bool :=
match c.run S0 with
| .ok (r, s') => denoteE s'.store r == some expect
| .error _ => false
/-- con-leche: none — two handle-valued twins agree on the DENOTATION: what an
executed walk owes its spec (task #97-P6-11's `abstractRangeC = abstractRange`).
Each runs from the same initial store, so the two handles may differ; the
denotations may not. -/
private def chkSame (a b : AM EIdx) : Bool :=
match a.run S0, b.run S0 with
| .ok (x, s), .ok (y, t) =>
match denoteE s.store x, denoteE t.store y with
| some u, some v => u == v
| _, _ => false
| _, _ => false
/-- con-leche: none — an `Option EIdx`-valued twin. -/
private def chkOE (c : AM (Option EIdx)) (expect : Option Expr) : Bool :=
match c.run S0, expect with
| .ok (some r, s'), some e => denoteE s'.store r == some e
| .ok (none, _), none => true
| _, _ => false
/-- con-leche: none — a `List EIdx`-valued twin. -/
private def chkLE (c : AM (List EIdx)) (expect : List Expr) : Bool :=
match c.run S0 with
| .ok (r, s') => mapDen s'.store r == some expect
| .error _ => false
/-- con-leche: none — a `Bool`-valued twin. -/
private def chkB (c : AM Bool) (expect : Bool) : Bool :=
match c.run S0 with
| .ok (r, _) => r == expect
| .error _ => false
/-- con-leche: none — a `Nat`-valued twin. -/
private def chkN (c : AM Nat) (expect : Nat) : Bool :=
match c.run S0 with
| .ok (r, _) => r == expect
| .error _ => false
/-- con-leche: none — the `fvarLeaves` shape. -/
private def chkFvL (c : AM (List (Nat × EIdx))) (expect : List (Nat × Expr)) : Bool :=
match c.run S0 with
| .ok (r, s') => mapDenIdx s'.store r == some expect
| .error _ => false
/-- con-leche: none — the `instPisAt` / `instLamsAt` shape. -/
private def chkPair (c : AM (Option (List EIdx × EIdx)))
(expect : Option (List Expr × Expr)) : Bool :=
match c.run S0, expect with
| .ok (some r, s'), some p =>
mapDen s'.store r.1 == some p.1 && denoteE s'.store r.2 == some p.2
| .ok (none, _), none => true
| _, _ => false
/-- con-leche: none — the `stripLams` / `stripPis` shape. -/
private def chkBinders (c : AM (Option (List EIdx × EIdx)))
(expect : Option (List Expr × Expr)) : Bool :=
match c.run S0, expect with
| .ok (some r, s'), some p =>
mapDen s'.store r.1 == some p.1 && denoteE s'.store r.2 == some p.2
| .ok (none, _), none => true
| _, _ => false
/-! ## `instantiate1` — `ExprOps.lean:29-45`, `:80-116`, `:182-184` -/
#guard chkE (instantiate1Fast F fx.big fx.cf 0) ((E fx.big).instantiate1 (E fx.cf) 0)
#guard chkE (instantiate1Fast F fx.big fx.cf 1) ((E fx.big).instantiate1 (E fx.cf) 1)
#guard chkE (instantiate1Fast F fx.letT fx.s1 0) ((E fx.letT).instantiate1 (E fx.s1) 0)
#guard chkE (instantiate1Fast F fx.b2 fx.cf 0) ((E fx.b2).instantiate1 (E fx.cf) 0)
#guard chkE (instantiate1Fast F fx.b0 fx.cf 0) ((E fx.b0).instantiate1 (E fx.cf) 0)
#guard chkE (instantiate1Fast F fx.lit7 fx.cf 0) ((E fx.lit7).instantiate1 (E fx.cf) 0)
#guard chkE (instantiate1Fast F fx.pj fx.fv0 0) ((E fx.pj).instantiate1 (E fx.fv0) 0)
-- the memoized walk without the top-level bracket is the same answer
#guard chkE (instantiate1Go fx.cf F fx.big 0) ((E fx.big).instantiate1 (E fx.cf) 0)
/-! ## `instantiateList` — `ExprOps.lean:191-235`, `:267-303`, `:371-373` -/
-- **The accumulator is an `Array` in PUSH order** (task #97-P6-15's clause
-- change, the maintainer's ruling): the array denotes the REVERSED list, so
-- con-leche's `[cf, s1]` is the twin's `#[s1, cf]`. These guards are the
-- reversal's own test.
#guard chkE (instantiateListFast F fx.big #[fx.s1, fx.cf] 0)
((E fx.big).instantiateList [E fx.cf, E fx.s1] 0)
#guard chkE (instantiateListFast F fx.big #[fx.s1, fx.cf] 1)
((E fx.big).instantiateList [E fx.cf, E fx.s1] 1)
#guard chkE (instantiateListFast F fx.letT #[fx.fv0] 0)
((E fx.letT).instantiateList [E fx.fv0] 0)
#guard chkE (instantiateListFast F fx.b2 #[fx.cf] 0) ((E fx.b2).instantiateList [E fx.cf] 0)
-- the unmemoized walk (the one `instantiateListGo`'s `bvar` arm calls)
#guard chkE (instantiateList #[fx.s1, fx.cf] 2 2 F fx.big 0)
((E fx.big).instantiateList [E fx.cf, E fx.s1] 0)
#guard chkE (instantiateList #[fx.cf] 1 1 F fx.b0 0) ((E fx.b0).instantiateList [E fx.cf] 0)
-- a range of a longer array, read in place: `vs[1:3]` is the list `[vs[2], vs[1]]`
#guard chkE (instantiateRevRange F fx.big #[fx.b0, fx.s1, fx.cf, fx.fv0] 1 3 0)
((E fx.big).instantiateList [E fx.cf, E fx.s1] 0)
#guard chkE (instantiateList #[fx.b0, fx.s1, fx.cf, fx.fv0] 3 2 F fx.big 0)
((E fx.big).instantiateList [E fx.cf, E fx.s1] 0)
-- an empty range is the identity
#guard chkE (instantiateRevRange F fx.big #[fx.s1, fx.cf] 2 1 0) (E fx.big)
-- `k = 0` is the identity, even on a term that HAS free variables
#guard chkSame (abstractRangeFast F fx.fv0 0 0 0) (pure fx.fv0)
-- and at `k = 1` it is `abstract1` (con-leche's `abstractRange_succ`)
#guard chkSame (abstractRangeFast F fx.big 0 1 0) (abstract1Fast F fx.big 0 0)
/-! ## `liftLooseBVars` — `ExprOps.lean:380-400`, `:430-466`, `:532-534` -/
#guard chkE (liftLooseBVarsFast F 2 0 fx.big) (Expr.liftLooseBVars 2 0 (E fx.big))
#guard chkE (liftLooseBVarsFast F 3 1 fx.big) (Expr.liftLooseBVars 3 1 (E fx.big))
#guard chkE (liftLooseBVarsFast F 1 0 fx.letT) (Expr.liftLooseBVars 1 0 (E fx.letT))
#guard chkE (liftLooseBVarsGo 2 F fx.big 0) (Expr.liftLooseBVars 2 0 (E fx.big))
/-! ## The measures and the scope predicates -/
#guard chkN (sizeB F fx.big) (Expr.sizeB (E fx.big))
#guard chkN (sizeB F fx.fv1) (Expr.sizeB (E fx.fv1))
#guard chkE (abstractRangeFast F fx.big 0 2 0) (Expr.abstractRange (E fx.big) 0 2 0)
#guard chkE (abstractRangeFast F fx.big 1 1 0) (Expr.abstractRange (E fx.big) 1 1 0)
#guard chkE (abstractRangeFast F fx.allT 0 3 1) (Expr.abstractRange (E fx.allT) 0 3 1)
#guard chkFvL (fvarLeaves F fx.big) (Expr.fvarLeaves (E fx.big))
#guard chkFvL (fvarLeaves F fx.fv1) (Expr.fvarLeaves (E fx.fv1))
#guard chkFvL (fvarLeaves F fx.s0) (Expr.fvarLeaves (E fx.s0))
#guard chkB (wscopedBFast F 5 fx.big) (Expr.wscopedB 5 (E fx.big))
#guard chkB (wscopedBFast F 1 fx.big) (Expr.wscopedB 1 (E fx.big))
#guard chkB (wscopedBFast F 0 fx.big) (Expr.wscopedB 0 (E fx.big))
#guard chkB (wscopedBFast F 2 fx.letT) (Expr.wscopedB 2 (E fx.letT))
/-! ### The memoized scope queries (task #97g)
`wscopedBFast`, `fvarLeavesFast` and `leafGuard` are `Cached/ExprOpsC.lean`'s
walks, and the point of having the pure ones beside them is that the two can
be compared: the expected side is the PURE arena walk (not a hand-written
answer), so a memo that answers differently from the walk it memoizes fails
the build. -/
/-- con-leche: none — a `Bool`-valued twin whose EXPECTED side is another
arena computation, for the memoized-against-pure differentials. -/
private def chkBB (c d : AM Bool) : Bool :=
match c.run S0, d.run S0 with
| .ok (r, _), .ok (r', _) => r == r'
| _, _ => false
/-- con-leche: none — the same for the `fvarLeaves` shape: the memoized walk
accumulates on the way IN, so the two lists are compared as sets. -/
private def chkFvLL (c d : AM (List (Nat × EIdx))) : Bool :=
match c.run S0, d.run S0 with
| .ok (r, _), .ok (r', _) =>
r.all (fun x => r'.contains x) && r'.all (fun x => r.contains x)
| _, _ => false
#guard chkB (wscopedBFast F 5 fx.big) (Expr.wscopedB 5 (E fx.big))
#guard chkB (wscopedBFast F 0 fx.big) (Expr.wscopedB 0 (E fx.big))
#guard chkFvLL (fvarLeavesFast F fx.big) (fvarLeaves F fx.big)
#guard chkFvLL (fvarLeavesFast F fx.fv1) (fvarLeaves F fx.fv1)
#guard chkFvLL (fvarLeavesFast F fx.s0) (fvarLeaves F fx.s0)
/-- con-leche: none — `leafGuard fab base` against `Core.lean`'s
specification, `fvarLeavesSubset (fvarLeaves fab) (fvarLeaves base)`. -/
private def leafSpec (fab base : EIdx) : AM Bool := do
let fl ← fvarLeaves F fab
let ml ← fvarLeaves F base
pure (fl.all (fun l => ml.contains l))
#guard chkBB (leafGuard F fx.big fx.big) (leafSpec fx.big fx.big)
#guard chkBB (leafGuard F fx.fv1 fx.big) (leafSpec fx.fv1 fx.big)
#guard chkBB (leafGuard F fx.big fx.fv1) (leafSpec fx.big fx.fv1)
#guard chkBB (leafGuard F fx.s0 fx.fv1) (leafSpec fx.s0 fx.fv1)
#guard chkBB (leafGuard F fx.letT fx.big) (leafSpec fx.letT fx.big)
#guard chkB (looseBVarsBoundedFast F 3 fx.big) (Expr.looseBVarsBounded 3 (E fx.big))
#guard chkB (looseBVarsBoundedFast F 0 fx.big) (Expr.looseBVarsBounded 0 (E fx.big))
#guard chkB (looseBVarsBoundedFast F 1 fx.letT) (Expr.looseBVarsBounded 1 (E fx.letT))
/-! ## The one-node readers -/
#guard isForallE fx.piT == Expr.isForallE (E fx.piT)
#guard isForallE fx.big == Expr.isForallE (E fx.big)
#guard
match (binderPair? true fx.lamT2 fx.lamT).run S0, Expr.binderPair? true (E fx.lamT2) (E fx.lamT) with
| .ok (some (a, b, c, d), s'), some (a', b', c', d') =>
denoteE s'.store a == some a' && denoteE s'.store b == some b' &&
denoteE s'.store c == some c' && denoteE s'.store d == some d'
| _, _ => false
#guard
match (binderPair? true fx.lamT2 fx.piT).run S0, Expr.binderPair? true (E fx.lamT2) (E fx.piT) with
| .ok (none, _), none => true
| _, _ => false
#guard
match (binderPair? false fx.piT fx.lamT).run S0, Expr.binderPair? false (E fx.piT) (E fx.lamT) with
| .ok (none, _), none => true
| _, _ => false
#guard chkB (hasFvarFast F fx.big) (Expr.hasFvar (E fx.big))
#guard chkB (hasFvarFast F fx.piT) (Expr.hasFvar (E fx.piT))
#guard chkB (hasFvarFast F fx.fv0) (Expr.hasFvar (E fx.fv0))
/-! ## Application spines -/
#guard chkE (getAppFn F fx.spine) (Expr.getAppFn (E fx.spine))
#guard chkE (getAppFn F fx.cf) (Expr.getAppFn (E fx.cf))
#guard chkLE (getAppArgs F fx.spine) (Expr.getAppArgs (E fx.spine))
#guard chkLE (getAppArgs F fx.cf) (Expr.getAppArgs (E fx.cf))
#guard chkE (mkAppN fx.cf [fx.fv0, fx.b0]) (Expr.mkAppN (E fx.cf) [E fx.fv0, E fx.b0])
#guard chkE (mkAppN fx.cf []) (Expr.mkAppN (E fx.cf) [])
/-! ## Telescopes -/
#guard chkBinders (stripLams 2 fx.lamT2) (Expr.stripLams 2 (E fx.lamT2))
#guard chkBinders (stripLams 0 fx.lamT2) (Expr.stripLams 0 (E fx.lamT2))
#guard chkBinders (stripLams 3 fx.lamT2) (Expr.stripLams 3 (E fx.lamT2))
#guard chkBinders (stripPis 2 fx.piT) (Expr.stripPis 2 (E fx.piT))
#guard chkBinders (stripPis 1 fx.piT) (Expr.stripPis 1 (E fx.piT))
#guard chkBinders (stripPis 3 fx.piT) (Expr.stripPis 3 (E fx.piT))
#guard chkE (piResult F fx.piT) (Expr.piResult (E fx.piT))
#guard chkE (piResult F fx.cf) (Expr.piResult (E fx.cf))
#guard chkE (fvarTypeD fx.fv1) (Expr.fvarTypeD (E fx.fv1))
#guard chkE (fvarTypeD fx.cf) (Expr.fvarTypeD (E fx.cf))
#guard chkE (instSpine F [fx.cf, fx.s1] 1 fx.big)
(Expr.instSpine [E fx.cf, E fx.s1] 1 (E fx.big))
#guard chkE (instSpine F [] 0 fx.big) (Expr.instSpine [] 0 (E fx.big))
/-! ## The packed range fields -/
#guard chkN (bvarBoundMemo F fx.big) (Expr.bvarBound (E fx.big))
#guard chkN (bvarBoundMemo F fx.letT) (Expr.bvarBound (E fx.letT))
#guard chkN (bvarBoundMemo F fx.b2) (Expr.bvarBound (E fx.b2))
#guard chkN (bvarBoundGo F fx.big) (Expr.bvarBound (E fx.big))
#guard chkN (fvarRangeMemo F fx.big) (Expr.fvarRange (E fx.big))
#guard chkN (fvarRangeMemo F fx.fv1) (Expr.fvarRange (E fx.fv1))
#guard chkN (fvarRangeGo F fx.big) (Expr.fvarRange (E fx.big))
#guard chkN (bvarB F fx.big) (Expr.bvarB (E fx.big))
#guard chkN (bvarB F fx.b2) (Expr.bvarB (E fx.b2))
#guard chkN (fvarB F fx.big) (Expr.fvarB (E fx.big))
#guard chkN (fvarB F fx.fv1) (Expr.fvarB (E fx.fv1))
#guard chkB (hasFvarFast F fx.big) (Expr.hasFvarFast (E fx.big))
#guard chkB (hasFvarFast F fx.piT) (Expr.hasFvarFast (E fx.piT))
#guard chkB (looseBVarsBoundedFast F 3 fx.big) (Expr.looseBVarsBoundedFast 3 (E fx.big))
#guard chkB (looseBVarsBoundedFast F 0 fx.big) (Expr.looseBVarsBoundedFast 0 (E fx.big))
-- the field read and the walk agree, which is con-leche's `bvarB_eq` /
-- `fvarB_eq` / `hasFvar_eq` at the fixture
#guard chkN (bvarB F fx.big) (Expr.bvarBound (E fx.big))
#guard chkN (fvarB F fx.big) (Expr.fvarRange (E fx.big))
#guard chkB (hasFvarFast F fx.big) (Expr.hasFvar (E fx.big))
#guard chkB (looseBVarsBoundedFast F 3 fx.big) (Expr.looseBVarsBounded 3 (E fx.big))
/-! ## `abstract1` — `ExprOps.lean:760-776`, `:1789-1833`, `:1927-1929` -/
#guard chkE (abstract1Fast F fx.big 0 0) (Expr.abstract1 (E fx.big) 0 0)
#guard chkE (abstract1Fast F fx.big 1 0) (Expr.abstract1 (E fx.big) 1 0)
#guard chkE (abstract1Fast F fx.big 2 0) (Expr.abstract1 (E fx.big) 2 0)
#guard chkE (abstract1Fast F fx.big 1 3) (Expr.abstract1 (E fx.big) 1 3)
#guard chkE (abstract1Fast F fx.fv0 0 0) (Expr.abstract1 (E fx.fv0) 0 0)
#guard chkE (abstract1Go 1 F fx.big 0) (Expr.abstract1 (E fx.big) 1 0)
/-! ## `instantiateLift` — `ExprOps.lean:921-952`, `Cached/ExprOpsC.lean:339-420`
The values in push order: con-leche's `[cf, b1]` is the twin's `#[b1, cf]`. -/
#guard chkE (instantiateLiftFast F fx.big #[fx.cf] 0) ((E fx.big).instantiateLift [E fx.cf] 0)
#guard chkE (instantiateLiftFast F fx.big #[fx.b1] 0) ((E fx.big).instantiateLift [E fx.b1] 0)
#guard chkE (instantiateLiftFast F fx.big #[fx.letT] 1) ((E fx.big).instantiateLift [E fx.letT] 1)
#guard chkE (instantiateLiftFast F fx.letT #[fx.b0] 0) ((E fx.letT).instantiateLift [E fx.b0] 0)
#guard chkE (instantiateLiftFast F fx.big #[fx.b1, fx.cf] 0)
((E fx.big).instantiateLift [E fx.cf, E fx.b1] 0)
#guard chkE (instantiateLiftFast F fx.piT #[fx.b0, fx.cf] 1)
((E fx.piT).instantiateLift [E fx.cf, E fx.b0] 1)
#guard chkE (instantiateLiftFast F fx.big #[] 0) ((E fx.big).instantiateLift [] 0)
#guard chkE (instantiateLiftGo #[fx.cf] F fx.big 0) ((E fx.big).instantiateLift [E fx.cf] 0)
/-! ## Equality -/
#guard (fx.big == fx.big) == (E fx.big == E fx.big)
#guard (fx.big == fx.letT) == (E fx.big == E fx.letT)
#guard (fx.s0 == fx.s1) == (E fx.s0 == E fx.s1)
-- interning is hash-consing, so a rebuilt node is the SAME handle and the
-- index test is the structural test (`denoteE_inj`, task #97a)
#guard
match (internE (.app fx.cf fx.b0)).run S0 with
| .ok (r, _) => r == fx.ap1
| .error _ => false
/-! ## `instantiateLevelParams` — `ExprOps.lean:2564-2603`, `:2718-2720` -/
#guard chkE (instLPFast F [fx.uN] fx.usZ fx.big)
(Expr.instantiateLevelParams [N fx.uN] [L fx.z] (E fx.big))
#guard chkE (instLPFast F [fx.uN] fx.usU fx.big)
(Expr.instantiateLevelParams [N fx.uN] [L fx.pu] (E fx.big))
#guard chkE (instLPFast F [fx.uN] fx.usZ fx.su)
(Expr.instantiateLevelParams [N fx.uN] [L fx.z] (E fx.su))
#guard chkE (instLPFast F [fx.uN] fx.usZ fx.cb)
(Expr.instantiateLevelParams [N fx.uN] [L fx.z] (E fx.cb))
#guard chkE (instLPFast F [fx.uN] fx.usZ fx.piT)
(Expr.instantiateLevelParams [N fx.uN] [L fx.z] (E fx.piT))
#guard chkE (instLPGo [N fx.uN] [L fx.z] F fx.big)
(Expr.instantiateLevelParams [N fx.uN] [L fx.z] (E fx.big))
/-! ## The store primitives of `Monad.lean` -/
#guard
match (view fx.big).run S0 with
| .ok (.lam ty body, _) => ty == fx.s0 && body != fx.s0
| _ => false
#guard
match (readLevel fx.pu).run S0 with
| .ok (u, _) => u == Level.param (.str .anonymous "u")
| .error _ => false
#guard
match (readName fx.foo).run S0 with
| .ok (n, _) => n == ConLeche.Name.str .anonymous "foo"
| .error _ => false
#guard
match (internLevel (.succ (.succ .zero))).run S0 with
| .ok (u, s') => denoteL s'.store.ls u == some (.succ (.succ .zero))
| .error _ => false
#guard
match (internName (.str (.str .anonymous "a") "b")).run S0 with
| .ok (n, s') => denoteN s'.store.ns n == some (.str (.str .anonymous "a") "b")
| .error _ => false
#guard
match (internLevels [Level.zero, .param (.str .anonymous "u")]).run S0 with
| .ok (us, s') =>
denoteLs s'.store.lss us == some [Level.zero, .param (.str .anonymous "u")]
| .error _ => false
/-! ## Fuel exhaustion fails rather than answering wrongly -/
-- `big`'s loose-bvar bound is 0, so the derived-word cutoff answers it at
-- any fuel; `letT`'s is 1, so the walk really descends and fuel 1 runs out.
#guard chkE (instantiate1Fast 1 fx.big fx.cf 0) ((E fx.big).instantiate1 (E fx.cf) 0)
#guard
match (instantiate1Fast 1 fx.letT fx.cf 0).run S0 with
| .ok _ => false
| .error _ => true
#guard
match (sizeB 0 fx.big).run S0 with
| .ok _ => false
| .error _ => true
end ConRon.Arena