-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgeneric_util_app.ml
More file actions
67 lines (52 loc) · 1.64 KB
/
Copy pathgeneric_util_app.ml
File metadata and controls
67 lines (52 loc) · 1.64 KB
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
(* Dealing with parametric types using defunctionalisation. *)
open Generic_util
open Monoid
module T = struct
type ('a,'b) app = ..
end
include T
type ('a,'b) t = ('a,'b) app
(** {2 Core parametric types} *)
type option' = OPTION
type (_,_) app += Option : 'a option -> ('a, option') app
let get_option = function
| Option x -> x
| _ -> assert false
type list' = LIST
type (_,_) app += List : 'a list -> ('a, list') app
let get_list = function
| List x -> x
| _ -> assert false
type array' = ARRAY
type (_,_) app += Array : 'a array -> ('a, array') app
let get_array = function
| Array x -> x
| _ -> assert false
(* Identity functor *)
type id = ID
type (_, _) app += Id : 'a -> ('a, id) app
let get_id = function
| Id x -> x
| _ -> assert false (* there is only way to deconstruct ('a, 't id) app *)
(* Example: the constant functor.
The type [const] doesn't build useful values, we use it as a {i code}
to be interpreted by [app] so that [('a, 'b const) app] is isomorphic to
['b]
*)
type 't const = CONST
type (_, _) app += Const : 't -> ('a, 't const) app
let get_const = function
| Const f -> f
| _ -> assert false (* there is only way to deconstruct ('a, 't const) app *)
(* exponential functor *)
type 'b exponential = EXPONENTIAL
type (_,_) app += Exponential : ('a -> 'b) -> ('a, 'b exponential) app
let get_exponential = function
| Exponential f -> f
| _ -> assert false (* there is only way to deconstruct ('a, 't exponential) app *)
(* functor composition *)
type ('f, 'g) comp = COMP
type (_, _) app += Comp : (('a,'f) app, 'g) app -> ('a, ('f, 'g) comp) app
let get_comp = function
| Comp x -> x
| _ -> assert false