forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCycle.lean
79 lines (59 loc) · 2.84 KB
/
Cycle.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/-
Copyright (c) 2022 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
namespace Lake
/-- A sequence of calls donated by the key type `κ`. -/
abbrev CallStack κ := List κ
/-- A `CallStack` ending in a cycle. -/
abbrev Cycle κ := CallStack κ
/-- A monad equipped with a call stack. -/
class MonadCallStackOf (κ : semiOutParam (Type u)) (m : Type u → Type v) where
getCallStack : m (CallStack κ)
withCallStack (stack : CallStack κ) (x : m α) : m α
/-- Similar to `MonadCallStackOf`, but `κ` is an `outParam` for convenience. -/
class MonadCallStack (κ : outParam (Type u)) (m : Type u → Type v) where
getCallStack : m (CallStack κ)
withCallStack (stack : CallStack κ) (x : m α) : m α
export MonadCallStack (getCallStack withCallStack)
instance [MonadCallStackOf κ m] : MonadCallStack κ m where
getCallStack := MonadCallStackOf.getCallStack
withCallStack := MonadCallStackOf.withCallStack
instance [MonadLift m n] [MonadFunctor m n] [MonadCallStackOf κ m] : MonadCallStackOf κ n where
getCallStack := liftM (m := m) getCallStack
withCallStack s := monadMap (m := m) (withCallStack s ·)
/-- A monad equipped with a call stack and the ability to error on a cycle. -/
class MonadCycleOf (κ : semiOutParam (Type u)) (m : Type u → Type v) extends MonadCallStackOf κ m where
throwCycle (cycle : Cycle κ) : m α
/-- Similar to `MonadCycle`, but `κ` is an `outParam` for convenience. -/
class MonadCycle (κ : outParam (Type u)) (m : Type u → Type v) extends MonadCallStack κ m where
throwCycle (cycle : Cycle κ) : m α
export MonadCycle (throwCycle)
instance [MonadCycleOf κ m] : MonadCycle κ m where
throwCycle := MonadCycleOf.throwCycle
export MonadCycle (throwCycle)
instance [MonadLift m n] [MonadFunctor m n] [MonadCycleOf κ m] : MonadCycleOf κ n where
throwCycle cycle := liftM (m := m) (throwCycle cycle)
instance inhabitedOfMonadCycle [MonadCycle κ m] : Inhabited (m α) := ⟨throwCycle []⟩
/-- A transformer that equips a monad with a `CallStack`. -/
abbrev CallStackT κ m := ReaderT (CallStack κ) m
instance [Monad m] : MonadCallStackOf κ (CallStackT κ m) where
getCallStack := read
withCallStack s x := x s
/-- A transformer that equips a monad with a `CallStack` to detect cycles. -/
abbrev CycleT κ m := CallStackT κ <| ExceptT (Cycle κ) m
instance [Monad m] : MonadCycleOf κ (CycleT κ m) where
throwCycle := throw
/--
Add `key` to the monad's `CallStack` before invoking `act`.
If adding `key` produces a cycle, the cyclic call stack is thrown.
-/
@[inline] def guardCycle
[BEq κ] [Monad m] [MonadCycle κ m] (key : κ) (act : m α)
: m α := do
let parents ← getCallStack
if parents.contains key then
throwCycle <| key :: (parents.partition (· != key)).1 ++ [key]
else
withCallStack (key :: parents) act