src/HOL/Library/Monad_Syntax.thy
author haftmann
Wed Jul 14 12:27:44 2010 +0200 (2010-07-14)
changeset 37816 e550439d4422
parent 37791 0d6b64060543
child 37818 dd65033fed78
permissions -rw-r--r--
dropped M suffix; added predicate monad bind
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@37791
    11
text {*
krauss@37791
    12
  We provide a convenient do-notation for monadic expressions
krauss@37791
    13
  well-known from Haskell.  @{const Let} is printed
krauss@37791
    14
  specially in do-expressions.
krauss@37791
    15
*}
krauss@37791
    16
krauss@37790
    17
consts
haftmann@37816
    18
  bind :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c" (infixr ">>=" 54)
krauss@37790
    19
krauss@37790
    20
notation (xsymbols)
haftmann@37816
    21
  bind (infixr "\<guillemotright>=" 54)
krauss@37790
    22
krauss@37790
    23
abbreviation (do_notation)
haftmann@37816
    24
  bind_do :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'c"
krauss@37790
    25
where
haftmann@37816
    26
  "bind_do \<equiv> bind"
krauss@37790
    27
krauss@37790
    28
notation (output)
haftmann@37816
    29
  bind_do (infixr ">>=" 54)
krauss@37790
    30
krauss@37790
    31
notation (xsymbols output)
haftmann@37816
    32
  bind_do (infixr "\<guillemotright>=" 54)
krauss@37790
    33
krauss@37790
    34
nonterminals
krauss@37790
    35
  do_binds do_bind
krauss@37790
    36
krauss@37790
    37
syntax
krauss@37790
    38
  "_do_block" :: "do_binds \<Rightarrow> 'a" ("do {//(2  _)//}" [12] 62)
krauss@37790
    39
  "_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ <-/ _)" 13)
krauss@37790
    40
  "_do_let" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2let _ =/ _)" [1000, 13] 13)
krauss@37790
    41
  "_do_then" :: "'a \<Rightarrow> do_bind" ("_" [14] 13)
krauss@37790
    42
  "_do_final" :: "'a \<Rightarrow> do_binds" ("_")
krauss@37790
    43
  "_do_cons" :: "[do_bind, do_binds] \<Rightarrow> do_binds" ("_;//_" [13, 12] 12)
krauss@37790
    44
  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr ">>" 54)
krauss@37790
    45
krauss@37790
    46
syntax (xsymbols)
krauss@37790
    47
  "_do_bind"  :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(_ \<leftarrow>/ _)" 13)
krauss@37790
    48
  "_thenM" :: "['a, 'b] \<Rightarrow> 'b" (infixr "\<guillemotright>" 54)
krauss@37790
    49
krauss@37790
    50
translations
krauss@37790
    51
  "_do_block (_do_cons (_do_then t) (_do_final e))"
haftmann@37816
    52
    == "CONST bind_do t (\<lambda>_. e)"
krauss@37790
    53
  "_do_block (_do_cons (_do_bind p t) (_do_final e))"
haftmann@37816
    54
    == "CONST bind_do t (\<lambda>p. e)"
krauss@37790
    55
  "_do_block (_do_cons (_do_let p t) bs)"
krauss@37790
    56
    == "let p = t in _do_block bs"
krauss@37790
    57
  "_do_block (_do_cons b (_do_cons c cs))"
krauss@37790
    58
    == "_do_block (_do_cons b (_do_final (_do_block (_do_cons c cs))))"
krauss@37790
    59
  "_do_cons (_do_let p t) (_do_final s)"
krauss@37790
    60
    == "_do_final (let p = t in s)"
krauss@37790
    61
  "_do_block (_do_final e)" => "e"
krauss@37790
    62
  "(m >> n)" => "(m >>= (\<lambda>_. n))"
krauss@37790
    63
haftmann@37816
    64
setup {*
haftmann@37816
    65
  Adhoc_Overloading.add_overloaded @{const_name bind}
haftmann@37816
    66
  #> Adhoc_Overloading.add_variant @{const_name bind} @{const_name Predicate.bind}
haftmann@37816
    67
*}
krauss@37790
    68
krauss@37790
    69
end