feat: add InternalExceptionId.lean
This commit is contained in:
parent
510cfb2a57
commit
4faaa392b3
2 changed files with 28 additions and 1 deletions
|
|
@ -24,6 +24,6 @@ import Lean.Structure
|
|||
import Lean.Delaborator
|
||||
import Lean.PrettyPrinter
|
||||
import Lean.CoreM
|
||||
|
||||
import Lean.InternalExceptionId
|
||||
-- import only for `[init]` side-effects
|
||||
import Lean.PrettyPrinter.Meta
|
||||
|
|
|
|||
27
src/Lean/InternalExceptionId.lean
Normal file
27
src/Lean/InternalExceptionId.lean
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/-
|
||||
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: Leonardo de Moura
|
||||
-/
|
||||
namespace Lean
|
||||
|
||||
structure InternalExceptionId :=
|
||||
(id : Nat)
|
||||
|
||||
def mkInternalExceptionsRef : IO (IO.Ref (Array Name)) :=
|
||||
IO.mkRef #[]
|
||||
|
||||
@[init mkInternalExceptionsRef] constant internalExceptionsRef : IO.Ref (Array Name) := arbitrary _
|
||||
|
||||
def registerInternalExceptionId (name : Name) : IO InternalExceptionId := do
|
||||
exs ← internalExceptionsRef.get;
|
||||
when (exs.contains name) $ throw $ IO.userError ("invalid internal exception id, '" ++ toString name ++ "' has already been used");
|
||||
let nextId := exs.size;
|
||||
internalExceptionsRef.modify fun a => a.push name;
|
||||
pure { id := nextId }
|
||||
|
||||
def InternalExceptionId.toString (id : InternalExceptionId) : IO Name := do
|
||||
exs ← internalExceptionsRef.get;
|
||||
pure $ exs.get! id.id
|
||||
|
||||
end Lean
|
||||
Loading…
Add table
Reference in a new issue