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