make maybe into a real type constructor; remove monad syntax
authorhuffman
Fri Feb 17 01:46:38 2006 +0100 (2006-02-17)
changeset 19092e32cf29f01fc
parent 19091 a6a15d3446ad
child 19093 6d584f9d2021
make maybe into a real type constructor; remove monad syntax
src/HOLCF/Fixrec.thy
src/HOLCF/domain/library.ML
src/HOLCF/domain/syntax.ML
     1.1 --- a/src/HOLCF/Fixrec.thy	Thu Feb 16 23:40:32 2006 +0100
     1.2 +++ b/src/HOLCF/Fixrec.thy	Fri Feb 17 01:46:38 2006 +0100
     1.3 @@ -14,84 +14,81 @@
     1.4  
     1.5  defaultsort cpo
     1.6  
     1.7 -types 'a maybe = "one ++ 'a u"
     1.8 +pcpodef (open) 'a maybe = "UNIV::(one ++ 'a u) set"
     1.9 +by simp
    1.10  
    1.11  constdefs
    1.12    fail :: "'a maybe"
    1.13 -  "fail \<equiv> sinl\<cdot>ONE"
    1.14 +  "fail \<equiv> Abs_maybe (sinl\<cdot>ONE)"
    1.15  
    1.16    return :: "'a \<rightarrow> 'a maybe"
    1.17 -  "return \<equiv> sinr oo up"
    1.18 +  "return \<equiv> \<Lambda> x. Abs_maybe (sinr\<cdot>(up\<cdot>x))"
    1.19 +
    1.20 +  maybe_when :: "'b \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a maybe \<rightarrow> 'b::pcpo"
    1.21 +  "maybe_when \<equiv> \<Lambda> f r m. sscase\<cdot>(\<Lambda> x. f)\<cdot>(fup\<cdot>r)\<cdot>(Rep_maybe m)"
    1.22  
    1.23  lemma maybeE:
    1.24    "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    1.25  apply (unfold fail_def return_def)
    1.26 -apply (rule_tac p=p in ssumE, simp)
    1.27 +apply (cases p, rename_tac r)
    1.28 +apply (rule_tac p=r in ssumE, simp add: Abs_maybe_strict)
    1.29  apply (rule_tac p=x in oneE, simp, simp)
    1.30 -apply (rule_tac p=y in upE, simp, simp)
    1.31 +apply (rule_tac p=y in upE, simp, simp add: cont_Abs_maybe)
    1.32  done
    1.33  
    1.34  lemma return_defined [simp]: "return\<cdot>x \<noteq> \<bottom>"
    1.35 -by (simp add: return_def)
    1.36 +by (simp add: return_def cont_Abs_maybe Abs_maybe_defined)
    1.37  
    1.38  lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
    1.39 -by (simp add: fail_def)
    1.40 +by (simp add: fail_def Abs_maybe_defined)
    1.41  
    1.42  lemma return_eq [simp]: "(return\<cdot>x = return\<cdot>y) = (x = y)"
    1.43 -by (simp add: return_def)
    1.44 +by (simp add: return_def cont_Abs_maybe Abs_maybe_inject)
    1.45  
    1.46  lemma return_neq_fail [simp]:
    1.47    "return\<cdot>x \<noteq> fail" "fail \<noteq> return\<cdot>x"
    1.48 -by (simp_all add: return_def fail_def)
    1.49 +by (simp_all add: return_def fail_def cont_Abs_maybe Abs_maybe_inject)
    1.50 +
    1.51 +lemma maybe_when_rews [simp]:
    1.52 +  "maybe_when\<cdot>f\<cdot>r\<cdot>\<bottom> = \<bottom>"
    1.53 +  "maybe_when\<cdot>f\<cdot>r\<cdot>fail = f"
    1.54 +  "maybe_when\<cdot>f\<cdot>r\<cdot>(return\<cdot>x) = r\<cdot>x"
    1.55 +by (simp_all add: return_def fail_def maybe_when_def cont_Rep_maybe
    1.56 +                  cont_Abs_maybe Abs_maybe_inverse Rep_maybe_strict)
    1.57 +
    1.58 +translations
    1.59 +  "case m of fail \<Rightarrow> t1 | return\<cdot>x \<Rightarrow> t2" == "maybe_when\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
    1.60  
    1.61  
    1.62  subsubsection {* Monadic bind operator *}
    1.63  
    1.64  constdefs
    1.65    bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe"
    1.66 -  "bind \<equiv> \<Lambda> m f. sscase\<cdot>sinl\<cdot>(fup\<cdot>f)\<cdot>m"
    1.67 -
    1.68 -syntax ">>=" :: "['a maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'b maybe" (infixl ">>=" 50)
    1.69 -translations "m >>= f" == "bind\<cdot>m\<cdot>f"
    1.70 -
    1.71 -nonterminals
    1.72 -  maybebind maybebinds
    1.73 -
    1.74 -syntax 
    1.75 -  "_MBIND"  :: "pttrn \<Rightarrow> 'a maybe \<Rightarrow> maybebind"         ("(2_ <-/ _)" 10)
    1.76 -  ""        :: "maybebind \<Rightarrow> maybebinds"                ("_")
    1.77 -
    1.78 -  "_MBINDS" :: "[maybebind, maybebinds] \<Rightarrow> maybebinds"  ("_;/ _")
    1.79 -  "_MDO"    :: "[maybebinds, 'a maybe] \<Rightarrow> 'a maybe"     ("(do _;/ (_))" 10)
    1.80 -
    1.81 -translations
    1.82 -  "_MDO (_MBINDS b bs) e" == "_MDO b (_MDO bs e)"
    1.83 -  "do (x,y) <- m; e" == "m >>= (LAM <x,y>. e)" 
    1.84 -  "do x <- m; e"            == "m >>= (LAM x. e)"
    1.85 +  "bind \<equiv> \<Lambda> m f. case m of fail \<Rightarrow> fail | return\<cdot>x \<Rightarrow> f\<cdot>x"
    1.86  
    1.87  text {* monad laws *}
    1.88  
    1.89 -lemma bind_strict [simp]: "UU >>= f = UU"
    1.90 +lemma bind_strict [simp]: "bind\<cdot>\<bottom>\<cdot>f = \<bottom>"
    1.91 +by (simp add: bind_def)
    1.92 +
    1.93 +lemma bind_fail [simp]: "bind\<cdot>fail\<cdot>f = fail"
    1.94  by (simp add: bind_def)
    1.95  
    1.96 -lemma bind_fail [simp]: "fail >>= f = fail"
    1.97 -by (simp add: bind_def fail_def)
    1.98 +lemma left_unit [simp]: "bind\<cdot>(return\<cdot>a)\<cdot>k = k\<cdot>a"
    1.99 +by (simp add: bind_def)
   1.100  
   1.101 -lemma left_unit [simp]: "(return\<cdot>a) >>= k = k\<cdot>a"
   1.102 -by (simp add: bind_def return_def)
   1.103 -
   1.104 -lemma right_unit [simp]: "m >>= return = m"
   1.105 +lemma right_unit [simp]: "bind\<cdot>m\<cdot>return = m"
   1.106  by (rule_tac p=m in maybeE, simp_all)
   1.107  
   1.108 -lemma bind_assoc [simp]:
   1.109 - "(do b <- (do a <- m; k\<cdot>a); h\<cdot>b) = (do a <- m; b <- k\<cdot>a; h\<cdot>b)"
   1.110 +lemma bind_assoc:
   1.111 + "bind\<cdot>(bind\<cdot>m\<cdot>k)\<cdot>h = bind\<cdot>m\<cdot>(\<Lambda> a. bind\<cdot>(k\<cdot>a)\<cdot>h)"
   1.112  by (rule_tac p=m in maybeE, simp_all)
   1.113  
   1.114  subsubsection {* Run operator *}
   1.115  
   1.116  constdefs
   1.117 -  run:: "'a::pcpo maybe \<rightarrow> 'a"
   1.118 -  "run \<equiv> sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)"
   1.119 +  run:: "'a maybe \<rightarrow> 'a::pcpo"
   1.120 +  "run \<equiv> maybe_when\<cdot>\<bottom>\<cdot>ID"
   1.121  
   1.122  text {* rewrite rules for run *}
   1.123  
   1.124 @@ -99,16 +96,16 @@
   1.125  by (simp add: run_def)
   1.126  
   1.127  lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
   1.128 -by (simp add: run_def fail_def)
   1.129 +by (simp add: run_def)
   1.130  
   1.131  lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
   1.132 -by (simp add: run_def return_def)
   1.133 +by (simp add: run_def)
   1.134  
   1.135  subsubsection {* Monad plus operator *}
   1.136  
   1.137  constdefs
   1.138    mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe"
   1.139 -  "mplus \<equiv> \<Lambda> m1 m2. sscase\<cdot>(\<Lambda> x. m2)\<cdot>(fup\<cdot>return)\<cdot>m1"
   1.140 +  "mplus \<equiv> \<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | return\<cdot>x \<Rightarrow> m1"
   1.141  
   1.142  syntax "+++" :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe" (infixr "+++" 65)
   1.143  translations "m1 +++ m2" == "mplus\<cdot>m1\<cdot>m2"
   1.144 @@ -119,10 +116,10 @@
   1.145  by (simp add: mplus_def)
   1.146  
   1.147  lemma mplus_fail [simp]: "fail +++ m = m"
   1.148 -by (simp add: mplus_def fail_def)
   1.149 +by (simp add: mplus_def)
   1.150  
   1.151  lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
   1.152 -by (simp add: mplus_def return_def)
   1.153 +by (simp add: mplus_def)
   1.154  
   1.155  lemma mplus_fail2 [simp]: "m +++ fail = m"
   1.156  by (rule_tac p=m in maybeE, simp_all)
   1.157 @@ -254,7 +251,8 @@
   1.158  
   1.159  constdefs
   1.160    cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat"
   1.161 -  "cpair_pat p1 p2 \<equiv> \<Lambda>\<langle>x, y\<rangle>. do a <- p1\<cdot>x; b <- p2\<cdot>y; return\<cdot>\<langle>a, b\<rangle>"
   1.162 +  "cpair_pat p1 p2 \<equiv> \<Lambda>\<langle>x, y\<rangle>.
   1.163 +    bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>))"
   1.164  
   1.165    spair_pat ::
   1.166    "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat"
   1.167 @@ -389,7 +387,7 @@
   1.168    "wild_pat \<equiv> \<Lambda> x. return\<cdot>()"
   1.169  
   1.170    as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe"
   1.171 -  "as_pat p \<equiv> \<Lambda> x. do a <- p\<cdot>x; return\<cdot>\<langle>x, a\<rangle>"
   1.172 +  "as_pat p \<equiv> \<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>)"
   1.173  
   1.174    lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)"
   1.175    "lazy_pat p \<equiv> \<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x))"
     2.1 --- a/src/HOLCF/domain/library.ML	Thu Feb 16 23:40:32 2006 +0100
     2.2 +++ b/src/HOLCF/domain/library.ML	Fri Feb 17 01:46:38 2006 +0100
     2.3 @@ -207,6 +207,7 @@
     2.4  |   mk_stuple ts = foldr1 spair ts;
     2.5  fun mk_ctupleT [] = HOLogic.unitT   (* used in match_defs *)
     2.6  |   mk_ctupleT Ts = foldr1 HOLogic.mk_prodT Ts;
     2.7 +fun mk_maybeT T = Type ("Fixrec.maybe",[T]);
     2.8  fun cpair_pat (p1,p2) = %%:cpair_patN $ p1 $ p2;
     2.9  fun lift_defined f = lift (fn x => defined (f x));
    2.10  fun bound_arg vns v = Bound(length vns -find_index_eq v vns -1);
     3.1 --- a/src/HOLCF/domain/syntax.ML	Thu Feb 16 23:40:32 2006 +0100
     3.2 +++ b/src/HOLCF/domain/syntax.ML	Fri Feb 17 01:46:38 2006 +0100
     3.3 @@ -47,13 +47,13 @@
     3.4  			(* strictly speaking, these constants have one argument,
     3.5  			   but the mixfix (without arguments) is introduced only
     3.6  			   to generate parse rules for non-alphanumeric names*)
     3.7 -  fun mat (con ,s,args) = (mat_name_ con, dtype->>mk_ssumT(oneT,mk_uT(mk_ctupleT(map third args))),
     3.8 +  fun mat (con ,s,args) = (mat_name_ con, dtype->>mk_maybeT(mk_ctupleT(map third args)),
     3.9  			   Mixfix(escape ("match_" ^ con), [], Syntax.max_pri));
    3.10    fun sel1 (_,sel,typ)  = Option.map (fn s => (s,dtype ->> typ,NoSyn)) sel;
    3.11    fun sel (_   ,_,args) = List.mapPartial sel1 args;
    3.12    fun freetvar s n      = let val tvar = mk_TFree (s ^ string_of_int n) in
    3.13  			  if tvar mem typevars then freetvar ("t"^s) n else tvar end;
    3.14 -  fun mk_patT (a,b)     = a ->> mk_ssumT (oneT, mk_uT b);
    3.15 +  fun mk_patT (a,b)     = a ->> mk_maybeT b;
    3.16    fun pat_arg_typ n arg = mk_patT (third arg, freetvar "t" n);
    3.17    fun pat (con ,s,args) = (pat_name_ con, (mapn pat_arg_typ 1 args) --->
    3.18  			   mk_patT (dtype, mk_ctupleT (map (freetvar "t") (1 upto length args))),