author | paulson |
Fri, 19 Sep 1997 16:12:21 +0200 | |
changeset 3685 | 5b8c0c8f576e |
parent 3589 | 244daa75f890 |
child 3842 | b55686a7b22c |
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 |
18 |
hd :: '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 |
|
1908 | 27 |
tl,ttl :: 'a list => 'a list |
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#[]" |
|
1475 | 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 |
3320 | 65 |
"tl([]) = arbitrary" |
1898 | 66 |
"tl(x#xs) = xs" |
923 | 67 |
primrec ttl list |
68 |
(* a "total" version of tl: *) |
|
1898 | 69 |
"ttl([]) = []" |
70 |
"ttl(x#xs) = xs" |
|
923 | 71 |
primrec "op mem" list |
1898 | 72 |
"x mem [] = False" |
73 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
3465 | 74 |
primrec set list |
75 |
"set [] = {}" |
|
76 |
"set (x#xs) = insert x (set xs)" |
|
923 | 77 |
primrec list_all list |
78 |
list_all_Nil "list_all P [] = True" |
|
79 |
list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)" |
|
80 |
primrec map list |
|
1898 | 81 |
"map f [] = []" |
82 |
"map f (x#xs) = f(x)#map f xs" |
|
923 | 83 |
primrec "op @" list |
1898 | 84 |
"[] @ ys = ys" |
85 |
"(x#xs)@ys = x#(xs@ys)" |
|
1169 | 86 |
primrec rev list |
1898 | 87 |
"rev([]) = []" |
88 |
"rev(x#xs) = rev(xs) @ [x]" |
|
923 | 89 |
primrec filter list |
1898 | 90 |
"filter P [] = []" |
91 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
923 | 92 |
primrec foldl list |
1898 | 93 |
"foldl f a [] = a" |
94 |
"foldl f a (x#xs) = foldl f (f a x) xs" |
|
2608 | 95 |
primrec concat list |
96 |
"concat([]) = []" |
|
97 |
"concat(x#xs) = x @ concat(xs)" |
|
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
98 |
primrec drop list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
99 |
drop_Nil "drop n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
100 |
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
|
101 |
primrec take list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
102 |
take_Nil "take n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
103 |
take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
2738 | 104 |
primrec nth nat |
105 |
"nth 0 xs = hd xs" |
|
106 |
"nth (Suc n) xs = nth n (tl xs)" |
|
2608 | 107 |
primrec takeWhile list |
108 |
"takeWhile P [] = []" |
|
109 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
110 |
primrec dropWhile list |
|
111 |
"dropWhile P [] = []" |
|
3584
8f9ee0f79d9a
Corected bug in def of dropWhile (also present in Haskell lib!)
nipkow
parents:
3507
diff
changeset
|
112 |
"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
|
113 |
primrec replicate nat |
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
114 |
replicate_0 "replicate 0 x = []" |
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
nipkow
parents:
3584
diff
changeset
|
115 |
replicate_Suc "replicate (Suc n) x = x # replicate n x" |
3196 | 116 |
|
923 | 117 |
end |
3507 | 118 |
|
119 |
ML |
|
120 |
||
121 |
local |
|
122 |
||
123 |
(* translating size::list -> length *) |
|
124 |
||
125 |
fun size_tr' (Type ("fun", (Type ("list", _) :: _))) [t] = |
|
126 |
Syntax.const "length" $ t |
|
127 |
| size_tr' _ _ = raise Match; |
|
128 |
||
129 |
in |
|
130 |
||
131 |
val typed_print_translation = [("size", size_tr')]; |
|
132 |
||
133 |
end; |