author | nipkow |
Fri, 17 Jan 1997 18:32:24 +0100 | |
changeset 2523 | 0ccea141409b |
parent 2512 | 0231e4f467f2 |
child 2608 | 450c9b682a92 |
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 |
||
9 |
List = Arith + |
|
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 |
|
14 |
||
1908 | 15 |
"@" :: ['a list, 'a list] => 'a list (infixr 65) |
16 |
drop :: [nat, 'a list] => 'a list |
|
17 |
filter :: ['a => bool, 'a list] => 'a list |
|
18 |
flat :: 'a list list => 'a list |
|
19 |
foldl :: [['b,'a] => 'b, 'b, 'a list] => 'b |
|
20 |
hd :: 'a list => 'a |
|
21 |
length :: 'a list => nat |
|
22 |
set_of_list :: ('a list => 'a set) |
|
23 |
list_all :: ('a => bool) => ('a list => bool) |
|
24 |
map :: ('a=>'b) => ('a list => 'b list) |
|
25 |
mem :: ['a, 'a list] => bool (infixl 55) |
|
26 |
nth :: [nat, 'a list] => 'a |
|
27 |
take :: [nat, 'a list] => 'a list |
|
28 |
tl,ttl :: 'a list => 'a list |
|
29 |
rev :: 'a list => '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 |
||
923 | 47 |
primrec hd list |
1898 | 48 |
"hd([]) = (@x.False)" |
49 |
"hd(x#xs) = x" |
|
923 | 50 |
primrec tl list |
1898 | 51 |
"tl([]) = (@x.False)" |
52 |
"tl(x#xs) = xs" |
|
923 | 53 |
primrec ttl list |
54 |
(* a "total" version of tl: *) |
|
1898 | 55 |
"ttl([]) = []" |
56 |
"ttl(x#xs) = xs" |
|
923 | 57 |
primrec "op mem" list |
1898 | 58 |
"x mem [] = False" |
59 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
1908 | 60 |
primrec set_of_list list |
61 |
"set_of_list [] = {}" |
|
62 |
"set_of_list (x#xs) = insert x (set_of_list xs)" |
|
923 | 63 |
primrec list_all list |
64 |
list_all_Nil "list_all P [] = True" |
|
65 |
list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)" |
|
66 |
primrec map list |
|
1898 | 67 |
"map f [] = []" |
68 |
"map f (x#xs) = f(x)#map f xs" |
|
923 | 69 |
primrec "op @" list |
1898 | 70 |
"[] @ ys = ys" |
71 |
"(x#xs)@ys = x#(xs@ys)" |
|
1169 | 72 |
primrec rev list |
1898 | 73 |
"rev([]) = []" |
74 |
"rev(x#xs) = rev(xs) @ [x]" |
|
923 | 75 |
primrec filter list |
1898 | 76 |
"filter P [] = []" |
77 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
923 | 78 |
primrec foldl list |
1898 | 79 |
"foldl f a [] = a" |
80 |
"foldl f a (x#xs) = foldl f (f a x) xs" |
|
923 | 81 |
primrec length list |
1898 | 82 |
"length([]) = 0" |
83 |
"length(x#xs) = Suc(length(xs))" |
|
923 | 84 |
primrec flat list |
1898 | 85 |
"flat([]) = []" |
86 |
"flat(x#xs) = x @ flat(xs)" |
|
1419
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
87 |
primrec drop list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
88 |
drop_Nil "drop n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
89 |
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
|
90 |
primrec take list |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
91 |
take_Nil "take n [] = []" |
a6a034a47a71
defined take/drop by induction over list rather than nat.
nipkow
parents:
1370
diff
changeset
|
92 |
take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
923 | 93 |
defs |
1824 | 94 |
nth_def "nth(n) == nat_rec hd (%m r xs. r(tl(xs))) n" |
923 | 95 |
end |