summaryrefslogtreecommitdiff
path: root/transfer/lib/prelude.tr
blob: 409647a26d2e8b7bcbde95549ef2d9ac0e13a029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
--
-- Prelude for the transfer language.
--


--
-- Basic functions
--

const : (A:Type) -> (B:Type) -> A -> B -> A
const _ _ x _ = x

id : (A:Type) -> A -> A
id _ x = x


--
-- The Bool type
--

data Bool : Type where 
	True : Bool
	False : Bool

not : Bool -> Bool
not b = if b then False else True


--
-- The Num class
--

Num : Type -> Type
Num = sig zero : A
	  plus : A -> A -> A
	  minus : A -> A -> A
	  one : A
	  times : A -> A -> A
	  div : A -> A -> A
	  mod : A -> A -> A
	  negate : A -> A
	  eq : A -> A -> Bool
	  compare : A -> A -> Ordering

-- Instances:

num_Integer : Num Integer
num_Integer = rec zero = 0
		  plus = prim_add_Int
		  minus = prim_sub_Int
		  one = 1
              	  times = prim_mul_Int
		  div = prim_div_Int
              	  mod = prim_mod_Int
		  negate = prim_neg_Int
		  eq = prim_eq_Int
		  compare = prim_cmp_Int

--
-- The Add class
--

Add : Type -> Type
Add = sig zero : A
	  plus : A -> A -> A

zero : (A : Type) -> Add A -> A
zero _ d = d.zero

plus : (A : Type) -> Add A -> A -> A -> A
plus _ d = d.plus

sum : (A:Type) -> Add A -> List A -> A
sum _ d (Nil _) = d.zero
sum A d (Cons _ x xs) = d.plus x (sum A d xs)

-- Operators:

{-
  (x + y) => (plus ? ? x y)
-}

-- Instances:

-- num_Integer

add_String : Add String
add_String = rec zero = ""
		 plus = prim_add_Str

--
-- The Sub class
--

Sub : Type -> Type
Sub = sig minus : A -> A -> A

minus : (A : Type) -> Sub A -> A
minus _ d = d.minus

-- Instances:

-- num_Integer


--
-- The Mul class
--

Mul : Type -> Type
Mul = sig one : A
	  times : A -> A -> A

one : (A : Type) -> Mul A -> A
one _ d = d.one

times : (A : Type) -> Mul A -> A -> A -> A
times _ d = d.times

product : (A:Type) -> Mul A -> List A -> A
product _ d (Nil _) = d.one
product A d (Cons _ x xs) = d.times x (product A d xs)

-- Operators:

{- 
  (x * y) => (times ? ? x y)
-}

-- Instances:

-- num_Integer


--
-- The Div class
--

Div : Type -> Type
Div = sig div : A -> A -> A
	  mod : A -> A -> A

div : (A : Type) -> Div A -> A -> A -> A
div _ d = d.div

mod : (A : Type) -> Div A -> A -> A -> A
mod _ d = d.mod

-- Operators:

{- 
  (x / y) => (div ? ? x y)
  (x % y) => (mod ? ? x y)
-}

-- Instances:

-- num_Integer



--
-- The Neg class
--

Neg : Type -> Type
Neg = sig negate : A -> A

negate : (A : Type) -> Neg A -> A -> A
negate _ d = d.neg

-- Operators:

{-
  (-x) => negate ? ? x
-}

-- Instances:

-- num_Integer

neg_Bool : Neg Bool
neg_Bool = rec negate = not



--
-- The Eq class
--

Eq : Type -> Type
Eq A = sig eq : A -> A -> Bool

eq : (A : Type) -> Eq A -> A -> A -> Bool
eq _ d = d.eq

neq : (A : Type) -> Eq A -> A -> A -> Bool
neq A d x y = not (eq A d x y)


-- Operators:

{-
  (x == y) => (eq ? ? x y)
  (x /= y) => (neq ? ? x y)
-}

-- Instances:

-- num_Integer

eq_String : Eq String
eq_String = rec eq = prim_eq_Str



--
-- The Ord class
--

data Ordering : Type where
	LT : Ordering
	EQ : Ordering
	GT : Ordering

Ord : Type -> Type
Ord A = sig eq : A -> A -> Bool
	    compare : A -> A -> Ordering

compare : (A : Type) -> Ord A -> A -> A -> Ordering
compare _ d = d.compare

ordOp : (Ordering -> Bool) -> (A : Type) -> Ord A -> A -> A -> Bool
ordOp f A d x y = f (compare A d x y)

lt : (A : Type) -> Ord A -> A -> A -> Bool
lt = ordOp (\o -> case o of { LT -> True; _ -> False })

le : (A : Type) -> Ord A -> A -> A -> Bool
le = ordOp (\o -> case o of { GT -> False; _ -> True })

ge : (A : Type) -> Ord A -> A -> A -> Bool
ge = ordOp (\o -> case o of { LT -> False; _ -> True })

gt : (A : Type) -> Ord A -> A -> A -> Bool
gt = ordOp (\o -> case o of { GT -> True; _ -> False })

-- Operators:

{- 
  (x < y) => (lt ? ? x y)
  (x <= y) => (le ? ? x y)
  (x >= y) => (ge ? ? x y)
  (x > y) => (gt ? ? x y)
-}

-- Instances:

-- num_Integer

ord_String : Ord String
ord_String = rec eq = prim_eq_Str
		 compare = prim_cmp_Str



--
-- The Show class
--

Show : Type -> Type
Show A = sig show : A -> String

show : (A : Type) -> Show A -> A -> String
show _ d = d.show


-- Instances:

show_Integer : Show Integer
show_Integer = rec show = prim_show_Int

show_String : Show String
show_String = rec show = prim_show_Str



--
-- The Monoid class
--

Monoid : Type -> Type
Monoid = sig mzero : A
	     mplus : A -> A -> A



--
-- The Compos class
--

Compos : Type -> Type
Compos T = sig 
  C : Type
  composOp : (c : C) -> ((a : C) -> T a -> T a) -> T c -> T c
  composFold : (B : Type) -> Monoid B -> (c : C) -> ((a : C) -> T a -> b) -> T c -> b

composOp : (T : Type) -> (d : Compos T) 
        -> (c : d.C) -> ((a : d.C) -> T a -> T a) -> T c -> T c
composOp _ d = d.composOp

composFold : (T : Type) -> (d : Compos T) -> (B : Type) -> Monoid B 
          -> (c : d.C) -> ((a : d.C) -> T a -> b) -> T c -> b
composFold _ _ d = d.composFold