author | paulson |
Tue, 13 Nov 2001 16:12:25 +0100 | |
changeset 12169 | d4ed9802082a |
parent 11481 | c77e5401f2ff |
child 13079 | e7738aa7267f |
permissions | -rw-r--r-- |
12169 | 1 |
(* *********************************************************************** *) |
2 |
(* *) |
|
3 |
(* Title: SList.thy (Extended List Theory) *) |
|
4 |
(* Based on: $Id$ *) |
|
5 |
(* Author: Lawrence C Paulson, Cambridge University Computer Laboratory*) |
|
6 |
(* Author: B. Wolff, University of Bremen *) |
|
7 |
(* Purpose: Enriched theory of lists *) |
|
8 |
(* mutual indirect recursive data-types *) |
|
9 |
(* *) |
|
10 |
(* *********************************************************************** *) |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
11 |
|
12169 | 12 |
(* Definition of type 'a list (strict lists) by a least fixed point |
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
13 |
|
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
14 |
We use list(A) == lfp(%Z. {NUMB(0)} <+> A <*> Z) |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
15 |
and not list == lfp(%Z. {NUMB(0)} <+> range(Leaf) <*> Z) |
12169 | 16 |
|
17 |
so that list can serve as a "functor" for defining other recursive types. |
|
18 |
||
19 |
This enables the conservative construction of mutual recursive data-types |
|
20 |
such as |
|
21 |
||
22 |
datatype 'a m = Node 'a * ('a m) list |
|
23 |
||
24 |
Tidied by lcp. Still needs removal of nat_rec. |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
25 |
*) |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
26 |
|
12169 | 27 |
SList = NatArith + Sexp + Hilbert_Choice (*gives us "inv"*) + |
28 |
(* *********************************************************************** *) |
|
29 |
(* *) |
|
30 |
(* Building up data type *) |
|
31 |
(* *) |
|
32 |
(* *********************************************************************** *) |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
33 |
|
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
34 |
consts |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
35 |
|
12169 | 36 |
list :: "'a item set => 'a item set" |
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
37 |
|
12169 | 38 |
NIL :: "'a item" |
39 |
CONS :: "['a item, 'a item] => 'a item" |
|
40 |
List_case :: "['b, ['a item, 'a item]=>'b, 'a item] => 'b" |
|
41 |
List_rec :: "['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
42 |
|
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
43 |
defs |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
44 |
(* Defining the Concrete Constructors *) |
12169 | 45 |
NIL_def "NIL == In0(Numb(0))" |
46 |
CONS_def "CONS M N == In1(Scons M N)" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
47 |
|
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
48 |
inductive "list(A)" |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
49 |
intrs |
12169 | 50 |
NIL_I "NIL: list A" |
51 |
CONS_I "[| a: A; M: list A |] ==> CONS a M : list A" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
52 |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
53 |
|
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
54 |
typedef (List) |
6382 | 55 |
'a list = "list(range Leaf) :: 'a item set" (list.NIL_I) |
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
56 |
|
12169 | 57 |
defs |
58 |
List_case_def "List_case c d == Case(%x. c)(Split(d))" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
59 |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
60 |
List_rec_def |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
61 |
"List_rec M c d == wfrec (trancl pred_sexp) |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
62 |
(%g. List_case c (%x y. d x y (g y))) M" |
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
63 |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
64 |
|
12169 | 65 |
(* *********************************************************************** *) |
66 |
(* *) |
|
67 |
(* Abstracting data type *) |
|
68 |
(* *) |
|
69 |
(* *********************************************************************** *) |
|
70 |
||
71 |
(*Declaring the abstract list constructors*) |
|
72 |
consts |
|
73 |
Nil :: "'a list" |
|
74 |
"#" :: "['a, 'a list] => 'a list" (infixr 65) |
|
75 |
list_case :: "['b, ['a, 'a list]=>'b, 'a list] => 'b" |
|
76 |
list_rec :: "['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b" |
|
77 |
||
78 |
||
79 |
(* list Enumeration *) |
|
80 |
||
81 |
"[]" :: "'a list" ("[]") |
|
82 |
"@list" :: "args => 'a list" ("[(_)]") |
|
83 |
||
84 |
translations |
|
85 |
"[x, xs]" == "x#[xs]" |
|
86 |
"[x]" == "x#[]" |
|
87 |
"[]" == "Nil" |
|
88 |
||
89 |
"case xs of Nil => a | y#ys => b" == "list_case(a, %y ys. b, xs)" |
|
90 |
||
91 |
||
92 |
||
93 |
defs |
|
94 |
(* Defining the Abstract Constructors *) |
|
95 |
Nil_def "Nil == Abs_List(NIL)" |
|
96 |
Cons_def "x#xs == Abs_List(CONS (Leaf x)(Rep_List xs))" |
|
97 |
||
98 |
list_case_def "list_case a f xs == list_rec xs a (%x xs r. f x xs)" |
|
99 |
||
100 |
(* list Recursion -- the trancl is Essential; see list.ML *) |
|
101 |
||
102 |
list_rec_def |
|
103 |
"list_rec l c d == \ |
|
104 |
\ List_rec(Rep_List l) c (%x y r. d(inv Leaf x)(Abs_List y) r)" |
|
105 |
||
106 |
||
107 |
||
108 |
(* *********************************************************************** *) |
|
109 |
(* *) |
|
110 |
(* Generalized Map Functionals *) |
|
111 |
(* *) |
|
112 |
(* *********************************************************************** *) |
|
113 |
||
114 |
||
115 |
(* Generalized Map Functionals *) |
|
116 |
||
117 |
consts |
|
118 |
Rep_map :: "('b => 'a item) => ('b list => 'a item)" |
|
119 |
Abs_map :: "('a item => 'b) => 'a item => 'b list" |
|
120 |
||
121 |
defs |
|
122 |
Rep_map_def "Rep_map f xs == list_rec xs NIL(%x l r. CONS(f x) r)" |
|
123 |
Abs_map_def "Abs_map g M == List_rec M Nil (%N L r. g(N)#r)" |
|
124 |
||
125 |
||
126 |
(**** Function definitions ****) |
|
127 |
||
128 |
constdefs |
|
129 |
||
130 |
null :: "'a list => bool" |
|
131 |
"null xs == list_rec xs True (%x xs r. False)" |
|
132 |
||
133 |
hd :: "'a list => 'a" |
|
134 |
"hd xs == list_rec xs (@x. True) (%x xs r. x)" |
|
135 |
||
136 |
tl :: "'a list => 'a list" |
|
137 |
"tl xs == list_rec xs (@xs. True) (%x xs r. xs)" |
|
138 |
||
139 |
(* a total version of tl: *) |
|
140 |
ttl :: "'a list => 'a list" |
|
141 |
"ttl xs == list_rec xs [] (%x xs r. xs)" |
|
142 |
||
143 |
mem :: "['a, 'a list] => bool" (infixl 55) |
|
144 |
"x mem xs == list_rec xs False (%y ys r. if y=x then True else r)" |
|
145 |
||
146 |
list_all :: "('a => bool) => ('a list => bool)" |
|
147 |
"list_all P xs == list_rec xs True(%x l r. P(x) & r)" |
|
148 |
||
149 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
150 |
"map f xs == list_rec xs [] (%x l r. f(x)#r)" |
|
151 |
||
152 |
||
153 |
consts |
|
154 |
"@" :: ['a list, 'a list] => 'a list (infixr 65) |
|
155 |
defs |
|
156 |
append_def"xs@ys == list_rec xs ys (%x l r. x#r)" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
157 |
|
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
158 |
|
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
159 |
constdefs |
12169 | 160 |
filter :: "['a => bool, 'a list] => 'a list" |
161 |
"filter P xs == list_rec xs [] (%x xs r. if P(x)then x#r else r)" |
|
162 |
||
163 |
foldl :: "[['b,'a] => 'b, 'b, 'a list] => 'b" |
|
164 |
"foldl f a xs == list_rec xs (%a. a)(%x xs r.%a. r(f a x))(a)" |
|
165 |
||
166 |
foldr :: "[['a,'b] => 'b, 'b, 'a list] => 'b" |
|
167 |
"foldr f a xs == list_rec xs a (%x xs r. (f x r))" |
|
168 |
||
169 |
length :: "'a list => nat" |
|
170 |
"length xs== list_rec xs 0 (%x xs r. Suc r)" |
|
171 |
||
172 |
drop :: "['a list,nat] => 'a list" |
|
173 |
"drop t n == (nat_rec(%x. x)(%m r xs. r(ttl xs)))(n)(t)" |
|
174 |
||
175 |
copy :: "['a, nat] => 'a list" (* make list of n copies of x *) |
|
176 |
"copy t == nat_rec [] (%m xs. t # xs)" |
|
177 |
||
178 |
flat :: "'a list list => 'a list" |
|
179 |
"flat == foldr (op @) []" |
|
180 |
||
181 |
nth :: "[nat, 'a list] => 'a" |
|
182 |
"nth == nat_rec hd (%m r xs. r(tl xs))" |
|
183 |
||
184 |
rev :: "'a list => 'a list" |
|
185 |
"rev xs == list_rec xs [] (%x xs xsa. xsa @ [x])" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
186 |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
187 |
|
12169 | 188 |
(* miscellaneous definitions *) |
189 |
zip :: "'a list * 'b list => ('a*'b) list" |
|
190 |
"zip == zipWith (%s. s)" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
191 |
|
12169 | 192 |
zipWith :: "['a * 'b => 'c, 'a list * 'b list] => 'c list" |
193 |
"zipWith f S == (list_rec (fst S) (%T.[]) |
|
194 |
(%x xs r. %T. if null T then [] |
|
195 |
else f(x,hd T) # r(tl T)))(snd(S))" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
196 |
|
12169 | 197 |
unzip :: "('a*'b) list => ('a list * 'b list)" |
198 |
"unzip == foldr(% (a,b)(c,d).(a#c,b#d))([],[])" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
199 |
|
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
200 |
|
12169 | 201 |
consts take :: "['a list,nat] => 'a list" |
202 |
primrec |
|
203 |
take_0 "take xs 0 = []" |
|
204 |
take_Suc "take xs (Suc n) = list_case [] (%x l. x # take l n) xs" |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
205 |
|
12169 | 206 |
consts enum :: "[nat,nat] => nat list" |
207 |
primrec |
|
208 |
enum_0 "enum i 0 = []" |
|
209 |
enum_Suc "enum i (Suc j) = (if i <= j then enum i j @ [j] else [])" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
210 |
|
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
211 |
|
12169 | 212 |
syntax |
213 |
(* Special syntax for list_all and filter *) |
|
214 |
"@Alls" :: "[idt, 'a list, bool] => bool" ("(2Alls _:_./ _)" 10) |
|
215 |
"@filter" :: "[idt, 'a list, bool] => 'a list" ("(1[_:_ ./ _])") |
|
216 |
||
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
217 |
translations |
12169 | 218 |
"[x:xs. P]" == "filter(%x. P) xs" |
219 |
"Alls x:xs. P"== "list_all(%x. P)xs" |
|
5977
9f0c8869cf71
tidied up list definitions, using type 'a option instead of
paulson
parents:
5191
diff
changeset
|
220 |
|
3120
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
221 |
|
c58423c20740
New directory to contain examples of (co)inductive definitions
paulson
parents:
diff
changeset
|
222 |
end |