| author | paulson |
| Fri, 11 Sep 1998 16:25:40 +0200 | |
| changeset 5479 | 5a5dfb0f0d7d |
| parent 5443 | e2459d18ff47 |
| child 5518 | 654ead0ba4f7 |
| 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 |
||
| 5427 | 9 |
List = Datatype + 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 |
map :: ('a=>'b) => ('a list => 'b list)
|
| 4502 | 21 |
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
|
22 |
list_update :: 'a list => nat => 'a => 'a list |
| 2608 | 23 |
take, drop :: [nat, 'a list] => 'a list |
24 |
takeWhile, |
|
25 |
dropWhile :: ('a => bool) => 'a list => 'a list
|
|
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
26 |
tl, butlast :: 'a list => 'a list |
| 1908 | 27 |
rev :: 'a list => 'a list |
| 4132 | 28 |
zip :: "'a list => 'b list => ('a * 'b) list"
|
| 5427 | 29 |
upt :: nat => nat => nat list ("(1[_../_'(])")
|
| 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 |
|
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
34 |
nonterminals |
|
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
35 |
lupdbinds lupdbind |
|
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
36 |
|
| 923 | 37 |
syntax |
|
5443
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
38 |
mem :: ['a, 'a list] => bool (infixl 55) |
|
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
39 |
list_all :: ('a => bool) => ('a list => bool)
|
|
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
40 |
|
| 923 | 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 |
|
5443
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
56 |
"x mem xs" == "x:set xs" |
|
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
57 |
"list_all P xs" == "Ball (set xs) P" |
|
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
58 |
|
| 923 | 59 |
"[x, xs]" == "x#[xs]" |
60 |
"[x]" == "x#[]" |
|
| 3842 | 61 |
"[x:xs . P]" == "filter (%x. P) xs" |
| 923 | 62 |
|
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
63 |
"_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
|
64 |
"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
|
65 |
|
| 5427 | 66 |
"[i..j]" == "[i..(Suc j)(]" |
67 |
||
68 |
||
| 2262 | 69 |
syntax (symbols) |
| 5295 | 70 |
"@filter" :: [pttrn, 'a list, bool] => 'a list ("(1[_\\<in>_ ./ _])")
|
| 2262 | 71 |
|
72 |
||
|
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
73 |
consts |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
74 |
lists :: 'a set => 'a list set |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
75 |
|
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
76 |
inductive "lists A" |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
77 |
intrs |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
78 |
Nil "[]: lists A" |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
79 |
Cons "[| a: A; l: lists A |] ==> a#l : lists A" |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
80 |
|
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
81 |
|
|
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
82 |
(*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
|
83 |
list version as "length".*) |
|
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
84 |
syntax length :: 'a list => nat |
| 3507 | 85 |
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
|
86 |
|
| 5183 | 87 |
primrec |
| 3320 | 88 |
"hd([]) = arbitrary" |
| 1898 | 89 |
"hd(x#xs) = x" |
| 5183 | 90 |
primrec |
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
91 |
"tl([]) = []" |
| 1898 | 92 |
"tl(x#xs) = xs" |
| 5183 | 93 |
primrec |
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
94 |
"last [] = arbitrary" |
|
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
95 |
"last(x#xs) = (if xs=[] then x else last xs)" |
| 5183 | 96 |
primrec |
|
3896
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
97 |
"butlast [] = []" |
|
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
nipkow
parents:
3842
diff
changeset
|
98 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
| 5183 | 99 |
primrec |
| 3465 | 100 |
"set [] = {}"
|
101 |
"set (x#xs) = insert x (set xs)" |
|
| 5183 | 102 |
primrec |
| 1898 | 103 |
"map f [] = []" |
104 |
"map f (x#xs) = f(x)#map f xs" |
|
| 5183 | 105 |
primrec |
106 |
append_Nil "[] @ys = ys" |
|
107 |
append_Cons "(x#xs)@ys = x#(xs@ys)" |
|
108 |
primrec |
|
| 1898 | 109 |
"rev([]) = []" |
110 |
"rev(x#xs) = rev(xs) @ [x]" |
|
| 5183 | 111 |
primrec |
| 1898 | 112 |
"filter P [] = []" |
113 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
| 5183 | 114 |
primrec |
| 1898 | 115 |
"foldl f a [] = a" |
116 |
"foldl f a (x#xs) = foldl f (f a x) xs" |
|
| 5183 | 117 |
primrec |
| 2608 | 118 |
"concat([]) = []" |
119 |
"concat(x#xs) = x @ concat(xs)" |
|
| 5183 | 120 |
primrec |
|
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
121 |
drop_Nil "drop n [] = []" |
|
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
122 |
drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
| 5183 | 123 |
primrec |
|
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
124 |
take_Nil "take n [] = []" |
|
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
125 |
take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
| 5183 | 126 |
primrec |
| 4502 | 127 |
"xs!0 = hd xs" |
128 |
"xs!(Suc n) = (tl xs)!n" |
|
| 5183 | 129 |
primrec |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
130 |
" [][i:=v] = []" |
|
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
131 |
"(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
|
132 |
| Suc j => x # xs[j:=v])" |
| 5183 | 133 |
primrec |
| 2608 | 134 |
"takeWhile P [] = []" |
135 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
| 5183 | 136 |
primrec |
| 2608 | 137 |
"dropWhile P [] = []" |
|
3584
8f9ee0f79d9a
Corected bug in def of dropWhile (also present in Haskell lib!)
nipkow
parents:
3507
diff
changeset
|
138 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
| 5183 | 139 |
primrec |
| 4132 | 140 |
"zip xs [] = []" |
141 |
"zip xs (y#ys) = (hd xs,y)#zip (tl xs) ys" |
|
| 5427 | 142 |
primrec |
143 |
"[i..0(] = []" |
|
144 |
"[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" |
|
| 5183 | 145 |
primrec |
| 4605 | 146 |
"nodups [] = True" |
147 |
"nodups (x#xs) = (x ~: set xs & nodups xs)" |
|
| 5183 | 148 |
primrec |
| 4605 | 149 |
"remdups [] = []" |
150 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
| 5183 | 151 |
primrec |
|
5443
e2459d18ff47
changed constants mem and list_all to mere translations
oheimb
parents:
5427
diff
changeset
|
152 |
replicate_0 "replicate 0 x = []" |
| 5183 | 153 |
replicate_Suc "replicate (Suc n) x = x # replicate n x" |
| 3196 | 154 |
|
| 5281 | 155 |
(** Lexcicographic orderings on lists **) |
156 |
||
157 |
consts |
|
158 |
lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
|
|
159 |
primrec |
|
160 |
"lexn r 0 = {}"
|
|
161 |
"lexn r (Suc n) = (prod_fun (split op#) (split op#) `` (r ** lexn r n)) Int |
|
162 |
{(xs,ys). length xs = Suc n & length ys = Suc n}"
|
|
163 |
||
164 |
constdefs |
|
165 |
lex :: "('a * 'a)set => ('a list * 'a list)set"
|
|
166 |
"lex r == UN n. lexn r n" |
|
167 |
||
168 |
lexico :: "('a * 'a)set => ('a list * 'a list)set"
|
|
169 |
"lexico r == inv_image (less_than ** lex r) (%xs. (length xs, xs))" |
|
170 |
||
| 923 | 171 |
end |
| 3507 | 172 |
|
173 |
ML |
|
174 |
||
175 |
local |
|
176 |
||
177 |
(* translating size::list -> length *) |
|
178 |
||
| 4151 | 179 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
|
| 3507 | 180 |
Syntax.const "length" $ t |
| 4151 | 181 |
| size_tr' _ _ _ = raise Match; |
| 3507 | 182 |
|
183 |
in |
|
184 |
||
185 |
val typed_print_translation = [("size", size_tr')];
|
|
186 |
||
187 |
end; |