aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/Monad.v
blob: 41a57e9e20b46f90710e054db553b13879f26cc8 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(*
 * Vericert: Verified high-level synthesis.
 * Copyright (C) 2020 Yann Herklotz <yann@yannherklotz.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *)

From Coq Require Import Lists.List.

Declare Scope vericert_scope.
#[local] Open Scope vericert_scope.

Declare Scope monad_scope.

Module Type Monad.

  Parameter mon : Type -> Type.

  Parameter ret : forall (A : Type) (x : A), mon A.
  Arguments ret [A].

  Parameter bind : forall (A B : Type) (g : A -> mon B) (f : mon A), mon B.
  Arguments bind [A B].

  Parameter bind2 : forall (A B C: Type) (g: A -> B -> mon C) (f: mon (A * B)), mon C.
  Arguments bind2 [A B C].

End Monad.

Module MonadExtra(M : Monad).
  Import M.

  Module Import MonadNotation.

Notation "'do' X <- A ; B" := (bind (fun X => B) A)
 (at level 200, X ident, A at level 100, B at level 200)
 : monad_scope.

Notation "'do' ( X , Y ) <- A ; B" := (bind2 (fun X Y => B) A)
 (at level 200, X ident, Y ident, A at level 100, B at level 200)
 : monad_scope.

  End MonadNotation.

  #[local] Open Scope monad_scope.

  Fixpoint traverselist {A B: Type} (f: A -> mon B) (l: list A) {struct l}: mon (list B) :=
    match l with
    | nil => ret nil
    | x::xs =>
      do r <- f x;
      do rs <- traverselist f xs;
      ret (r::rs)
    end.

  Fixpoint collectlist {A : Type} (f : A -> mon unit) (l : list A) {struct l} : mon unit :=
    match l with
    | nil => ret tt
    | x::xs => do y <- f x; collectlist f xs
    end.

  Definition mfold_left {A B} (f: A -> B -> mon A) (l: list B) (s: mon A): mon A :=
    fold_left (fun a b => do a' <- a; f a' b) l s.

End MonadExtra.

(** A [Params f n] instance forces the setoid rewriting mechanism not to
rewrite in the first [n] arguments of the function [f]. We will declare such
instances for all operational type classes in this development. *)
From Coq Require Export Morphisms RelationClasses.

From Coq Require Setoid.

Global Instance: Params (@Relation_Definitions.equiv) 2 := {}.

Class MRet (M : Type -> Type) := mret: forall {A}, A -> M A.
Global Arguments mret {_ _ _} _ : assert.
Global Instance: Params (@mret) 3 := {}.
Global Hint Mode MRet ! : typeclass_instances.

Class MBind (M : Type -> Type) := mbind : forall {A B}, (A -> M B) -> M A -> M B.
Global Arguments mbind {_ _ _ _} _ !_ / : assert.
Global Instance: Params (@mbind) 4 := {}.
Global Hint Mode MBind ! : typeclass_instances.

Class MJoin (M : Type -> Type) := mjoin: forall {A}, M (M A) -> M A.
Global Arguments mjoin {_ _ _} !_ / : assert.
Global Instance: Params (@mjoin) 3 := {}.
Global Hint Mode MJoin ! : typeclass_instances.

Class FMap (M : Type -> Type) := fmap : forall {A B}, (A -> B) -> M A -> M B.
Global Arguments fmap {_ _ _ _} _ !_ / : assert.
Global Instance: Params (@fmap) 4 := {}.
Global Hint Mode FMap ! : typeclass_instances.

Class OMap (M : Type -> Type) := omap: forall {A B}, (A -> option B) -> M A -> M B.
Global Arguments omap {_ _ _ _} _ !_ / : assert.
Global Instance: Params (@omap) 4 := {}.
Global Hint Mode OMap ! : typeclass_instances.

Notation "m ≫= f" := (mbind f m) (at level 60, right associativity) : vericert_scope.
Notation "( m ≫=.)" := (fun f => mbind f m) (only parsing) : vericert_scope.
Notation "(.≫= f )" := (mbind f) (only parsing) : vericert_scope.
Notation "(≫=)" := (fun m f => mbind f m) (only parsing) : vericert_scope.

Notation "x ← y ; z" := (y ≫= (fun x : _ => z))
  (at level 20, y at level 100, z at level 200, only parsing) : vericert_scope.

Notation "' x ← y ; z" := (y ≫= (fun x : _ => z))
  (at level 20, x pattern, y at level 100, z at level 200, only parsing) : vericert_scope.

Infix "<$>" := fmap (at level 61, left associativity) : vericert_scope.

Notation "x ;; z" := (x ≫= fun _ => z)
  (at level 100, z at level 200, only parsing, right associativity): vericert_scope.