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