
wenzelm@11024: (*  Title:      HOL/ex/Primrec.thy
paulson@3335:     ID:         $Id$
paulson@3335:     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@3335:     Copyright   1997  University of Cambridge
paulson@3335: 
wenzelm@11024: Primitive Recursive Functions.  Demonstrates recursive definitions,
wenzelm@11024: the TFL package.
paulson@3335: *)
paulson@3335: 
wenzelm@11024: header {* Primitive Recursive Functions *}
wenzelm@11024: 
haftmann@16417: theory Primrec imports Main begin
wenzelm@11024: 
wenzelm@11024: text {*
wenzelm@11024:   Proof adopted from
wenzelm@11024: 
wenzelm@11024:   Nora Szasz, A Machine Checked Proof that Ackermann's Function is not
wenzelm@11024:   Primitive Recursive, In: Huet \& Plotkin, eds., Logical Environments
wenzelm@11024:   (CUP, 1993), 317-338.
wenzelm@11024: 
wenzelm@11024:   See also E. Mendelson, Introduction to Mathematical Logic.  (Van
wenzelm@11024:   Nostrand, 1964), page 250, exercise 11.
wenzelm@11024:   \medskip
wenzelm@11024: *}
wenzelm@11024: 
wenzelm@11024: consts ack :: "nat * nat => nat"
wenzelm@11024: recdef ack  "less_than <*lex*> less_than"
wenzelm@11024:   "ack (0, n) =  Suc n"
wenzelm@11024:   "ack (Suc m, 0) = ack (m, 1)"
wenzelm@11024:   "ack (Suc m, Suc n) = ack (m, ack (Suc m, n))"
wenzelm@11024: 
wenzelm@11024: consts list_add :: "nat list => nat"
wenzelm@11024: primrec
wenzelm@11024:   "list_add [] = 0"
wenzelm@11024:   "list_add (m # ms) = m + list_add ms"
wenzelm@11024: 
wenzelm@11024: consts zeroHd :: "nat list => nat"
wenzelm@11024: primrec
wenzelm@11024:   "zeroHd [] = 0"
wenzelm@11024:   "zeroHd (m # ms) = m"
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* The set of primitive recursive functions of type @{typ "nat list => nat"}. *}
wenzelm@11024: 
wenzelm@19736: definition
wenzelm@21404:   SC :: "nat list => nat" where
wenzelm@19736:   "SC l = Suc (zeroHd l)"
paulson@3335: 
wenzelm@21404: definition
wenzelm@21404:   CONSTANT :: "nat => nat list => nat" where
wenzelm@19736:   "CONSTANT k l = k"
wenzelm@11024: 
wenzelm@21404: definition
wenzelm@21404:   PROJ :: "nat => nat list => nat" where
wenzelm@19736:   "PROJ i l = zeroHd (drop i l)"
wenzelm@11024: 
wenzelm@21404: definition
wenzelm@21404:   COMP :: "(nat list => nat) => (nat list => nat) list => nat list => nat" where
wenzelm@19736:   "COMP g fs l = g (map (\<lambda>f. f l) fs)"
wenzelm@11024: 
wenzelm@21404: definition
wenzelm@21404:   PREC :: "(nat list => nat) => (nat list => nat) => nat list => nat" where
wenzelm@19736:   "PREC f g l =
wenzelm@19736:     (case l of
wenzelm@11024:       [] => 0
wenzelm@19736:     | x # l' => nat_rec (f l') (\<lambda>y r. g (r # y # l')) x)"
wenzelm@11024:   -- {* Note that @{term g} is applied first to @{term "PREC f g y"} and then to @{term y}! *}
wenzelm@11024: 
berghofe@23776: inductive PRIMREC :: "(nat list => nat) => bool"
berghofe@22283:   where
berghofe@22283:     SC: "PRIMREC SC"
berghofe@22283:   | CONSTANT: "PRIMREC (CONSTANT k)"
berghofe@22283:   | PROJ: "PRIMREC (PROJ i)"
nipkow@22944:   | COMP: "PRIMREC g ==> \<forall>f \<in> set fs. PRIMREC f ==> PRIMREC (COMP g fs)"
berghofe@22283:   | PREC: "PRIMREC f ==> PRIMREC g ==> PRIMREC (PREC f g)"
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* Useful special cases of evaluation *}
wenzelm@11024: 
wenzelm@11024: lemma SC [simp]: "SC (x # l) = Suc x"
wenzelm@11024:   apply (simp add: SC_def)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@19676: lemma CONSTANT [simp]: "CONSTANT k l = k"
wenzelm@19676:   apply (simp add: CONSTANT_def)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma PROJ_0 [simp]: "PROJ 0 (x # l) = x"
wenzelm@11024:   apply (simp add: PROJ_def)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma COMP_1 [simp]: "COMP g [f] l = g [f l]"
wenzelm@11024:   apply (simp add: COMP_def)
wenzelm@11024:   done
paulson@3335: 
wenzelm@11024: lemma PREC_0 [simp]: "PREC f g (0 # l) = f l"
wenzelm@11024:   apply (simp add: PREC_def)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma PREC_Suc [simp]: "PREC f g (Suc x # l) = g (PREC f g (x # l) # x # l)"
wenzelm@11024:   apply (simp add: PREC_def)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 4 *}
wenzelm@11024: 
wenzelm@11024: lemma less_ack2 [iff]: "j < ack (i, j)"
wenzelm@11024:   apply (induct i j rule: ack.induct)
wenzelm@11024:     apply simp_all
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 5-, the single-step lemma *}
wenzelm@11024: 
wenzelm@11024: lemma ack_less_ack_Suc2 [iff]: "ack(i, j) < ack (i, Suc j)"
wenzelm@11024:   apply (induct i j rule: ack.induct)
wenzelm@11024:     apply simp_all
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 5, monotonicity for @{text "<"} *}
wenzelm@11024: 
wenzelm@11024: lemma ack_less_mono2: "j < k ==> ack (i, j) < ack (i, k)"
wenzelm@11024:   apply (induct i k rule: ack.induct)
wenzelm@11024:     apply simp_all
wenzelm@11024:   apply (blast elim!: less_SucE intro: less_trans)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 5', monotonicity for @{text \<le>} *}
wenzelm@11024: 
wenzelm@11024: lemma ack_le_mono2: "j \<le> k ==> ack (i, j) \<le> ack (i, k)"
wenzelm@11024:   apply (simp add: order_le_less)
wenzelm@11024:   apply (blast intro: ack_less_mono2)
wenzelm@11024:   done
paulson@3335: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 6 *}
wenzelm@11024: 
wenzelm@11024: lemma ack2_le_ack1 [iff]: "ack (i, Suc j) \<le> ack (Suc i, j)"
haftmann@26072: proof (induct j)
haftmann@26072:   case 0 show ?case by simp
haftmann@26072: next
haftmann@26072:   case (Suc j) show ?case 
haftmann@26072:     by (auto intro!: ack_le_mono2)
haftmann@26072:       (metis Suc Suc_leI Suc_lessI less_ack2 linorder_not_less)
haftmann@26072: qed
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 7-, the single-step lemma *}
wenzelm@11024: 
wenzelm@11024: lemma ack_less_ack_Suc1 [iff]: "ack (i, j) < ack (Suc i, j)"
wenzelm@11024:   apply (blast intro: ack_less_mono2 less_le_trans)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@19676: text {* PROPERTY A 4'? Extra lemma needed for @{term CONSTANT} case, constant functions *}
wenzelm@11024: 
wenzelm@11024: lemma less_ack1 [iff]: "i < ack (i, j)"
wenzelm@11024:   apply (induct i)
wenzelm@11024:    apply simp_all
wenzelm@11024:   apply (blast intro: Suc_leI le_less_trans)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 8 *}
wenzelm@11024: 
wenzelm@11704: lemma ack_1 [simp]: "ack (Suc 0, j) = j + 2"
wenzelm@11024:   apply (induct j)
wenzelm@11024:    apply simp_all
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11701: text {* PROPERTY A 9.  The unary @{text 1} and @{text 2} in @{term
wenzelm@11024:   ack} is essential for the rewriting. *}
wenzelm@11024: 
wenzelm@11704: lemma ack_2 [simp]: "ack (Suc (Suc 0), j) = 2 * j + 3"
wenzelm@11024:   apply (induct j)
wenzelm@11024:    apply simp_all
wenzelm@11024:   done
paulson@3335: 
paulson@3335: 
wenzelm@11024: text {* PROPERTY A 7, monotonicity for @{text "<"} [not clear why
wenzelm@11024:   @{thm [source] ack_1} is now needed first!] *}
wenzelm@11024: 
wenzelm@11024: lemma ack_less_mono1_aux: "ack (i, k) < ack (Suc (i +i'), k)"
wenzelm@11024:   apply (induct i k rule: ack.induct)
wenzelm@11024:     apply simp_all
wenzelm@11024:    prefer 2
wenzelm@11024:    apply (blast intro: less_trans ack_less_mono2)
wenzelm@11024:   apply (induct_tac i' n rule: ack.induct)
wenzelm@11024:     apply simp_all
wenzelm@11024:   apply (blast intro: Suc_leI [THEN le_less_trans] ack_less_mono2)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma ack_less_mono1: "i < j ==> ack (i, k) < ack (j, k)"
wenzelm@11024:   apply (drule less_imp_Suc_add)
wenzelm@11024:   apply (blast intro!: ack_less_mono1_aux)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 7', monotonicity for @{text "\<le>"} *}
wenzelm@11024: 
wenzelm@11024: lemma ack_le_mono1: "i \<le> j ==> ack (i, k) \<le> ack (j, k)"
wenzelm@11024:   apply (simp add: order_le_less)
wenzelm@11024:   apply (blast intro: ack_less_mono1)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 10 *}
wenzelm@11024: 
wenzelm@11704: lemma ack_nest_bound: "ack(i1, ack (i2, j)) < ack (2 + (i1 + i2), j)"
wenzelm@11024:   apply (simp add: numerals)
wenzelm@11024:   apply (rule ack2_le_ack1 [THEN [2] less_le_trans])
wenzelm@11024:   apply simp
wenzelm@11024:   apply (rule le_add1 [THEN ack_le_mono1, THEN le_less_trans])
wenzelm@11024:   apply (rule ack_less_mono1 [THEN ack_less_mono2])
wenzelm@11024:   apply (simp add: le_imp_less_Suc le_add2)
wenzelm@11024:   done
wenzelm@11024: 
paulson@3335: 
wenzelm@11024: text {* PROPERTY A 11 *}
paulson@3335: 
wenzelm@11704: lemma ack_add_bound: "ack (i1, j) + ack (i2, j) < ack (4 + (i1 + i2), j)"
paulson@24742:   apply (rule less_trans [of _ "ack (Suc (Suc 0), ack (i1 + i2, j))" _])
wenzelm@11024:    prefer 2
wenzelm@11024:    apply (rule ack_nest_bound [THEN less_le_trans])
wenzelm@11024:    apply (simp add: Suc3_eq_add_3)
wenzelm@11024:   apply simp
wenzelm@11024:   apply (cut_tac i = i1 and m1 = i2 and k = j in le_add1 [THEN ack_le_mono1])
wenzelm@11024:   apply (cut_tac i = "i2" and m1 = i1 and k = j in le_add2 [THEN ack_le_mono1])
wenzelm@11024:   apply auto
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* PROPERTY A 12.  Article uses existential quantifier but the ALF proof
wenzelm@11024:   used @{text "k + 4"}.  Quantified version must be nested @{text
wenzelm@11024:   "\<exists>k'. \<forall>i j. ..."} *}
paulson@3335: 
wenzelm@11704: lemma ack_add_bound2: "i < ack (k, j) ==> i + j < ack (4 + k, j)"
paulson@24742:   apply (rule less_trans [of _ "ack (k, j) + ack (0, j)" _])
paulson@24742:    apply (blast intro: add_less_mono less_ack2) 
wenzelm@11024:    apply (rule ack_add_bound [THEN less_le_trans])
wenzelm@11024:    apply simp
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* Inductive definition of the @{term PR} functions *}
paulson@3335: 
wenzelm@11024: text {* MAIN RESULT *}
wenzelm@11024: 
wenzelm@11024: lemma SC_case: "SC l < ack (1, list_add l)"
wenzelm@11024:   apply (unfold SC_def)
wenzelm@11024:   apply (induct l)
wenzelm@11024:   apply (simp_all add: le_add1 le_imp_less_Suc)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@19676: lemma CONSTANT_case: "CONSTANT k l < ack (k, list_add l)"
paulson@24551:   by simp
paulson@3335: 
wenzelm@11024: lemma PROJ_case [rule_format]: "\<forall>i. PROJ i l < ack (0, list_add l)"
wenzelm@11024:   apply (simp add: PROJ_def)
wenzelm@11024:   apply (induct l)
paulson@24551:    apply (auto simp add: drop_Cons split: nat.split) 
paulson@24551:   apply (blast intro: less_le_trans le_add2)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* @{term COMP} case *}
paulson@3335: 
nipkow@22944: lemma COMP_map_aux: "\<forall>f \<in> set fs. PRIMREC f \<and> (\<exists>kf. \<forall>l. f l < ack (kf, list_add l))
wenzelm@11024:   ==> \<exists>k. \<forall>l. list_add (map (\<lambda>f. f l) fs) < ack (k, list_add l)"
nipkow@22944:   apply (induct fs)
paulson@24551:   apply (rule_tac x = 0 in exI) 
wenzelm@11024:    apply simp
wenzelm@11024:   apply simp
wenzelm@11024:   apply (blast intro: add_less_mono ack_add_bound less_trans)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma COMP_case:
wenzelm@11024:   "\<forall>l. g l < ack (kg, list_add l) ==>
nipkow@22944:   \<forall>f \<in> set fs. PRIMREC f \<and> (\<exists>kf. \<forall>l. f l < ack(kf, list_add l))
wenzelm@11024:   ==> \<exists>k. \<forall>l. COMP g fs  l < ack(k, list_add l)"
wenzelm@11024:   apply (unfold COMP_def)
paulson@16588:     --{*Now, if meson tolerated map, we could finish with
wenzelm@16731:   @{text "(drule COMP_map_aux, meson ack_less_mono2 ack_nest_bound less_trans)"} *}
paulson@16588:   apply (erule COMP_map_aux [THEN exE])
paulson@16588:   apply (rule exI)
paulson@16588:   apply (rule allI)
paulson@16588:   apply (drule spec)+
paulson@16588:   apply (erule less_trans)
paulson@16588:   apply (blast intro: ack_less_mono2 ack_nest_bound less_trans)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: 
wenzelm@11024: text {* @{term PREC} case *}
paulson@3335: 
wenzelm@11024: lemma PREC_case_aux:
wenzelm@11024:   "\<forall>l. f l + list_add l < ack (kf, list_add l) ==>
wenzelm@11024:     \<forall>l. g l + list_add l < ack (kg, list_add l) ==>
wenzelm@11024:     PREC f g l + list_add l < ack (Suc (kf + kg), list_add l)"
wenzelm@11024:   apply (unfold PREC_def)
wenzelm@11024:   apply (case_tac l)
wenzelm@11024:    apply simp_all
wenzelm@11024:    apply (blast intro: less_trans)
wenzelm@11024:   apply (erule ssubst) -- {* get rid of the needless assumption *}
wenzelm@11024:   apply (induct_tac a)
wenzelm@11024:    apply simp_all
wenzelm@11024:    txt {* base case *}
wenzelm@11024:    apply (blast intro: le_add1 [THEN le_imp_less_Suc, THEN ack_less_mono1] less_trans)
wenzelm@11024:   txt {* induction step *}
wenzelm@11024:   apply (rule Suc_leI [THEN le_less_trans])
wenzelm@11024:    apply (rule le_refl [THEN add_le_mono, THEN le_less_trans])
wenzelm@11024:     prefer 2
wenzelm@11024:     apply (erule spec)
wenzelm@11024:    apply (simp add: le_add2)
wenzelm@11024:   txt {* final part of the simplification *}
wenzelm@11024:   apply simp
wenzelm@11024:   apply (rule le_add2 [THEN ack_le_mono1, THEN le_less_trans])
wenzelm@11024:   apply (erule ack_less_mono2)
wenzelm@11024:   done
wenzelm@11024: 
wenzelm@11024: lemma PREC_case:
wenzelm@11024:   "\<forall>l. f l < ack (kf, list_add l) ==>
wenzelm@11024:     \<forall>l. g l < ack (kg, list_add l) ==>
wenzelm@11024:     \<exists>k. \<forall>l. PREC f g l < ack (k, list_add l)"
paulson@24551:   by (metis le_less_trans [OF le_add1 PREC_case_aux] ack_add_bound2) 
wenzelm@11024: 
berghofe@22283: lemma ack_bounds_PRIMREC: "PRIMREC f ==> \<exists>k. \<forall>l. f l < ack (k, list_add l)"
wenzelm@11024:   apply (erule PRIMREC.induct)
wenzelm@19676:       apply (blast intro: SC_case CONSTANT_case PROJ_case COMP_case PREC_case)+
wenzelm@11024:   done
wenzelm@11024: 
berghofe@22283: lemma ack_not_PRIMREC: "\<not> PRIMREC (\<lambda>l. case l of [] => 0 | x # l' => ack (x, x))"
wenzelm@11024:   apply (rule notI)
wenzelm@11024:   apply (erule ack_bounds_PRIMREC [THEN exE])
paulson@24742:   apply (rule Nat.less_irrefl)
wenzelm@11024:   apply (drule_tac x = "[x]" in spec)
wenzelm@11024:   apply simp
wenzelm@11024:   done
paulson@3335: 
paulson@3335: end




