lean4-htt/tests/lean/optionGetD.lean
Mario Carneiro 178ab8ef2e
fix: Option.getD eagerly evaluates dflt (#3043)
Reported [on
Zulip](https://leanprover.zulipchat.com/#narrow/stream/348111-std4/topic/Panics.20in.20Std.2EHashMap.2Efind!/near/406872395).
The `dflt` argument of `Option.getD` is not evaluated lazily, as the
documentation says, because even after `macro_inline` the expression
```lean
match opt, dflt with
| some x, _ => x
| none, e => e
```
still has the semantics of evaluating `dflt` when `opt` is `some x`.
2023-12-11 10:07:30 +00:00

12 lines
307 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean.Data.HashMap
def test (m : Lean.HashMap Nat Nat) : IO (Nat × Nat) := do
let start := 1
let mut i := start
let mut count := 0
while i != 0 do
i := (m.find? i).getD (panic! "key is not in the map")
count := count + 1
return (i, count)
#eval test (.ofList [(1,3),(3,2),(2,0)])