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