After this commit, we have to use an explicit `discard` in code such as ``` def g (x : Nat) : IO Nat := ... def f (x : Nat) : IO Unit := do discard <| g x -- type error without the `discard` IO.println x ``` Motivation: prevent users from making mistakes such as ``` def f (xs : Array Nat) : IO Unit := do xs.set! 0 1 IO.println xs ``` when they meant to write ``` def f (xs : Array Nat) : IO Unit := do let xs := xs.set! 0 1 IO.println xs ```
24 lines
599 B
Text
24 lines
599 B
Text
--
|
|
def inc (r : IO.Ref Nat) : IO Unit := do
|
|
let v ← r.get;
|
|
r.set (v+1);
|
|
IO.println (">> " ++ toString v)
|
|
|
|
def initArray (r : IO.Ref (Array Nat)) (n : Nat) : IO Unit :=
|
|
n.forM $ fun i => do
|
|
r.modify $ fun a => a.push (2*i)
|
|
|
|
def showArrayRef (r : IO.Ref (Array Nat)) : IO Unit := do
|
|
let a ← r.swap ∅;
|
|
a.size.forM (fun i => IO.println ("[" ++ toString i ++ "]: " ++ toString (a.get! i)));
|
|
discard $ r.swap a;
|
|
pure ()
|
|
|
|
def tst (n : Nat) : IO Unit := do
|
|
let r₁ ← IO.mkRef 0;
|
|
n.forM fun _ => inc r₁;
|
|
let r₂ ← IO.mkRef (∅ : Array Nat);
|
|
initArray r₂ n;
|
|
showArrayRef r₂
|
|
|
|
#eval tst 10
|