root/trunk/HasCASL/test/Prelude.hascasl @ 4544

Revision 4544, 4.9 kB (checked in by 2maeder, 4 years ago)

moved variance sign to front of kind

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1%% predefined universe containing all types,
2%% superclass of all other classes
3
4class Type
5
6var s,t  : Type
7
8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9%% invisible type "Unit" for formulae
10
11type Unit: Type  %% flat cpo with bottom
12
13%% type aliases
14type Pred __ : Type -> Type := \ t: -Type . t ->? Unit
15type ? __ := \ t:Type . Unit ->? t
16
17pred true, false : Unit
18
19preds __/\__, __\/__, __=>__, __if__,__<=>__ : Unit * Unit
20pred not : Unit
21
22pred __=__ : s * s  %% =e=
23
24%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25%% (builtin) type (constructors)
26
27type __->?__ : -Type -> +Type -> Type
28
29%% nested pairs are different from n-tupels (n > 2)
30type __*__ : +Type -> +Type -> Type
31type __*__*__ : +Type -> +Type -> +Type -> Type
32
33%% ...
34
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%% "pred p args = e" abbreviates "op p args :? unit = e" 
37%% CASL requires "<=>" for pred-defn and disallows "()" as result
38
39op def, tt : Pred s
40var x : s
41
42program def = \x: s . () %% def is also total (identical to tt)
43
44program tt = \x: s . ()  %% tt is total "op tt(x: s): unit = ()"
45
46program __und__ (x, y: Unit) : Unit = ()
47
48
49%% total function type
50type __->__ : -Type -> +Type -> Type
51
52type __->__ < __ ->? __
53
54. __ in s -> t = \f: s ->? t . all(\x:s. def f(x))
55
56
57%% total functions
58op __res__ (x: s; y: t) : s = x
59op fst (x: s; y: t) : s = x
60program snd (x: s, y: t) : t = y
61
62
63%% trivial because its the strict function property
64
65. (\ (x:s, y:t). def (x res y)) =  (\ (x:s, y:t). (def y) und (def x))
66. fst = (__ res__)
67
68
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%% Internal Logic
71
72pred eq : s * s
73
74. (\x: s. eq(x, x)) = tt
75. (\(x, y:s). x res eq(x,y)) = (\(x, y:s). y res eq(x,y))
76
77%% then %def
78
79%% notation "\ ."  abbreviates "\bla:unit."
80%% where "bla" is never used, but only "()" instead
81
82%% for type inference
83%% an implicit cast from s to ?s of a term "e" yields the term "\ . e"
84
85type s < ?s
86
87program all (p: Pred(s)) : Pred Unit = eq(p, tt)
88
89%% the cast from ?s to s is still done manually here (for the strict "und")
90program And (x, y: Pred Unit) : Pred Unit = t1() und t2()
91
92%% use "And" instead of "und" to avoid cast from "?unit" to "unit"
93program __impl__ (x, y: Pred Unit) : Pred Unit = eq(x, x And y)
94
95program __or__ (x, y: Pred Unit) : Pred Unit = all(\r: Pred Unit.
96                ((x impl r) und (y impl r)) impl r)
97
98program ex (p: Pred(s)) : Pred Unit = all(\r:  Pred Unit.
99                all(\x:s. p(x) impl r) impl r)
100
101program ff () : Pred Unit = all(\r:  Pred Unit. r())
102
103program neg (r: Pred Unit) : Pred Unit = r impl ff
104
105%% the type instance for the first "eq" should be "?t"
106%% this is explicitely enforced by "\ .f(x)"
107
108. all(\(f,g): s->?t. all(\x:s. eq(\ . f(x), g(x)) impl eq(f, g)))
109
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111%% Recursion (requires "free type nat ::= 0 | Suc(nat)" and "+" 
112
113type nat
114
115class Cpo < Type
116{
117var c : Cpo
118
119pred __<<=__ : c * c
120
121. all(\x: c. x <<= x)                   %(reflexive)%
122. all(\(x,y,z: c). ((x <<= y) und (y <<= z)) impl x <<= z)   %(transitive)%
123. all(\(x,y,z: c). ((x <<= y) und (y <<= x)) impl eq(x,y))  %(antisymmetric)%
124
125pred isChain (s: nat -> c) <=> all(\n:nat. s(n) <<= s(Suc(n)))
126pred isBound (x: c; s: nat -> c) <=> all(\n:nat. s(n) <<= x) 
127
128op sup : (nat -> c) ->? c
129
130. all(\s: nat -> c. def(sup s) impl
131        (isBound(sup s, s) und all(\x:c. isBound(x, s) impl sup(s) <<= x)))
132                                                     %(sup is minimally bound)%
133
134. all(\s:nat -> c. isChain(s) impl def(sup(s)))      %(sup exists)%
135}
136
137class Pcpo < Cpo
138{
139var p : Pcpo
140
141op bottom : p
142
143. all(\x : p. bottom <<= x)   
144}
145
146class instance Flatcpo < Cpo
147{
148 var f : Flatcpo
149
150 program __<<=[f]__ = eq
151}
152
153var c, d: Cpo
154
155type instance __*__ : +Cpo -> +Cpo -> Cpo
156
157var x1,x2 : c; y1, y2 : d
158
159program (x1, y1) <<= (x2, y2) = (x1 <<= x2) und (y1 <<= y2)
160
161
162type instance __*__ : +Pcpo -> +Pcpo -> Pcpo     
163
164type Unit : Pcpo
165
166%% Pcont
167type instance __-->?__ : -Cpo -> +Cpo -> Pcpo
168
169 type __-->?__ < __->?__
170 . __ in c -->? d  = \f : c ->? d .
171        all(\(x,y: c). (def (f x) und x <<= y) impl def (f y)) und
172        all(\s: nat -> c. (isChain(s) und def f(sup(s))) impl
173                ex(\m:nat. def f(s(m)) und
174                        eq(sup(\n:nat.!f(s(n+m))), f(sup(s)))))
175
176  program f <<=[c -->? d] g = all(\x:c. def (f x) impl f(x) <<= g(x))
177
178%% Tcont
179type instance __-->__ : -Cpo -> +Cpo -> Cpo
180
181 type __-->__ < __-->?__
182 . __ in c --> d = \f : c -->? d . all(\x:c. def f(x))
183
184 var f, g : c --> d
185
186 program f <<= g = f <<=[c -->? d] g
187
188type instance __-->__ : -Cpo -> +Pcpo -> Pcpo
189
190fun Y : (p --> p) --> p
191
192. all(\f: p -->? p . eq(f(Y(f)), Y(f)) und
193        all(\x:p . eq(f(x), x) impl Y(f) <<= x))
194
195var f : p --> p; x : p
196. f(Y(f)) = Y(f)
197. f(x) = x => Y(f) <<= x 
198
199op undefined : c --> p = Y((\x': c --> p . x') as (c --> p) --> c --> p)
200
201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202%% user stuff
203
204free type bool ::= true | false
205
206type bool : Flatcpo
207type nat : Flatcpo
208
Note: See TracBrowser for help on using the browser.