Skip to content

Commit 737425e

Browse files
Make Monad_stateT a local instance (#106)
1 parent 7bce55d commit 737425e

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

examples/StateTMonad.v

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
From ExtLib Require Import
2+
Monad
3+
OptionMonad
4+
StateMonad.
5+
6+
(** [Monad_stateT] is not in context, so this definition fails *)
7+
Fail Definition foo : stateT unit option unit :=
8+
ret tt.
9+
10+
(** Use [Existing Instance] to bring the Local [Monad_stateT] instance into context *)
11+
Existing Instance Monad_stateT.
12+
13+
(** Now the definition succeeds *)
14+
Definition foo : stateT unit option unit :=
15+
ret tt.

examples/_CoqProject

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ MonadReasoning.v
77
Printing.v
88
UsingSets.v
99
WithDemo.v
10-
Notations.v
10+
Notations.v
11+
StateTMonad.v

theories/Data/Monads/StateMonad.v

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ Section StateType.
4343
Definition execStateT {t} (c : stateT t) (s : S) : m S :=
4444
bind (runStateT c s) (fun x => ret (snd x)).
4545

46-
47-
Global Instance Monad_stateT : Monad stateT :=
46+
(** [Monad_stateT] is not a Global Instance because it can cause an infinite loop
47+
in typeclass inference under certain circumstances. Use [Existing Instance
48+
Monad_stateT.] to bring the instance into context. *)
49+
Instance Monad_stateT : Monad stateT :=
4850
{ ret := fun _ x => mkStateT (fun s => @ret _ M _ (x,s))
4951
; bind := fun _ _ c1 c2 =>
5052
mkStateT (fun s =>

0 commit comments

Comments
 (0)