/- From https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Ensuring.20naive.20purity Will revisit later -/ -- def natFun : Nat โ ByteArray := -- IO.getRandomBytes 8 |>.run' -- -- | k => match (IO.getRandomBytes 8 |>.run' ()) with -- -- | some bytes => bytes.toUInt64BE!.toNat % 256 -- -- | none => k -- -- #eval natFun 4 /- type mismatch EStateM.set s has type EStateM ?m.273 (?m.61 โ EStateM.Result ?m.60 ?m.61 ?m.61) PUnit : Type but is expected to have type Id Unit : Type -/ -- def ohNo : Unit := Id.run do -- let s โ EStateM.get -- IO.println "Oh no" -- EStateM.set s -- #eval ohNo def anyway : IO Unit := do -- v4.32: `IO` is no longer an `EStateM`, so the naive-purity attempt below no longer -- typechecks (see the live `ohNo` guard); the remainder of the experiment just draws -- random bytes without touching any state. let x := โ (ยท.toUInt64BE!.toNat % 256) <$> IO.getRandomBytes 8 let y := โ (ยท.toUInt64BE!.toNat % 256) <$> IO.getRandomBytes 8 let z := โ (ยท.toUInt64BE!.toNat % 256) <$> IO.getRandomBytes 8 IO.println f!"{x} {y} {z}" -- v4.32: `IO.getRandomBytes` depends on a `sorry`-based core definition, so plain `#eval` aborts; -- use `#eval!` to force evaluation.anywayanyway: IO Unit
The naive-purity attempt itself now fails to typecheck (previously IO was an EStateM, so
EStateM.get/EStateM.set compiled). The guard keeps the failure visible while the file still
elaborates.
error: Type mismatch EStateM.set s has type EStateM ?m.9 (?m.4 โ EStateM.Result ?m.3 ?m.4 ?m.4) PUnit but is expected to have type Id Unit
in defohNo :ohNo: UnitUnit :=Unit: TypeId.run do letId.run: {ฮฑ : Type} โ Id ฮฑ โ ฮฑs โs: ?m.4 โ EStateM.Result ?m.3 ?m.4 ?m.4EStateM.getEStateM.get: {ฮต ฯ : Type} โ EStateM ฮต ฯ ฯexample :example: 1 + 1 = 21 +1: Nat1 =1: Nat2 :=2: NatGoals accomplished! ๐Goals accomplished! ๐structureGoals accomplished! ๐Two whereTwo: Typex :x: Two โ NatNatNat: Typeproperty :property: โ (self : Two), self.x = 2x =x: Nat2 := by decide def2: Nata :a: TwoTwo := {x :=Two: Type2} def2: Natb :=b: TwoTwo.mkTwo.mk: (x : Nat) โ autoParam (x = 2) Two.property._autoParam โ Two2 def2: Natc :c: TwoTwo := โจTwo: Type2,2: Natrflโฉ -- hope to omit rflrfl: โ {ฮฑ : Type} {a : ฮฑ}, a = a