src/HOL/Library/Monad_Syntax.thy
author wenzelm
Mon Dec 28 21:47:32 2015 +0100 (2015-12-28)
changeset 61955 e96292f32c3c
parent 60500 903bb1495239
child 62026 ea3b1b0413b4
permissions -rw-r--r--
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
     1 (* Author: Alexander Krauss, TU Muenchen
     2    Author: Christian Sternagel, University of Innsbruck
     3 *)
     4 
     5 section \<open>Monad notation for arbitrary types\<close>
     6 
     7 theory Monad_Syntax
     8 imports Main "~~/src/Tools/Adhoc_Overloading"
     9 begin
    10 
    11 text \<open>
    12   We provide a convenient do-notation for monadic expressions
    13   well-known from Haskell.  @{const Let} is printed
    14   specially in do-expressions.
    15 \<close>
    16 
    17 consts
    18   bind :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'd" (infixr "\<guillemotright>=" 54)
    19 
    20 notation (ASCII)
    21   bind (infixr ">>=" 54)
    22 
    23 notation (latex output)
    24   bind (infixr "\<bind>" 54)
    25 
    26 
    27 abbreviation (do_notation)
    28   bind_do :: "['a, 'b \<Rightarrow> 'c] \<Rightarrow> 'd"
    29   where "bind_do \<equiv> bind"
    30 
    31 notation (output)
    32   bind_do (infixr "\<guillemotright>=" 54)
    33 
    34 notation (ASCII output)
    35   bind_do (infixr ">>=" 54)
    36 
    37 notation (latex output)
    38   bind_do (infixr "\<bind>" 54)
    39 
    40 
    41 nonterminal do_binds and do_bind
    42 syntax
    43   "_do_block" :: "do_binds \<Rightarrow> 'a" ("do {//(2  _)//}" [12] 62)
    44   "_do_bind"  :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2_ \<leftarrow>/ _)" 13)
    45   "_do_let" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2let _ =/ _)" [1000, 13] 13)
    46   "_do_then" :: "'a \<Rightarrow> do_bind" ("_" [14] 13)
    47   "_do_final" :: "'a \<Rightarrow> do_binds" ("_")
    48   "_do_cons" :: "[do_bind, do_binds] \<Rightarrow> do_binds" ("_;//_" [13, 12] 12)
    49   "_thenM" :: "['a, 'b] \<Rightarrow> 'c" (infixr "\<guillemotright>" 54)
    50 
    51 syntax (ASCII)
    52   "_do_bind" :: "[pttrn, 'a] \<Rightarrow> do_bind" ("(2_ <-/ _)" 13)
    53   "_thenM" :: "['a, 'b] \<Rightarrow> 'c" (infixr ">>" 54)
    54 
    55 syntax (latex output)
    56   "_thenM" :: "['a, 'b] \<Rightarrow> 'c" (infixr "\<then>" 54)
    57 
    58 translations
    59   "_do_block (_do_cons (_do_then t) (_do_final e))"
    60     == "CONST bind_do t (\<lambda>_. e)"
    61   "_do_block (_do_cons (_do_bind p t) (_do_final e))"
    62     == "CONST bind_do t (\<lambda>p. e)"
    63   "_do_block (_do_cons (_do_let p t) bs)"
    64     == "let p = t in _do_block bs"
    65   "_do_block (_do_cons b (_do_cons c cs))"
    66     == "_do_block (_do_cons b (_do_final (_do_block (_do_cons c cs))))"
    67   "_do_cons (_do_let p t) (_do_final s)"
    68     == "_do_final (let p = t in s)"
    69   "_do_block (_do_final e)" => "e"
    70   "(m >> n)" => "(m >>= (\<lambda>_. n))"
    71 
    72 adhoc_overloading
    73   bind Set.bind Predicate.bind Option.bind List.bind
    74 
    75 end