Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make Monad_stateT a local instance #106

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/StateTMonad.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
From ExtLib Require Import
Monad
OptionMonad
StateMonad.

(** [Monad_stateT] is not in context, so this definition fails *)
Fail Definition foo : stateT unit option unit :=
ret tt.

(** Use [Existing Instance] to bring the Local [Monad_stateT] instance into context *)
Existing Instance Monad_stateT.

(** Now the definition succeeds *)
Definition foo : stateT unit option unit :=
ret tt.
3 changes: 2 additions & 1 deletion examples/_CoqProject
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ MonadReasoning.v
Printing.v
UsingSets.v
WithDemo.v
Notations.v
Notations.v
StateTMonad.v
6 changes: 4 additions & 2 deletions theories/Data/Monads/StateMonad.v
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ Section StateType.
Definition execStateT {t} (c : stateT t) (s : S) : m S :=
bind (runStateT c s) (fun x => ret (snd x)).


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