author | wenzelm |
Mon, 29 Nov 1999 15:52:49 +0100 | |
changeset 8039 | a901bafe4578 |
parent 8000 | acafa0f15131 |
child 8115 | c802042066e8 |
permissions | -rw-r--r-- |
923 | 1 |
(* Title: HOL/List.thy |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow |
|
4 |
Copyright 1994 TU Muenchen |
|
5 |
||
2512 | 6 |
The datatype of finite lists. |
923 | 7 |
*) |
8 |
||
7032 | 9 |
List = Datatype + WF_Rel + NatBin + |
923 | 10 |
|
7224 | 11 |
datatype 'a list = Nil ("[]") | Cons 'a ('a list) (infixr "#" 65) |
923 | 12 |
|
13 |
consts |
|
1908 | 14 |
"@" :: ['a list, 'a list] => 'a list (infixr 65) |
15 |
filter :: ['a => bool, 'a list] => 'a list |
|
2608 | 16 |
concat :: 'a list list => 'a list |
1908 | 17 |
foldl :: [['b,'a] => 'b, 'b, 'a list] => 'b |
8000 | 18 |
foldr :: [['a,'b] => 'b, 'a list, 'b] => 'b |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
19 |
hd, last :: 'a list => 'a |
3465 | 20 |
set :: 'a list => 'a set |
5518 | 21 |
list_all :: ('a => bool) => ('a list => bool) |
1908 | 22 |
map :: ('a=>'b) => ('a list => 'b list) |
5518 | 23 |
mem :: ['a, 'a list] => bool (infixl 55) |
4502 | 24 |
nth :: ['a list, nat] => 'a (infixl "!" 100) |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
25 |
list_update :: 'a list => nat => 'a => 'a list |
2608 | 26 |
take, drop :: [nat, 'a list] => 'a list |
27 |
takeWhile, |
|
28 |
dropWhile :: ('a => bool) => 'a list => 'a list |
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
29 |
tl, butlast :: 'a list => 'a list |
1908 | 30 |
rev :: 'a list => 'a list |
4132 | 31 |
zip :: "'a list => 'b list => ('a * 'b) list" |
5427 | 32 |
upt :: nat => nat => nat list ("(1[_../_'(])") |
4605 | 33 |
remdups :: 'a list => 'a list |
34 |
nodups :: "'a list => bool" |
|
3589
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
35 |
replicate :: nat => 'a => 'a list |
923 | 36 |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
37 |
nonterminals |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
38 |
lupdbinds lupdbind |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
39 |
|
923 | 40 |
syntax |
41 |
(* list Enumeration *) |
|
2262 | 42 |
"@list" :: args => 'a list ("[(_)]") |
923 | 43 |
|
2512 | 44 |
(* Special syntax for filter *) |
5295 | 45 |
"@filter" :: [pttrn, 'a list, bool] => 'a list ("(1[_:_ ./ _])") |
923 | 46 |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
47 |
(* list update *) |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
48 |
"_lupdbind" :: ['a, 'a] => lupdbind ("(2_ :=/ _)") |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
49 |
"" :: lupdbind => lupdbinds ("_") |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
50 |
"_lupdbinds" :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _") |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
51 |
"_LUpdate" :: ['a, lupdbinds] => 'a ("_/[(_)]" [900,0] 900) |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
52 |
|
5427 | 53 |
upto :: nat => nat => nat list ("(1[_../_])") |
54 |
||
923 | 55 |
translations |
56 |
"[x, xs]" == "x#[xs]" |
|
57 |
"[x]" == "x#[]" |
|
3842 | 58 |
"[x:xs . P]" == "filter (%x. P) xs" |
923 | 59 |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
60 |
"_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs" |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
61 |
"xs[i:=x]" == "list_update xs i x" |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
62 |
|
5427 | 63 |
"[i..j]" == "[i..(Suc j)(]" |
64 |
||
65 |
||
2262 | 66 |
syntax (symbols) |
5295 | 67 |
"@filter" :: [pttrn, 'a list, bool] => 'a list ("(1[_\\<in>_ ./ _])") |
2262 | 68 |
|
69 |
||
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
70 |
consts |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
71 |
lists :: 'a set => 'a list set |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
72 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
73 |
inductive "lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
74 |
intrs |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
75 |
Nil "[]: lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
76 |
Cons "[| a: A; l: lists A |] ==> a#l : lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
77 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
78 |
|
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
79 |
(*Function "size" is overloaded for all datatypes. Users may refer to the |
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
80 |
list version as "length".*) |
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
81 |
syntax length :: 'a list => nat |
3507 | 82 |
translations "length" => "size:: _ list => nat" |
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
83 |
|
5183 | 84 |
primrec |
3320 | 85 |
"hd([]) = arbitrary" |
1898 | 86 |
"hd(x#xs) = x" |
5183 | 87 |
primrec |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
88 |
"tl([]) = []" |
1898 | 89 |
"tl(x#xs) = xs" |
5183 | 90 |
primrec |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
91 |
"last [] = arbitrary" |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
92 |
"last(x#xs) = (if xs=[] then x else last xs)" |
5183 | 93 |
primrec |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
94 |
"butlast [] = []" |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
95 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
5183 | 96 |
primrec |
5518 | 97 |
"x mem [] = False" |
98 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
99 |
primrec |
|
3465 | 100 |
"set [] = {}" |
101 |
"set (x#xs) = insert x (set xs)" |
|
5183 | 102 |
primrec |
5518 | 103 |
list_all_Nil "list_all P [] = True" |
104 |
list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)" |
|
105 |
primrec |
|
1898 | 106 |
"map f [] = []" |
107 |
"map f (x#xs) = f(x)#map f xs" |
|
5183 | 108 |
primrec |
109 |
append_Nil "[] @ys = ys" |
|
110 |
append_Cons "(x#xs)@ys = x#(xs@ys)" |
|
111 |
primrec |
|
1898 | 112 |
"rev([]) = []" |
113 |
"rev(x#xs) = rev(xs) @ [x]" |
|
5183 | 114 |
primrec |
1898 | 115 |
"filter P [] = []" |
116 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
5183 | 117 |
primrec |
6141 | 118 |
foldl_Nil "foldl f a [] = a" |
119 |
foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs" |
|
5183 | 120 |
primrec |
8000 | 121 |
"foldr f [] a = a" |
122 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
123 |
primrec |
|
2608 | 124 |
"concat([]) = []" |
125 |
"concat(x#xs) = x @ concat(xs)" |
|
5183 | 126 |
primrec |
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
127 |
drop_Nil "drop n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
128 |
drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
6408 | 129 |
(* Warning: simpset does not contain this definition but separate theorems |
130 |
for n=0 / n=Suc k*) |
|
5183 | 131 |
primrec |
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
132 |
take_Nil "take n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
133 |
take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
6408 | 134 |
(* Warning: simpset does not contain this definition but separate theorems |
135 |
for n=0 / n=Suc k*) |
|
136 |
primrec |
|
137 |
nth_Cons "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
|
138 |
(* Warning: simpset does not contain this definition but separate theorems |
|
139 |
for n=0 / n=Suc k*) |
|
5183 | 140 |
primrec |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
141 |
" [][i:=v] = []" |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
142 |
"(x#xs)[i:=v] = (case i of 0 => v # xs |
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
143 |
| Suc j => x # xs[j:=v])" |
5183 | 144 |
primrec |
2608 | 145 |
"takeWhile P [] = []" |
146 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
5183 | 147 |
primrec |
2608 | 148 |
"dropWhile P [] = []" |
3584
8f9ee0f79d9a
Corected bug in def of dropWhile (also present in Haskell lib!)
nipkow
parents:
3507
diff
changeset
|
149 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
5183 | 150 |
primrec |
4132 | 151 |
"zip xs [] = []" |
6306 | 152 |
"zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
6408 | 153 |
(* Warning: simpset does not contain this definition but separate theorems |
154 |
for xs=[] / xs=z#zs *) |
|
5427 | 155 |
primrec |
156 |
"[i..0(] = []" |
|
157 |
"[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" |
|
5183 | 158 |
primrec |
4605 | 159 |
"nodups [] = True" |
160 |
"nodups (x#xs) = (x ~: set xs & nodups xs)" |
|
5183 | 161 |
primrec |
4605 | 162 |
"remdups [] = []" |
163 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
5183 | 164 |
primrec |
5443
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
165 |
replicate_0 "replicate 0 x = []" |
5183 | 166 |
replicate_Suc "replicate (Suc n) x = x # replicate n x" |
3196 | 167 |
|
6408 | 168 |
(** Lexicographic orderings on lists **) |
5281 | 169 |
|
170 |
consts |
|
171 |
lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" |
|
172 |
primrec |
|
173 |
"lexn r 0 = {}" |
|
174 |
"lexn r (Suc n) = (prod_fun (split op#) (split op#) `` (r ** lexn r n)) Int |
|
175 |
{(xs,ys). length xs = Suc n & length ys = Suc n}" |
|
176 |
||
177 |
constdefs |
|
178 |
lex :: "('a * 'a)set => ('a list * 'a list)set" |
|
179 |
"lex r == UN n. lexn r n" |
|
180 |
||
181 |
lexico :: "('a * 'a)set => ('a list * 'a list)set" |
|
182 |
"lexico r == inv_image (less_than ** lex r) (%xs. (length xs, xs))" |
|
183 |
||
923 | 184 |
end |
3507 | 185 |
|
186 |
ML |
|
187 |
||
188 |
local |
|
189 |
||
190 |
(* translating size::list -> length *) |
|
191 |
||
4151 | 192 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = |
3507 | 193 |
Syntax.const "length" $ t |
4151 | 194 |
| size_tr' _ _ _ = raise Match; |
3507 | 195 |
|
196 |
in |
|
197 |
||
198 |
val typed_print_translation = [("size", size_tr')]; |
|
199 |
||
200 |
end; |