author | oheimb |
Mon, 27 Apr 1998 19:29:19 +0200 | |
changeset 4836 | fc5773ae2790 |
parent 4643 | 1b40fcac5a09 |
child 5077 | 71043526295f |
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 |
||
4643 | 9 |
List = WF_Rel + |
923 | 10 |
|
977
5d57287e5e1e
changed syntax of datatype declarations (curried types for constructor
clasohm
parents:
965
diff
changeset
|
11 |
datatype 'a list = "[]" ("[]") | "#" '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 |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
18 |
hd, last :: 'a list => 'a |
3465 | 19 |
set :: 'a list => 'a set |
1908 | 20 |
list_all :: ('a => bool) => ('a list => bool) |
21 |
map :: ('a=>'b) => ('a list => 'b list) |
|
22 |
mem :: ['a, 'a list] => bool (infixl 55) |
|
4502 | 23 |
nth :: ['a list, nat] => 'a (infixl "!" 100) |
2608 | 24 |
take, drop :: [nat, 'a list] => 'a list |
25 |
takeWhile, |
|
26 |
dropWhile :: ('a => bool) => 'a list => 'a list |
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
27 |
tl, butlast :: 'a list => 'a list |
1908 | 28 |
rev :: 'a list => 'a list |
4132 | 29 |
zip :: "'a list => 'b list => ('a * 'b) list" |
4605 | 30 |
remdups :: 'a list => 'a list |
31 |
nodups :: "'a list => bool" |
|
3589
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
32 |
replicate :: nat => 'a => 'a list |
923 | 33 |
|
34 |
syntax |
|
35 |
(* list Enumeration *) |
|
2262 | 36 |
"@list" :: args => 'a list ("[(_)]") |
923 | 37 |
|
2512 | 38 |
(* Special syntax for filter *) |
2262 | 39 |
"@filter" :: [idt, 'a list, bool] => 'a list ("(1[_:_ ./ _])") |
923 | 40 |
|
41 |
translations |
|
42 |
"[x, xs]" == "x#[xs]" |
|
43 |
"[x]" == "x#[]" |
|
3842 | 44 |
"[x:xs . P]" == "filter (%x. P) xs" |
923 | 45 |
|
2262 | 46 |
syntax (symbols) |
47 |
"@filter" :: [idt, 'a list, bool] => 'a list ("(1[_\\<in>_ ./ _])") |
|
48 |
||
49 |
||
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
50 |
consts |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
51 |
lists :: 'a set => 'a list set |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
52 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
53 |
inductive "lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
54 |
intrs |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
55 |
Nil "[]: lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
56 |
Cons "[| a: A; l: lists A |] ==> a#l : lists A" |
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
57 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
58 |
|
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
59 |
(*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
|
60 |
list version as "length".*) |
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
61 |
syntax length :: 'a list => nat |
3507 | 62 |
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
|
63 |
|
923 | 64 |
primrec hd list |
3320 | 65 |
"hd([]) = arbitrary" |
1898 | 66 |
"hd(x#xs) = x" |
923 | 67 |
primrec tl list |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
68 |
"tl([]) = []" |
1898 | 69 |
"tl(x#xs) = xs" |
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
70 |
primrec last list |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
71 |
"last [] = arbitrary" |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
72 |
"last(x#xs) = (if xs=[] then x else last xs)" |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
73 |
primrec butlast list |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
74 |
"butlast [] = []" |
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
75 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
923 | 76 |
primrec "op mem" list |
1898 | 77 |
"x mem [] = False" |
78 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
3465 | 79 |
primrec set list |
80 |
"set [] = {}" |
|
81 |
"set (x#xs) = insert x (set xs)" |
|
923 | 82 |
primrec list_all list |
83 |
list_all_Nil "list_all P [] = True" |
|
84 |
list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)" |
|
85 |
primrec map list |
|
1898 | 86 |
"map f [] = []" |
87 |
"map f (x#xs) = f(x)#map f xs" |
|
923 | 88 |
primrec "op @" list |
1898 | 89 |
"[] @ ys = ys" |
90 |
"(x#xs)@ys = x#(xs@ys)" |
|
1169 | 91 |
primrec rev list |
1898 | 92 |
"rev([]) = []" |
93 |
"rev(x#xs) = rev(xs) @ [x]" |
|
923 | 94 |
primrec filter list |
1898 | 95 |
"filter P [] = []" |
96 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
923 | 97 |
primrec foldl list |
1898 | 98 |
"foldl f a [] = a" |
99 |
"foldl f a (x#xs) = foldl f (f a x) xs" |
|
2608 | 100 |
primrec concat list |
101 |
"concat([]) = []" |
|
102 |
"concat(x#xs) = x @ concat(xs)" |
|
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
103 |
primrec drop list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
104 |
drop_Nil "drop n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
105 |
drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
106 |
primrec take list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
107 |
take_Nil "take n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
108 |
take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
2738 | 109 |
primrec nth nat |
4502 | 110 |
"xs!0 = hd xs" |
111 |
"xs!(Suc n) = (tl xs)!n" |
|
2608 | 112 |
primrec takeWhile list |
113 |
"takeWhile P [] = []" |
|
114 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
115 |
primrec dropWhile list |
|
116 |
"dropWhile P [] = []" |
|
3584
8f9ee0f79d9a
Corected bug in def of dropWhile (also present in Haskell lib!)
nipkow
parents:
3507
diff
changeset
|
117 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
4132 | 118 |
primrec zip list |
119 |
"zip xs [] = []" |
|
120 |
"zip xs (y#ys) = (hd xs,y)#zip (tl xs) ys" |
|
4605 | 121 |
primrec nodups list |
122 |
"nodups [] = True" |
|
123 |
"nodups (x#xs) = (x ~: set xs & nodups xs)" |
|
124 |
primrec remdups list |
|
125 |
"remdups [] = []" |
|
126 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
3589
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
127 |
primrec replicate nat |
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
128 |
replicate_0 "replicate 0 x = []" |
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
129 |
replicate_Suc "replicate (Suc n) x = x # replicate n x" |
3196 | 130 |
|
923 | 131 |
end |
3507 | 132 |
|
133 |
ML |
|
134 |
||
135 |
local |
|
136 |
||
137 |
(* translating size::list -> length *) |
|
138 |
||
4151 | 139 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = |
3507 | 140 |
Syntax.const "length" $ t |
4151 | 141 |
| size_tr' _ _ _ = raise Match; |
3507 | 142 |
|
143 |
in |
|
144 |
||
145 |
val typed_print_translation = [("size", size_tr')]; |
|
146 |
||
147 |
end; |