|
| 1 | +open Metatypes |
| 2 | +open TypeOperations.MapType |
| 3 | +open ExampleHelpers |
| 4 | +open Recursive |
| 5 | + |
| 6 | +(* TODO: create some helper functions to make existential encoding easier to use *) |
| 7 | + |
| 8 | +let new_label = typed_term (Const "new") |
| 9 | +let get_label = typed_term (Const "get") |
| 10 | +let inc_label = typed_term (Const "inc") |
| 11 | + |
| 12 | +(* Represents the type `CounterModule = {new: Counter, get: Counter -> Nat, inc: Counter -> Counter}` *) |
| 13 | +let counter_module = |
| 14 | + map_type |
| 15 | + (fun int -> |
| 16 | + [ |
| 17 | + Intersection |
| 18 | + [ |
| 19 | + (new_label.rtype.union, [ UnivTypeVar 0 ]); |
| 20 | + ( get_label.rtype.union, |
| 21 | + [ Intersection [ ([ UnivTypeVar 0 ], int) ] ] ); |
| 22 | + ( inc_label.rtype.union, |
| 23 | + [ Intersection [ ([ UnivTypeVar 0 ], [ UnivTypeVar 0 ]) ] ] ); |
| 24 | + ]; |
| 25 | + ]) |
| 26 | + ind_integer |
| 27 | + |
| 28 | +(* Represents `forall Counter. (CounterModule -> Y)`, the "continuation" at the |
| 29 | + core of the polymorphic encoding of existential types *) |
| 30 | +let counter_continuation = |
| 31 | + map_type |
| 32 | + (fun counter_module_union -> |
| 33 | + [ |
| 34 | + UnivQuantification |
| 35 | + [ Intersection [ (counter_module_union, [ UnivTypeVar 1 ]) ] ]; |
| 36 | + ]) |
| 37 | + counter_module |
| 38 | + |
| 39 | +(* |
| 40 | + Represents the existential package type: |
| 41 | + `{exists Counter, CounterModule}` |
| 42 | + encoded using polymoprhism as: |
| 43 | + `forall Y.((forall Counter.(CounterModule -> Y) -> Y)` |
| 44 | +*) |
| 45 | +let counter_package_type = |
| 46 | + map_type |
| 47 | + (fun counter_continuation_union -> |
| 48 | + [ |
| 49 | + UnivQuantification |
| 50 | + [ Intersection [ (counter_continuation_union, [ UnivTypeVar 0 ]) ] ]; |
| 51 | + ]) |
| 52 | + counter_continuation |
| 53 | + |
| 54 | +(* Term representing a counter ADT, encoded using polymoprhism, using Int as the implementation for Counter *) |
| 55 | +let counter_adt = |
| 56 | + typed_term |
| 57 | + (UnivQuantifier |
| 58 | + (Abstraction |
| 59 | + [ |
| 60 | + ( counter_continuation, |
| 61 | + Application |
| 62 | + ( UnivApplication (Variable 0, ind_integer), |
| 63 | + Abstraction |
| 64 | + [ |
| 65 | + (new_label.rtype, one.term); |
| 66 | + ( get_label.rtype, |
| 67 | + Abstraction [ (ind_integer, Variable 0) ] ); |
| 68 | + ( inc_label.rtype, |
| 69 | + Abstraction |
| 70 | + [ |
| 71 | + ( ind_integer, |
| 72 | + Application (increment.term, Variable 0) ); |
| 73 | + ] ); |
| 74 | + ] ) ); |
| 75 | + ])) |
0 commit comments