src/HOL/Library/Monad_Syntax.thy
changeset 37790 7fea92005066
child 37791 0d6b64060543
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Monad_Syntax.thy	Tue Jul 13 00:15:37 2010 +0200
     1.3 @@ -0,0 +1,60 @@
     1.4 +(* Author: Alexander Krauss, TU Muenchen
     1.5 +   Author: Christian Sternagel, University of Innsbruck
     1.6 +*)
     1.7 +
     1.8 +header {* Monad notation for arbitrary types *}
     1.9 +
    1.10 +theory Monad_Syntax
    1.11 +imports Adhoc_Overloading
    1.12 +begin
    1.13 +
    1.14 +consts
    1.15 +  bindM :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c" (infixr ">>=" 54)
    1.16 +
    1.17 +notation (xsymbols)
    1.18 +  bindM (infixr "\<guillemotright>=" 54)
    1.19 +
    1.20 +abbreviation (do_notation)
    1.21 +  bindM_do :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c"
    1.22 +where
    1.23 +  "bindM_do \<equiv> bindM"
    1.24 +
    1.25 +notation (output)
    1.26 +  bindM_do (infixr ">>=" 54)
    1.27 +
    1.28 +notation (xsymbols output)
    1.29 +  bindM_do (infixr "\<guillemotright>=" 54)
    1.30 +
    1.31 +nonterminals
    1.32 +  do_binds do_bind
    1.33 +
    1.34 +syntax
    1.35 +  "_do_block" :: "do_binds \<Rightarrow> 'a" ("do {//(2  _)//}" [12] 62)
    1.36 +  "_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ <-/ _)" 13)
    1.37 +  "_do_let" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2let _ =/ _)" [1000, 13] 13)
    1.38 +  "_do_then" :: "'a \<Rightarrow> do_bind" ("_" [14] 13)
    1.39 +  "_do_final" :: "'a \<Rightarrow> do_binds" ("_")
    1.40 +  "_do_cons" :: "[do_bind, do_binds] \<Rightarrow> do_binds" ("_;//_" [13, 12] 12)
    1.41 +  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr ">>" 54)
    1.42 +
    1.43 +syntax (xsymbols)
    1.44 +  "_do_bind"  :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ \<leftarrow>/ _)" 13)
    1.45 +  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr "\<guillemotright>" 54)
    1.46 +
    1.47 +translations
    1.48 +  "_do_block (_do_cons (_do_then t) (_do_final e))"
    1.49 +    == "CONST bindM_do t (\<lambda>_. e)"
    1.50 +  "_do_block (_do_cons (_do_bind p t) (_do_final e))"
    1.51 +    == "CONST bindM_do t (\<lambda>p. e)"
    1.52 +  "_do_block (_do_cons (_do_let p t) bs)"
    1.53 +    == "let p = t in _do_block bs"
    1.54 +  "_do_block (_do_cons b (_do_cons c cs))"
    1.55 +    == "_do_block (_do_cons b (_do_final (_do_block (_do_cons c cs))))"
    1.56 +  "_do_cons (_do_let p t) (_do_final s)"
    1.57 +    == "_do_final (let p = t in s)"
    1.58 +  "_do_block (_do_final e)" => "e"
    1.59 +  "(m >> n)" => "(m >>= (\<lambda>_. n))"
    1.60 +
    1.61 +setup {* Adhoc_Overloading.add_overloaded @{const_name bindM} *}
    1.62 +
    1.63 +end