equal
deleted
inserted
replaced
64 |
64 |
65 |
65 |
66 text {* \medskip The recursion operator -- polymorphic! *} |
66 text {* \medskip The recursion operator -- polymorphic! *} |
67 |
67 |
68 definition |
68 definition |
69 rec :: "'a \<Rightarrow> ('n \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'n \<Rightarrow> 'a" |
69 rec :: "'a \<Rightarrow> ('n \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'n \<Rightarrow> 'a" where |
70 "rec e r x = (THE y. Rec e r x y)" |
70 "rec e r x = (THE y. Rec e r x y)" |
71 |
71 |
72 lemma rec_eval: |
72 lemma rec_eval: |
73 assumes Rec: "Rec e r x y" |
73 assumes Rec: "Rec e r x y" |
74 shows "rec e r x = y" |
74 shows "rec e r x = y" |
90 |
90 |
91 |
91 |
92 text {* \medskip Example: addition (monomorphic) *} |
92 text {* \medskip Example: addition (monomorphic) *} |
93 |
93 |
94 definition |
94 definition |
95 add :: "'n \<Rightarrow> 'n \<Rightarrow> 'n" |
95 add :: "'n \<Rightarrow> 'n \<Rightarrow> 'n" where |
96 "add m n = rec n (\<lambda>_ k. succ k) m" |
96 "add m n = rec n (\<lambda>_ k. succ k) m" |
97 |
97 |
98 lemma add_zero [simp]: "add zero n = n" |
98 lemma add_zero [simp]: "add zero n = n" |
99 and add_succ [simp]: "add (succ m) n = succ (add m n)" |
99 and add_succ [simp]: "add (succ m) n = succ (add m n)" |
100 unfolding add_def by simp_all |
100 unfolding add_def by simp_all |
114 |
114 |
115 |
115 |
116 text {* \medskip Example: replication (polymorphic) *} |
116 text {* \medskip Example: replication (polymorphic) *} |
117 |
117 |
118 definition |
118 definition |
119 repl :: "'n \<Rightarrow> 'a \<Rightarrow> 'a list" |
119 repl :: "'n \<Rightarrow> 'a \<Rightarrow> 'a list" where |
120 "repl n x = rec [] (\<lambda>_ xs. x # xs) n" |
120 "repl n x = rec [] (\<lambda>_ xs. x # xs) n" |
121 |
121 |
122 lemma repl_zero [simp]: "repl zero x = []" |
122 lemma repl_zero [simp]: "repl zero x = []" |
123 and repl_succ [simp]: "repl (succ n) x = x # repl n x" |
123 and repl_succ [simp]: "repl (succ n) x = x # repl n x" |
124 unfolding repl_def by simp_all |
124 unfolding repl_def by simp_all |