src/HOL/HOLCF/Fixrec.thy
author huffman
Sat Nov 27 16:08:10 2010 -0800 (2010-11-27)
changeset 40774 0437dbc127b3
parent 40768 src/HOLCF/Fixrec.thy@50a80cf4b7ef
child 40795 c52cd8bc426d
permissions -rw-r--r--
moved directory src/HOLCF to src/HOL/HOLCF;
added HOLCF theories to src/HOL/IsaMakefile;
     1 (*  Title:      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_defined)
    40 
    41 lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
    42 by (simp add: fail_def Abs_match_defined)
    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 lemmas cont2cont_Rep_match = cont_Rep_match [THEN cont_compose]
    84 
    85 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
    86 unfolding mplus_def
    87 by (simp add: cont2cont_Rep_match Rep_match_strict)
    88 
    89 lemma mplus_fail [simp]: "fail +++ m = m"
    90 unfolding mplus_def fail_def
    91 by (simp add: cont2cont_Rep_match Abs_match_inverse)
    92 
    93 lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
    94 unfolding mplus_def succeed_def
    95 by (simp add: cont2cont_Rep_match cont_Abs_match Abs_match_inverse)
    96 
    97 lemma mplus_fail2 [simp]: "m +++ fail = m"
    98 by (cases m, simp_all)
    99 
   100 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
   101 by (cases x, simp_all)
   102 
   103 subsection {* Match functions for built-in types *}
   104 
   105 default_sort pcpo
   106 
   107 definition
   108   match_bottom :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
   109 where
   110   "match_bottom = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
   111 
   112 definition
   113   match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   114 where
   115   "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
   116 
   117 definition
   118   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   119 where
   120   "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
   121 
   122 definition
   123   match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   124 where
   125   "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
   126 
   127 definition
   128   match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
   129 where
   130   "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
   131 
   132 definition
   133   match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   134 where
   135   "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
   136 
   137 definition
   138   match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
   139 where
   140   "match_ONE = (\<Lambda> ONE k. k)"
   141 
   142 definition
   143   match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   144 where
   145   "match_TT = (\<Lambda> x k. If x then k else fail)"
   146  
   147 definition
   148   match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   149 where
   150   "match_FF = (\<Lambda> x k. If x then fail else k)"
   151 
   152 lemma match_bottom_simps [simp]:
   153   "match_bottom\<cdot>\<bottom>\<cdot>k = \<bottom>"
   154   "x \<noteq> \<bottom> \<Longrightarrow> match_bottom\<cdot>x\<cdot>k = fail"
   155 by (simp_all add: match_bottom_def)
   156 
   157 lemma match_Pair_simps [simp]:
   158   "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
   159 by (simp_all add: match_Pair_def)
   160 
   161 lemma match_spair_simps [simp]:
   162   "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
   163   "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
   164 by (simp_all add: match_spair_def)
   165 
   166 lemma match_sinl_simps [simp]:
   167   "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
   168   "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
   169   "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
   170 by (simp_all add: match_sinl_def)
   171 
   172 lemma match_sinr_simps [simp]:
   173   "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
   174   "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
   175   "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
   176 by (simp_all add: match_sinr_def)
   177 
   178 lemma match_up_simps [simp]:
   179   "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
   180   "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
   181 by (simp_all add: match_up_def)
   182 
   183 lemma match_ONE_simps [simp]:
   184   "match_ONE\<cdot>ONE\<cdot>k = k"
   185   "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
   186 by (simp_all add: match_ONE_def)
   187 
   188 lemma match_TT_simps [simp]:
   189   "match_TT\<cdot>TT\<cdot>k = k"
   190   "match_TT\<cdot>FF\<cdot>k = fail"
   191   "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
   192 by (simp_all add: match_TT_def)
   193 
   194 lemma match_FF_simps [simp]:
   195   "match_FF\<cdot>FF\<cdot>k = k"
   196   "match_FF\<cdot>TT\<cdot>k = fail"
   197   "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
   198 by (simp_all add: match_FF_def)
   199 
   200 subsection {* Mutual recursion *}
   201 
   202 text {*
   203   The following rules are used to prove unfolding theorems from
   204   fixed-point definitions of mutually recursive functions.
   205 *}
   206 
   207 lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
   208 by simp
   209 
   210 lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
   211 by simp
   212 
   213 lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
   214 by simp
   215 
   216 lemma def_cont_fix_eq:
   217   "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
   218 by (simp, subst fix_eq, simp)
   219 
   220 lemma def_cont_fix_ind:
   221   "\<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"
   222 by (simp add: fix_ind)
   223 
   224 text {* lemma for proving rewrite rules *}
   225 
   226 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   227 by simp
   228 
   229 
   230 subsection {* Initializing the fixrec package *}
   231 
   232 use "Tools/holcf_library.ML"
   233 use "Tools/fixrec.ML"
   234 
   235 setup {* Fixrec.setup *}
   236 
   237 setup {*
   238   Fixrec.add_matchers
   239     [ (@{const_name up}, @{const_name match_up}),
   240       (@{const_name sinl}, @{const_name match_sinl}),
   241       (@{const_name sinr}, @{const_name match_sinr}),
   242       (@{const_name spair}, @{const_name match_spair}),
   243       (@{const_name Pair}, @{const_name match_Pair}),
   244       (@{const_name ONE}, @{const_name match_ONE}),
   245       (@{const_name TT}, @{const_name match_TT}),
   246       (@{const_name FF}, @{const_name match_FF}),
   247       (@{const_name UU}, @{const_name match_bottom}) ]
   248 *}
   249 
   250 hide_const (open) succeed fail run
   251 
   252 end