author | haftmann |
Fri, 14 Jun 2019 08:34:27 +0000 | |
changeset 70333 | 0f7edf0853df |
parent 69593 | 3dda49e08b9d |
child 70433 | 2137db107788 |
permissions | -rw-r--r-- |
68061
81d90f830f99
clarified theory location and imports: avoid surprises due to Pure instead of Main (e.g. simpset operations);
wenzelm
parents:
66608
diff
changeset
|
1 |
(* Title: HOL/Library/Monad_Syntax.thy |
81d90f830f99
clarified theory location and imports: avoid surprises due to Pure instead of Main (e.g. simpset operations);
wenzelm
parents:
66608
diff
changeset
|
2 |
Author: Alexander Krauss, TU Muenchen |
81d90f830f99
clarified theory location and imports: avoid surprises due to Pure instead of Main (e.g. simpset operations);
wenzelm
parents:
66608
diff
changeset
|
3 |
Author: Christian Sternagel, University of Innsbruck |
37790 | 4 |
*) |
5 |
||
60500 | 6 |
section \<open>Monad notation for arbitrary types\<close> |
37790 | 7 |
|
8 |
theory Monad_Syntax |
|
68061
81d90f830f99
clarified theory location and imports: avoid surprises due to Pure instead of Main (e.g. simpset operations);
wenzelm
parents:
66608
diff
changeset
|
9 |
imports Adhoc_Overloading |
37790 | 10 |
begin |
11 |
||
60500 | 12 |
text \<open> |
37791 | 13 |
We provide a convenient do-notation for monadic expressions |
69593 | 14 |
well-known from Haskell. \<^const>\<open>Let\<close> is printed |
37791 | 15 |
specially in do-expressions. |
60500 | 16 |
\<close> |
37791 | 17 |
|
37790 | 18 |
consts |
62026 | 19 |
bind :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'd" (infixr "\<bind>" 54) |
37790 | 20 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
21 |
notation (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
22 |
bind (infixr ">>=" 54) |
37790 | 23 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
24 |
|
37790 | 25 |
abbreviation (do_notation) |
53616 | 26 |
bind_do :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'd" |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
27 |
where "bind_do \<equiv> bind" |
37790 | 28 |
|
29 |
notation (output) |
|
62026 | 30 |
bind_do (infixr "\<bind>" 54) |
37790 | 31 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
32 |
notation (ASCII output) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
33 |
bind_do (infixr ">>=" 54) |
37790 | 34 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
35 |
|
41229
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
wenzelm
parents:
39151
diff
changeset
|
36 |
nonterminal do_binds and do_bind |
37790 | 37 |
syntax |
38 |
"_do_block" :: "do_binds \<Rightarrow> 'a" ("do {//(2 _)//}" [12] 62) |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
39 |
"_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2_ \<leftarrow>/ _)" 13) |
37790 | 40 |
"_do_let" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2let _ =/ _)" [1000, 13] 13) |
41 |
"_do_then" :: "'a \<Rightarrow> do_bind" ("_" [14] 13) |
|
42 |
"_do_final" :: "'a \<Rightarrow> do_binds" ("_") |
|
43 |
"_do_cons" :: "[do_bind, do_binds] \<Rightarrow> do_binds" ("_;//_" [13, 12] 12) |
|
62026 | 44 |
"_thenM" :: "['a, 'b] \<Rightarrow> 'c" (infixr "\<then>" 54) |
37790 | 45 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
46 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
47 |
"_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2_ <-/ _)" 13) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
60500
diff
changeset
|
48 |
"_thenM" :: "['a, 'b] \<Rightarrow> 'c" (infixr ">>" 54) |
37790 | 49 |
|
50 |
translations |
|
51 |
"_do_block (_do_cons (_do_then t) (_do_final e))" |
|
62035 | 52 |
\<rightleftharpoons> "CONST bind_do t (\<lambda>_. e)" |
37790 | 53 |
"_do_block (_do_cons (_do_bind p t) (_do_final e))" |
62035 | 54 |
\<rightleftharpoons> "CONST bind_do t (\<lambda>p. e)" |
37790 | 55 |
"_do_block (_do_cons (_do_let p t) bs)" |
62035 | 56 |
\<rightleftharpoons> "let p = t in _do_block bs" |
37790 | 57 |
"_do_block (_do_cons b (_do_cons c cs))" |
62035 | 58 |
\<rightleftharpoons> "_do_block (_do_cons b (_do_final (_do_block (_do_cons c cs))))" |
37790 | 59 |
"_do_cons (_do_let p t) (_do_final s)" |
62035 | 60 |
\<rightleftharpoons> "_do_final (let p = t in s)" |
62189 | 61 |
"_do_block (_do_final e)" \<rightharpoonup> "e" |
62035 | 62 |
"(m \<then> n)" \<rightharpoonup> "(m \<bind> (\<lambda>_. n))" |
37790 | 63 |
|
52622
e0ff1625e96d
localized and modernized adhoc-overloading (patch by Christian Sternagel);
wenzelm
parents:
46143
diff
changeset
|
64 |
adhoc_overloading |
66608 | 65 |
bind Set.bind Predicate.bind Option.bind List.bind |
37790 | 66 |
|
66608 | 67 |
end |