src/HOL/HOLCF/Fixrec.thy
author wenzelm
Tue Mar 29 17:47:11 2011 +0200 (2011-03-29)
changeset 42151 4da4fc77664b
parent 41429 cf5f025bc3c7
child 46950 d0181abdbdac
permissions -rw-r--r--
tuned headers;
     1 (*  Title:      HOL/HOLCF/Fixrec.thy
     2     Author:     Amber Telfer and Brian Huffman
     3 *)
     4 
     5 header "Package for defining recursive functions in HOLCF"
     6 
     7 theory Fixrec
     8 imports Plain_HOLCF
     9 uses
    10   ("Tools/holcf_library.ML")
    11   ("Tools/fixrec.ML")
    12 begin
    13 
    14 subsection {* Pattern-match monad *}
    15 
    16 default_sort cpo
    17 
    18 pcpodef (open) 'a match = "UNIV::(one ++ 'a u) set"
    19 by simp_all
    20 
    21 definition
    22   fail :: "'a match" where
    23   "fail = Abs_match (sinl\<cdot>ONE)"
    24 
    25 definition
    26   succeed :: "'a \<rightarrow> 'a match" where
    27   "succeed = (\<Lambda> x. Abs_match (sinr\<cdot>(up\<cdot>x)))"
    28 
    29 lemma matchE [case_names bottom fail succeed, cases type: match]:
    30   "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    31 unfolding fail_def succeed_def
    32 apply (cases p, rename_tac r)
    33 apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
    34 apply (rule_tac p=x in oneE, simp, simp)
    35 apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
    36 done
    37 
    38 lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
    39 by (simp add: succeed_def cont_Abs_match Abs_match_bottom_iff)
    40 
    41 lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
    42 by (simp add: fail_def Abs_match_bottom_iff)
    43 
    44 lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
    45 by (simp add: succeed_def cont_Abs_match Abs_match_inject)
    46 
    47 lemma succeed_neq_fail [simp]:
    48   "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
    49 by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
    50 
    51 subsubsection {* Run operator *}
    52 
    53 definition
    54   run :: "'a match \<rightarrow> 'a::pcpo" where
    55   "run = (\<Lambda> m. sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)\<cdot>(Rep_match m))"
    56 
    57 text {* rewrite rules for run *}
    58 
    59 lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
    60 unfolding run_def
    61 by (simp add: cont_Rep_match Rep_match_strict)
    62 
    63 lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
    64 unfolding run_def fail_def
    65 by (simp add: cont_Rep_match Abs_match_inverse)
    66 
    67 lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
    68 unfolding run_def succeed_def
    69 by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
    70 
    71 subsubsection {* Monad plus operator *}
    72 
    73 definition
    74   mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
    75   "mplus = (\<Lambda> m1 m2. sscase\<cdot>(\<Lambda> _. m2)\<cdot>(\<Lambda> _. m1)\<cdot>(Rep_match m1))"
    76 
    77 abbreviation
    78   mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
    79   "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
    80 
    81 text {* rewrite rules for mplus *}
    82 
    83 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
    84 unfolding mplus_def
    85 by (simp add: cont_Rep_match Rep_match_strict)
    86 
    87 lemma mplus_fail [simp]: "fail +++ m = m"
    88 unfolding mplus_def fail_def
    89 by (simp add: cont_Rep_match Abs_match_inverse)
    90 
    91 lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
    92 unfolding mplus_def succeed_def
    93 by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
    94 
    95 lemma mplus_fail2 [simp]: "m +++ fail = m"
    96 by (cases m, simp_all)
    97 
    98 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
    99 by (cases x, simp_all)
   100 
   101 subsection {* Match functions for built-in types *}
   102 
   103 default_sort pcpo
   104 
   105 definition
   106   match_bottom :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
   107 where
   108   "match_bottom = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
   109 
   110 definition
   111   match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   112 where
   113   "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
   114 
   115 definition
   116   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   117 where
   118   "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
   119 
   120 definition
   121   match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   122 where
   123   "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
   124 
   125 definition
   126   match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
   127 where
   128   "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
   129 
   130 definition
   131   match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   132 where
   133   "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
   134 
   135 definition
   136   match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
   137 where
   138   "match_ONE = (\<Lambda> ONE k. k)"
   139 
   140 definition
   141   match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   142 where
   143   "match_TT = (\<Lambda> x k. If x then k else fail)"
   144  
   145 definition
   146   match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   147 where
   148   "match_FF = (\<Lambda> x k. If x then fail else k)"
   149 
   150 lemma match_bottom_simps [simp]:
   151   "match_bottom\<cdot>x\<cdot>k = (if x = \<bottom> then \<bottom> else fail)"
   152 by (simp add: match_bottom_def)
   153 
   154 lemma match_Pair_simps [simp]:
   155   "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
   156 by (simp_all add: match_Pair_def)
   157 
   158 lemma match_spair_simps [simp]:
   159   "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
   160   "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
   161 by (simp_all add: match_spair_def)
   162 
   163 lemma match_sinl_simps [simp]:
   164   "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
   165   "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
   166   "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
   167 by (simp_all add: match_sinl_def)
   168 
   169 lemma match_sinr_simps [simp]:
   170   "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
   171   "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
   172   "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
   173 by (simp_all add: match_sinr_def)
   174 
   175 lemma match_up_simps [simp]:
   176   "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
   177   "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
   178 by (simp_all add: match_up_def)
   179 
   180 lemma match_ONE_simps [simp]:
   181   "match_ONE\<cdot>ONE\<cdot>k = k"
   182   "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
   183 by (simp_all add: match_ONE_def)
   184 
   185 lemma match_TT_simps [simp]:
   186   "match_TT\<cdot>TT\<cdot>k = k"
   187   "match_TT\<cdot>FF\<cdot>k = fail"
   188   "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
   189 by (simp_all add: match_TT_def)
   190 
   191 lemma match_FF_simps [simp]:
   192   "match_FF\<cdot>FF\<cdot>k = k"
   193   "match_FF\<cdot>TT\<cdot>k = fail"
   194   "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
   195 by (simp_all add: match_FF_def)
   196 
   197 subsection {* Mutual recursion *}
   198 
   199 text {*
   200   The following rules are used to prove unfolding theorems from
   201   fixed-point definitions of mutually recursive functions.
   202 *}
   203 
   204 lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
   205 by simp
   206 
   207 lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
   208 by simp
   209 
   210 lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
   211 by simp
   212 
   213 lemma def_cont_fix_eq:
   214   "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
   215 by (simp, subst fix_eq, simp)
   216 
   217 lemma def_cont_fix_ind:
   218   "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F; adm P; P \<bottom>; \<And>x. P x \<Longrightarrow> P (F x)\<rbrakk> \<Longrightarrow> P f"
   219 by (simp add: fix_ind)
   220 
   221 text {* lemma for proving rewrite rules *}
   222 
   223 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   224 by simp
   225 
   226 
   227 subsection {* Initializing the fixrec package *}
   228 
   229 use "Tools/holcf_library.ML"
   230 use "Tools/fixrec.ML"
   231 
   232 setup {* Fixrec.setup *}
   233 
   234 setup {*
   235   Fixrec.add_matchers
   236     [ (@{const_name up}, @{const_name match_up}),
   237       (@{const_name sinl}, @{const_name match_sinl}),
   238       (@{const_name sinr}, @{const_name match_sinr}),
   239       (@{const_name spair}, @{const_name match_spair}),
   240       (@{const_name Pair}, @{const_name match_Pair}),
   241       (@{const_name ONE}, @{const_name match_ONE}),
   242       (@{const_name TT}, @{const_name match_TT}),
   243       (@{const_name FF}, @{const_name match_FF}),
   244       (@{const_name bottom}, @{const_name match_bottom}) ]
   245 *}
   246 
   247 hide_const (open) succeed fail run
   248 
   249 end