Skip to content

Commit

Permalink
Add the let* monadic notation
Browse files Browse the repository at this point in the history
Inspired by the notation introduced by OCaml 4.08, we propose to
introduce a new notation following the same principle.

    let* x := p in q

is strictly equivalent to

    x <- p;; q
  • Loading branch information
lthms committed Aug 6, 2020
1 parent ae627bc commit 22d4734
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
30 changes: 30 additions & 0 deletions examples/Notations.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Require Import ExtLib.Structures.Monad.
Generalizable All Variables.

Module NotationExample.

Import MonadNotation.
Open Scope monad_scope.

Fixpoint repeatM `{Monad M} (n : nat) `(x : A) (p : A -> M A) : M unit :=
match n with
| O => ret tt
| S n => y <- p x;;
repeatM n y p
end.

End NotationExample.

Module LetNotationExample.

Import MonadLetNotation.
Open Scope monad_scope.

Fixpoint repeatM `{Monad M} (n : nat) `(x : A) (p : A -> M A) : M unit :=
match n with
| O => ret tt
| S n => let* y := p x in
repeatM n y p
end.

End LetNotationExample.
1 change: 1 addition & 0 deletions examples/_CoqProject
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ MonadReasoning.v
Printing.v
UsingSets.v
WithDemo.v
Nonations.v
26 changes: 22 additions & 4 deletions theories/Structures/Monad.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,47 @@ Section monadic.

End monadic.

Module MonadNotation.
Module MonadBaseNotation.

Delimit Scope monad_scope with monad.

Notation "c >>= f" := (@bind _ _ _ _ c f) (at level 58, left associativity) : monad_scope.
Notation "f =<< c" := (@bind _ _ _ _ c f) (at level 61, right associativity) : monad_scope.
Notation "f >=> g" := (@mcompose _ _ _ _ _ f g) (at level 61, right associativity) : monad_scope.

Notation "e1 ;; e2" := (@bind _ _ _ _ e1%monad (fun _ => e2%monad))%monad
(at level 61, right associativity) : monad_scope.

End MonadBaseNotation.

Module MonadNotation.

Export MonadBaseNotation.

Notation "x <- c1 ;; c2" := (@bind _ _ _ _ c1 (fun x => c2))
(at level 61, c1 at next level, right associativity) : monad_scope.

Notation "` x : t <- c1 ;; c2" := (@bind _ _ _ _ c1 (fun x : t => c2))
(at level 61, t at next level, c1 at next level, x ident, right associativity) : monad_scope.

Notation "e1 ;; e2" := (_ <- e1%monad ;; e2%monad)%monad
(at level 61, right associativity) : monad_scope.

Notation "' pat <- c1 ;; c2" :=
(@bind _ _ _ _ c1 (fun x => match x with pat => c2 end))
(at level 61, pat pattern, c1 at next level, right associativity) : monad_scope.

End MonadNotation.

Module MonadLetNotation.

Export MonadBaseNotation.

Notation "'let*' x ':=' c1 'in' c2" := (@bind _ _ _ _ c1 (fun x => c2))
(at level 61, c1 at next level, right associativity) : monad_scope.

Notation "'`let*' x ':' t ':=' c1 'in' c2" := (@bind _ _ _ _ c1 (fun x : t => c2))
(at level 61, t at next level, c1 at next level, x ident, right associativity) : monad_scope.

End MonadLetNotation.

Section Instances.

Universe d c.
Expand Down

0 comments on commit 22d4734

Please sign in to comment.