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