0
|
1 |
(* Title: HOL/llist.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1992 University of Cambridge
|
|
5 |
|
|
6 |
Definition of type 'a llist by a greatest fixed point
|
|
7 |
|
|
8 |
Shares NIL, CONS, List_Fun, List_case with list.thy
|
|
9 |
|
|
10 |
Still needs filter and flatten functions -- hard because they need
|
|
11 |
bounds on the amount of lookahead required.
|
|
12 |
|
|
13 |
Could try (but would it work for the gfp analogue of term?)
|
|
14 |
LListD_Fun_def "LListD_Fun(A) == (%Z.diag({Numb(0)}) <++> diag(A) <**> Z)"
|
|
15 |
|
|
16 |
A nice but complex example would be [ML for the Working Programmer, page 176]
|
|
17 |
from(1) = enumerate (Lmap (Lmap(pack), makeqq(from(1),from(1))))
|
|
18 |
|
|
19 |
Previous definition of llistD_Fun was explicit:
|
|
20 |
llistD_Fun_def
|
|
21 |
"llistD_Fun(r) == \
|
|
22 |
\ {<LNil,LNil>} Un \
|
|
23 |
\ (UN x. (%z.split(z, %l1 l2.<LCons(x,l1),LCons(x,l2)>))``r)"
|
|
24 |
*)
|
|
25 |
|
|
26 |
LList = Gfp + List +
|
51
|
27 |
|
|
28 |
types
|
|
29 |
'a llist
|
|
30 |
|
|
31 |
arities
|
|
32 |
llist :: (term)term
|
|
33 |
|
0
|
34 |
consts
|
|
35 |
LList :: "'a node set set => 'a node set set"
|
|
36 |
LListD_Fun ::
|
|
37 |
"[('a node set * 'a node set)set, ('a node set * 'a node set)set] => \
|
|
38 |
\ ('a node set * 'a node set)set"
|
|
39 |
LListD ::
|
|
40 |
"('a node set * 'a node set)set => ('a node set * 'a node set)set"
|
|
41 |
llistD_Fun :: "('a llist * 'a llist)set => ('a llist * 'a llist)set"
|
|
42 |
|
|
43 |
Rep_LList :: "'a llist => 'a node set"
|
|
44 |
Abs_LList :: "'a node set => 'a llist"
|
|
45 |
LNil :: "'a llist"
|
|
46 |
LCons :: "['a, 'a llist] => 'a llist"
|
|
47 |
|
|
48 |
llist_case :: "['a llist, 'b, ['a, 'a llist]=>'b] => 'b"
|
|
49 |
|
|
50 |
LList_corec_fun :: "[nat, 'a=>unit+('b node set * 'a), 'a] => 'b node set"
|
|
51 |
LList_corec :: "['a, 'a => unit + ('b node set * 'a)] => 'b node set"
|
|
52 |
llist_corec :: "['a, 'a => unit + ('b * 'a)] => 'b llist"
|
|
53 |
|
|
54 |
Lmap :: "('a node set => 'b node set) => ('a node set => 'b node set)"
|
|
55 |
lmap :: "('a=>'b) => ('a llist => 'b llist)"
|
|
56 |
|
|
57 |
iterates :: "['a => 'a, 'a] => 'a llist"
|
|
58 |
|
|
59 |
Lconst :: "'a node set => 'a node set"
|
|
60 |
Lappend :: "['a node set, 'a node set] => 'a node set"
|
|
61 |
lappend :: "['a llist, 'a llist] => 'a llist"
|
|
62 |
|
|
63 |
|
|
64 |
rules
|
|
65 |
LListD_Fun_def "LListD_Fun(r) == (%Z.diag({Numb(0)}) <++> r <**> Z)"
|
|
66 |
LList_def "LList(A) == gfp(List_Fun(A))"
|
|
67 |
LListD_def "LListD(r) == gfp(LListD_Fun(r))"
|
|
68 |
(*faking a type definition...*)
|
|
69 |
Rep_LList "Rep_LList(xs): LList(range(Leaf))"
|
|
70 |
Rep_LList_inverse "Abs_LList(Rep_LList(xs)) = xs"
|
|
71 |
Abs_LList_inverse "M: LList(range(Leaf)) ==> Rep_LList(Abs_LList(M)) = M"
|
|
72 |
(*defining the abstract constructors*)
|
|
73 |
LNil_def "LNil == Abs_LList(NIL)"
|
|
74 |
LCons_def "LCons(x,xs) == Abs_LList(CONS(Leaf(x), Rep_LList(xs)))"
|
|
75 |
|
|
76 |
llist_case_def
|
|
77 |
"llist_case(l,c,d) == \
|
|
78 |
\ List_case(Rep_LList(l), c, %x y. d(Inv(Leaf,x), Abs_LList(y)))"
|
|
79 |
|
|
80 |
LList_corec_fun_def
|
|
81 |
"LList_corec_fun(k,f) == \
|
|
82 |
\ nat_rec(k, %x. {}, \
|
38
|
83 |
\ %j r x. sum_case(f(x), %u.NIL, \
|
0
|
84 |
\ %v. split(v, %z w. CONS(z, r(w)))))"
|
|
85 |
|
|
86 |
LList_corec_def
|
|
87 |
"LList_corec(a,f) == UN k. LList_corec_fun(k,f,a)"
|
|
88 |
|
|
89 |
llist_corec_def
|
|
90 |
"llist_corec(a,f) == \
|
38
|
91 |
\ Abs_LList(LList_corec(a, %z.sum_case(f(z), %x.Inl(x), \
|
0
|
92 |
\ %y.split(y, %v w. Inr(<Leaf(v), w>)))))"
|
|
93 |
|
|
94 |
llistD_Fun_def
|
|
95 |
"llistD_Fun(r) == \
|
|
96 |
\ prod_fun(Abs_LList,Abs_LList) `` \
|
|
97 |
\ LListD_Fun(diag(range(Leaf)), \
|
|
98 |
\ prod_fun(Rep_LList,Rep_LList) `` r)"
|
|
99 |
|
|
100 |
Lconst_def "Lconst(M) == lfp(%N. CONS(M, N))"
|
|
101 |
|
|
102 |
Lmap_def
|
|
103 |
"Lmap(f,M) == \
|
|
104 |
\ LList_corec(M, %M. List_case(M, Inl(Unity), %x M'. Inr(<f(x), M'>)))"
|
|
105 |
|
|
106 |
lmap_def
|
|
107 |
"lmap(f,l) == \
|
|
108 |
\ llist_corec(l, %l. llist_case(l, Inl(Unity), %y z. Inr(<f(y), z>)))"
|
|
109 |
|
|
110 |
iterates_def "iterates(f,a) == llist_corec(a, %x. Inr(<x, f(x)>))"
|
|
111 |
|
|
112 |
(*Append generates its result by applying f, where
|
|
113 |
f(<NIL,NIL>) = Inl(Unity)
|
|
114 |
f(<NIL, CONS(N1,N2)>) = Inr(<N1, <NIL,N2>)
|
|
115 |
f(<CONS(M1,M2), N>) = Inr(<M1, <M2,N>)
|
|
116 |
*)
|
|
117 |
|
|
118 |
Lappend_def
|
|
119 |
"Lappend(M,N) == LList_corec(<M,N>, %p. split(p, \
|
|
120 |
\ %M N. List_case(M, List_case(N, Inl(Unity), \
|
|
121 |
\ %N1 N2. Inr(<N1, <NIL,N2>>)), \
|
|
122 |
\ %M1 M2. Inr(<M1, <M2,N>>))))"
|
|
123 |
|
|
124 |
lappend_def
|
|
125 |
"lappend(l,n) == llist_corec(<l,n>, %p. split(p, \
|
|
126 |
\ %l n. llist_case(l, llist_case(n, Inl(Unity), \
|
|
127 |
\ %n1 n2. Inr(<n1, <LNil,n2>>)), \
|
|
128 |
\ %l1 l2. Inr(<l1, <l2,n>>))))"
|
|
129 |
|
|
130 |
|
|
131 |
end
|