@@ -3,6 +3,7 @@ open Structured.TermTypes
3
3
open Structured.TypeOperations.Create
4
4
open TypeOperations.Union
5
5
open StructuredHelpers
6
+ open StructuredBool
6
7
7
8
let name = get_typed_term_unsafe (Const " Name" )
8
9
let val_lambda = get_typed_term_unsafe (Const " Val" )
@@ -192,3 +193,151 @@ let neg_infinity =
192
193
[ (Coinductive , get_flat_union_type [ generate_pred_rec_step 1 ]) ])
193
194
194
195
let infinity = get_type_union [ pos_infinity; neg_infinity ]
196
+
197
+ let unary_numerical_op =
198
+ build_structured_type
199
+ [ Intersection [ (ind_integer.union, ind_integer.union) ] ]
200
+ ind_integer.context
201
+
202
+ let binary_numerical_op =
203
+ build_structured_type
204
+ [
205
+ Intersection
206
+ [
207
+ ( ind_integer.union,
208
+ [ Intersection [ (ind_integer.union, ind_integer.union) ] ] );
209
+ ];
210
+ ]
211
+ ind_integer.context
212
+
213
+ let num_to_bool_op =
214
+ build_structured_type
215
+ [ Intersection [ (ind_integer.union, bool_type.union) ] ]
216
+ ind_integer.context
217
+
218
+ let binary_num_to_bool_op =
219
+ build_structured_type
220
+ [
221
+ Intersection
222
+ [
223
+ ( ind_integer.union,
224
+ [ Intersection [ (ind_integer.union, bool_type.union) ] ] );
225
+ ];
226
+ ]
227
+ ind_integer.context
228
+
229
+ (* Increments an inductive number by one *)
230
+ let increment =
231
+ get_typed_term_unsafe
232
+ (Abstraction
233
+ [
234
+ (ind_negative_number, Application (Variable 0 , val_lambda.term));
235
+ ( get_type_union [ zero.stype; ind_positive_number ],
236
+ Abstraction
237
+ [ (name.stype, succ.term); (val_lambda.stype, Variable 1 ) ] );
238
+ ])
239
+
240
+ (* Decrements an inductive number by one *)
241
+ let decrement =
242
+ get_typed_term_unsafe
243
+ (Abstraction
244
+ [
245
+ ( get_type_union [ zero.stype; ind_negative_number ],
246
+ Abstraction
247
+ [ (name.stype, pred.term); (val_lambda.stype, Variable 1 ) ] );
248
+ (ind_positive_number, Application (Variable 0 , val_lambda.term));
249
+ ])
250
+
251
+ (* Determines if a value is even or odd, leveraging the subtyping system *)
252
+ let is_even =
253
+ get_typed_term_unsafe
254
+ (Abstraction
255
+ [
256
+ (ind_even_integer, true_lambda.term);
257
+ (ind_odd_integer, false_lambda.term);
258
+ ])
259
+
260
+ let fix_binary_num_to_bool = fix ind_integer num_to_bool_op
261
+ let fix_binary_num_op = fix ind_integer unary_numerical_op
262
+
263
+ let is_equal =
264
+ get_typed_term_unsafe
265
+ (fix_binary_num_to_bool
266
+ (Abstraction
267
+ [
268
+ ( binary_num_to_bool_op,
269
+ Abstraction
270
+ [
271
+ ( zero.stype,
272
+ Abstraction
273
+ [
274
+ ( get_type_union
275
+ [ ind_positive_number; ind_negative_number ],
276
+ false_lambda.term );
277
+ (zero.stype, true_lambda.term);
278
+ ] );
279
+ ( ind_positive_number,
280
+ Abstraction
281
+ [
282
+ ( get_type_union [ zero.stype; ind_negative_number ],
283
+ false_lambda.term );
284
+ ( ind_positive_number,
285
+ Application
286
+ ( Application
287
+ ( Variable 2 ,
288
+ Application (decrement.term, Variable 1 ) ),
289
+ Application (decrement.term, Variable 0 ) ) );
290
+ ] );
291
+ ( ind_negative_number,
292
+ Abstraction
293
+ [
294
+ ( get_type_union [ zero.stype; ind_positive_number ],
295
+ false_lambda.term );
296
+ ( ind_negative_number,
297
+ Application
298
+ ( Application
299
+ ( Variable 2 ,
300
+ Application (increment.term, Variable 1 ) ),
301
+ Application (increment.term, Variable 0 ) ) );
302
+ ] );
303
+ ] );
304
+ ]))
305
+
306
+ let add =
307
+ get_typed_term_unsafe
308
+ (fix_binary_num_op
309
+ (Abstraction
310
+ [
311
+ ( binary_numerical_op,
312
+ Abstraction
313
+ [
314
+ (zero.stype, Abstraction [ (ind_integer, Variable 0 ) ]);
315
+ ( ind_negative_number,
316
+ Abstraction
317
+ [
318
+ ( ind_integer,
319
+ Application
320
+ ( Application
321
+ ( Variable 2 ,
322
+ Application (increment.term, Variable 1 ) ),
323
+ Application (decrement.term, Variable 0 ) ) );
324
+ ] );
325
+ ( ind_positive_number,
326
+ Abstraction
327
+ [
328
+ ( ind_integer,
329
+ Application
330
+ ( Application
331
+ ( Variable 2 ,
332
+ Application (decrement.term, Variable 1 ) ),
333
+ Application (increment.term, Variable 0 ) ) );
334
+ ] );
335
+ ] );
336
+ ]))
337
+
338
+ (* Later, consider also implementing these functions *)
339
+ (* subtract *)
340
+ (* fibonnaci *)
341
+ (* negate *)
342
+ (* multiply *)
343
+ (* divide *)
0 commit comments