10 so that list can serve as a "functor" for defining other recursive types |
10 so that list can serve as a "functor" for defining other recursive types |
11 *) |
11 *) |
12 |
12 |
13 SList = Sexp + |
13 SList = Sexp + |
14 |
14 |
15 types |
|
16 'a list |
|
17 |
|
18 arities |
|
19 list :: (term) term |
|
20 |
|
21 |
|
22 consts |
15 consts |
23 |
16 |
24 list :: 'a item set => 'a item set |
17 list :: 'a item set => 'a item set |
25 Rep_list :: 'a list => 'a item |
|
26 Abs_list :: 'a item => 'a list |
|
27 NIL :: 'a item |
18 NIL :: 'a item |
28 CONS :: ['a item, 'a item] => 'a item |
19 CONS :: ['a item, 'a item] => 'a item |
29 Nil :: 'a list |
|
30 "#" :: ['a, 'a list] => 'a list (infixr 65) |
|
31 List_case :: ['b, ['a item, 'a item]=>'b, 'a item] => 'b |
20 List_case :: ['b, ['a item, 'a item]=>'b, 'a item] => 'b |
32 List_rec :: ['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b |
21 List_rec :: ['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b |
33 list_case :: ['b, ['a, 'a list]=>'b, 'a list] => 'b |
|
34 list_rec :: ['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b |
|
35 Rep_map :: ('b => 'a item) => ('b list => 'a item) |
|
36 Abs_map :: ('a item => 'b) => 'a item => 'b list |
|
37 null :: 'a list => bool |
|
38 hd :: 'a list => 'a |
|
39 tl,ttl :: 'a list => 'a list |
|
40 set :: ('a list => 'a set) |
|
41 mem :: ['a, 'a list] => bool (infixl 55) |
|
42 map :: ('a=>'b) => ('a list => 'b list) |
|
43 "@" :: ['a list, 'a list] => 'a list (infixr 65) |
|
44 filter :: ['a => bool, 'a list] => 'a list |
|
45 |
22 |
46 (* list Enumeration *) |
|
47 |
|
48 "[]" :: 'a list ("[]") |
|
49 "@list" :: args => 'a list ("[(_)]") |
|
50 |
|
51 (* Special syntax for filter *) |
|
52 "@filter" :: [idt, 'a list, bool] => 'a list ("(1[_:_ ./ _])") |
|
53 |
|
54 translations |
|
55 "[x, xs]" == "x#[xs]" |
|
56 "[x]" == "x#[]" |
|
57 "[]" == "Nil" |
|
58 |
|
59 "case xs of Nil => a | y#ys => b" == "list_case a (%y ys. b) xs" |
|
60 |
|
61 "[x:xs . P]" == "filter (%x. P) xs" |
|
62 |
23 |
63 defs |
24 defs |
64 (* Defining the Concrete Constructors *) |
25 (* Defining the Concrete Constructors *) |
65 NIL_def "NIL == In0 (Numb 0)" |
26 NIL_def "NIL == In0 (Numb 0)" |
66 CONS_def "CONS M N == In1 (Scons M N)" |
27 CONS_def "CONS M N == In1 (Scons M N)" |
68 inductive "list(A)" |
29 inductive "list(A)" |
69 intrs |
30 intrs |
70 NIL_I "NIL: list(A)" |
31 NIL_I "NIL: list(A)" |
71 CONS_I "[| a: A; M: list(A) |] ==> CONS a M : list(A)" |
32 CONS_I "[| a: A; M: list(A) |] ==> CONS a M : list(A)" |
72 |
33 |
73 rules |
34 |
74 (* Faking a Type Definition ... *) |
35 typedef (List) |
75 Rep_list "Rep_list(xs): list(range(Leaf))" |
36 'a list = "list(range Leaf)" (list.NIL_I) |
76 Rep_list_inverse "Abs_list(Rep_list(xs)) = xs" |
37 |
77 Abs_list_inverse "M: list(range(Leaf)) ==> Rep_list(Abs_list(M)) = M" |
38 |
|
39 (*Declaring the abstract list constructors*) |
|
40 consts |
|
41 Nil :: 'a list |
|
42 "#" :: ['a, 'a list] => 'a list (infixr 65) |
|
43 |
|
44 (* list Enumeration *) |
|
45 |
|
46 "[]" :: 'a list ("[]") |
|
47 "@list" :: args => 'a list ("[(_)]") |
|
48 |
|
49 (* Special syntax for filter *) |
|
50 "@filter" :: [idt, 'a list, bool] => 'a list ("(1[_:_ ./ _])") |
|
51 |
|
52 |
|
53 translations |
|
54 "[x, xs]" == "x#[xs]" |
|
55 "[x]" == "x#[]" |
|
56 "[]" == "Nil" |
78 |
57 |
79 |
58 |
80 defs |
59 defs |
81 (* Defining the Abstract Constructors *) |
60 Nil_def "Nil == Abs_List NIL" |
82 Nil_def "Nil == Abs_list(NIL)" |
61 Cons_def "x#xs == Abs_List(CONS (Leaf x) (Rep_List xs))" |
83 Cons_def "x#xs == Abs_list(CONS (Leaf x) (Rep_list xs))" |
|
84 |
62 |
85 List_case_def "List_case c d == Case (%x. c) (Split d)" |
63 List_case_def "List_case c d == Case (%x. c) (Split d)" |
86 |
64 |
87 (* list Recursion -- the trancl is Essential; see list.ML *) |
65 (* list Recursion -- the trancl is Essential; see list.ML *) |
88 |
66 |
89 List_rec_def |
67 List_rec_def |
90 "List_rec M c d == wfrec (trancl pred_sexp) |
68 "List_rec M c d == wfrec (trancl pred_sexp) |
91 (%g. List_case c (%x y. d x y (g y))) M" |
69 (%g. List_case c (%x y. d x y (g y))) M" |
92 |
70 |
93 list_rec_def |
|
94 "list_rec l c d == |
|
95 List_rec (Rep_list l) c (%x y r. d (inv Leaf x) (Abs_list y) r)" |
|
96 |
71 |
|
72 |
|
73 |
|
74 constdefs |
97 (* Generalized Map Functionals *) |
75 (* Generalized Map Functionals *) |
|
76 Rep_map :: ('b => 'a item) => ('b list => 'a item) |
|
77 "Rep_map f xs == list_rec xs NIL (%x l r. CONS (f x) r)" |
|
78 |
|
79 Abs_map :: ('a item => 'b) => 'a item => 'b list |
|
80 "Abs_map g M == List_rec M Nil (%N L r. g(N)#r)" |
98 |
81 |
99 Rep_map_def "Rep_map f xs == list_rec xs NIL (%x l r. CONS (f x) r)" |
|
100 Abs_map_def "Abs_map g M == List_rec M Nil (%N L r. g(N)#r)" |
|
101 |
82 |
102 null_def "null(xs) == list_rec xs True (%x xs r. False)" |
83 list_rec :: ['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b |
103 hd_def "hd(xs) == list_rec xs arbitrary (%x xs r. x)" |
84 "list_rec l c d == |
104 tl_def "tl(xs) == list_rec xs arbitrary (%x xs r. xs)" |
85 List_rec (Rep_List l) c (%x y r. d (inv Leaf x) (Abs_List y) r)" |
105 (* a total version of tl: *) |
|
106 ttl_def "ttl(xs) == list_rec xs [] (%x xs r. xs)" |
|
107 |
86 |
108 set_def "set xs == list_rec xs {} (%x l r. insert x r)" |
87 null :: 'a list => bool |
|
88 "null(xs) == list_rec xs True (%x xs r. False)" |
109 |
89 |
110 mem_def "x mem xs == |
90 hd :: 'a list => 'a |
111 list_rec xs False (%y ys r. if y=x then True else r)" |
91 "hd(xs) == list_rec xs arbitrary (%x xs r. x)" |
112 map_def "map f xs == list_rec xs [] (%x l r. f(x)#r)" |
|
113 append_def "xs@ys == list_rec xs ys (%x l r. x#r)" |
|
114 filter_def "filter P xs == |
|
115 list_rec xs [] (%x xs r. if P(x) then x#r else r)" |
|
116 |
92 |
117 list_case_def "list_case a f xs == list_rec xs a (%x xs r. f x xs)" |
93 tl :: 'a list => 'a list |
|
94 "tl(xs) == list_rec xs arbitrary (%x xs r. xs)" |
|
95 |
|
96 (* a total version of tl *) |
|
97 ttl :: 'a list => 'a list |
|
98 "ttl(xs) == list_rec xs [] (%x xs r. xs)" |
|
99 |
|
100 set :: ('a list => 'a set) |
|
101 "set xs == list_rec xs {} (%x l r. insert x r)" |
|
102 |
|
103 mem :: ['a, 'a list] => bool (infixl 55) |
|
104 "x mem xs == list_rec xs False (%y ys r. if y=x then True else r)" |
|
105 |
|
106 map :: ('a=>'b) => ('a list => 'b list) |
|
107 "map f xs == list_rec xs [] (%x l r. f(x)#r)" |
|
108 |
|
109 filter :: ['a => bool, 'a list] => 'a list |
|
110 "filter P xs == list_rec xs [] (%x xs r. if P(x) then x#r else r)" |
|
111 |
|
112 list_case :: ['b, ['a, 'a list]=>'b, 'a list] => 'b |
|
113 "list_case a f xs == list_rec xs a (%x xs r. f x xs)" |
|
114 |
|
115 |
|
116 consts |
|
117 "@" :: ['a list, 'a list] => 'a list (infixr 65) |
|
118 |
|
119 defs |
|
120 append_def "xs@ys == list_rec xs ys (%x l r. x#r)" |
|
121 |
|
122 |
|
123 translations |
|
124 "case xs of Nil => a | y#ys => b" == "list_case a (%y ys. b) xs" |
|
125 |
|
126 "[x:xs . P]" == "filter (%x. P) xs" |
118 |
127 |
119 end |
128 end |