Built with Alectryon. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+โ†‘ Ctrl+โ†“ to navigate, Ctrl+๐Ÿ–ฑ๏ธ to focus. On Mac, use โŒ˜ instead of Ctrl.
Hover-Settings: Show types: Show goals:
/-

  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.
73 64 3
anyway: IO Unit
anyway

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 IO.println "Oh no" has type IO Unit but is expected to have type Id Unit

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

Error: โŒ๏ธ Docstring on `#guard_msgs` does not match generated message: - + error: Type mismatch + IO.println "Oh no" + has type + IO Unit + but is expected to have type + Id Unit + --- + 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 def
ohNo: Unit
ohNo
:
Unit: Type
Unit
:=
Id.run: {ฮฑ : Type} โ†’ Id ฮฑ โ†’ ฮฑ
Id.run
do let
s: ?m.4 โ†’ EStateM.Result ?m.3 ?m.4 ?m.4
s
โ†
EStateM.get: {ฮต ฯƒ : Type} โ†’ EStateM ฮต ฯƒ ฯƒ
EStateM.get
Error: Type mismatch IO.println "Oh no" has type IO Unit but is expected to have type Id Unit
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
example: 1 + 1 = 2
example
:
1: Nat
1
+
1: Nat
1
=
2: Nat
2
:=

Goals accomplished! ๐Ÿ™

Goals accomplished! ๐Ÿ™

Goals accomplished! ๐Ÿ™
structure
Two: Type
Two
where
x: Two โ†’ Nat
x
:
Nat: Type
Nat
property: โˆ€ (self : Two), self.x = 2
property
:
x: Nat
x
=
2: Nat
2
:= by decide def
a: Two
a
:
Two: Type
Two
:= {x :=
2: Nat
2
} def
b: Two
b
:=
Two.mk: (x : Nat) โ†’ autoParam (x = 2) Two.property._autoParam โ†’ Two
Two.mk
2: Nat
2
def
c: Two
c
:
Two: Type
Two
:= โŸจ
2: Nat
2
,
rfl: โˆ€ {ฮฑ : Type} {a : ฮฑ}, a = a
rfl
โŸฉ -- hope to omit rfl