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