-
Notifications
You must be signed in to change notification settings - Fork 9
/
endpoints.ml
63 lines (49 loc) · 1.85 KB
/
endpoints.ml
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
open Util
(* We parametrize over an abstract module specifying how many endpoints our cubes have. Internally it just counts them with a stored natural number *)
type 'l len = 'l N.t
type wrapped = Wrap : 'l len -> wrapped
type 'l t = 'l len * 'l N.index
let data : wrapped option ref = ref None
let set_len x =
match !data with
| Some _ -> raise (Failure "arity can only be set once")
| None ->
let (Plus_something x) = N.plus_of_int x in
data := Some (Wrap (Nat x))
let wrapped () =
match !data with
| Some x -> x
| None -> raise (Failure "arity unset")
let uniq : type l1 l2. l1 len -> l2 len -> (l1, l2) Eq.t =
fun l1 l2 ->
match N.compare l1 l2 with
| Eq -> Eq
| _ -> raise (Failure "unexpected arity")
let len l = l
let indices : type l. l len -> (l t, l) Bwv.t =
fun l -> Bwv.map (fun i -> (l, i)) (Bwv.all_indices (len l))
let to_string : type l. l t option -> string =
fun i ->
let (Wrap l) = wrapped () in
match i with
| Some i -> string_of_int (N.to_int (len l) - N.int_of_index (snd i) - 1)
| None -> string_of_int (N.to_int (len l))
let of_char : type l. l len -> char -> (l t option, unit) result =
fun l c ->
try
let i = N.to_int (len l) - (int_of_char c - int_of_char '0') in
if i = 0 then Ok None else Ok (Some (l, N.index_of_int (len l) (i - 1)))
with Failure _ -> Error ()
let refl_char_data : char ref = ref 'e'
let refl_string_data : string ref = ref "e"
let refl_names_data : string list ref = ref [ "refl"; "Id" ]
let set_char c =
refl_char_data := c;
refl_string_data := String.make 1 c
let set_names strs = refl_names_data := strs
let refl_string () = !refl_string_data
let refl_names () = !refl_names_data
(* *)
let internal_data : (unit -> bool) ref = ref (fun () -> raise (Failure "internality unset"))
let internal () = !internal_data ()
let set_internal i = internal_data := fun () -> i