src/HOL/Library/Monad_Syntax.thy
author krauss
Tue Jul 13 00:15:37 2010 +0200 (2010-07-13)
changeset 37790 7fea92005066
child 37791 0d6b64060543
permissions -rw-r--r--
uniform do notation for monads
krauss@37790
     1
(* Author: Alexander Krauss, TU Muenchen
krauss@37790
     2
   Author: Christian Sternagel, University of Innsbruck
krauss@37790
     3
*)
krauss@37790
     4
krauss@37790
     5
header {* Monad notation for arbitrary types *}
krauss@37790
     6
krauss@37790
     7
theory Monad_Syntax
krauss@37790
     8
imports Adhoc_Overloading
krauss@37790
     9
begin
krauss@37790
    10
krauss@37790
    11
consts
krauss@37790
    12
  bindM :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c" (infixr ">>=" 54)
krauss@37790
    13
krauss@37790
    14
notation (xsymbols)
krauss@37790
    15
  bindM (infixr "\<guillemotright>=" 54)
krauss@37790
    16
krauss@37790
    17
abbreviation (do_notation)
krauss@37790
    18
  bindM_do :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c"
krauss@37790
    19
where
krauss@37790
    20
  "bindM_do \<equiv> bindM"
krauss@37790
    21
krauss@37790
    22
notation (output)
krauss@37790
    23
  bindM_do (infixr ">>=" 54)
krauss@37790
    24
krauss@37790
    25
notation (xsymbols output)
krauss@37790
    26
  bindM_do (infixr "\<guillemotright>=" 54)
krauss@37790
    27
krauss@37790
    28
nonterminals
krauss@37790
    29
  do_binds do_bind
krauss@37790
    30
krauss@37790
    31
syntax
krauss@37790
    32
  "_do_block" :: "do_binds \<Rightarrow> 'a" ("do {//(2  _)//}" [12] 62)
krauss@37790
    33
  "_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ <-/ _)" 13)
krauss@37790
    34
  "_do_let" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2let _ =/ _)" [1000, 13] 13)
krauss@37790
    35
  "_do_then" :: "'a \<Rightarrow> do_bind" ("_" [14] 13)
krauss@37790
    36
  "_do_final" :: "'a \<Rightarrow> do_binds" ("_")
krauss@37790
    37
  "_do_cons" :: "[do_bind, do_binds] \<Rightarrow> do_binds" ("_;//_" [13, 12] 12)
krauss@37790
    38
  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr ">>" 54)
krauss@37790
    39
krauss@37790
    40
syntax (xsymbols)
krauss@37790
    41
  "_do_bind"  :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ \<leftarrow>/ _)" 13)
krauss@37790
    42
  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr "\<guillemotright>" 54)
krauss@37790
    43
krauss@37790
    44
translations
krauss@37790
    45
  "_do_block (_do_cons (_do_then t) (_do_final e))"
krauss@37790
    46
    == "CONST bindM_do t (\<lambda>_. e)"
krauss@37790
    47
  "_do_block (_do_cons (_do_bind p t) (_do_final e))"
krauss@37790
    48
    == "CONST bindM_do t (\<lambda>p. e)"
krauss@37790
    49
  "_do_block (_do_cons (_do_let p t) bs)"
krauss@37790
    50
    == "let p = t in _do_block bs"
krauss@37790
    51
  "_do_block (_do_cons b (_do_cons c cs))"
krauss@37790
    52
    == "_do_block (_do_cons b (_do_final (_do_block (_do_cons c cs))))"
krauss@37790
    53
  "_do_cons (_do_let p t) (_do_final s)"
krauss@37790
    54
    == "_do_final (let p = t in s)"
krauss@37790
    55
  "_do_block (_do_final e)" => "e"
krauss@37790
    56
  "(m >> n)" => "(m >>= (\<lambda>_. n))"
krauss@37790
    57
krauss@37790
    58
setup {* Adhoc_Overloading.add_overloaded @{const_name bindM} *}
krauss@37790
    59
krauss@37790
    60
end