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