-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
64 lines (50 loc) · 1.51 KB
/
main.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
64
#use "topfind"
open OptComplex;;
open OptVectorData;;
#use "CCVector.ml"
open Runcode;;
(*
type 'a opt_vector =
| OptNil
| OptPush of ('a opt_vector * 'a code)
| OptPop of ('a opt_vector);;
*)
let get_opt v i =
if i < 0 || i >= v.size then None else Some (get v i)
(** get the nth element starting from the back *)
let rec back_nth n v : ('a option) code =
match v with
| OptFrom vec -> .<(get_opt vec (length vec - n - 1))>.
| OptPush (v, x) -> if n = 0 then .<(Some x)>. else back_nth (n - 1) v
| OptPop v -> back_nth (n + 1) v
(** get the last element *)
let back =
fun v -> back_nth 0 v
(** Create an empty vector *)
let create_opt () = OptFrom (create ())
(** Create a vector from an existing vector *)
let create_from_opt v = OptFrom v
(** Push one element in the vector *)
let push_opt v x = OptPush (v, x)
(** Pop one element from the vector and gets its value now *)
let pop_opt v = match v with
| OptPush (v', x) -> v', .<Some x>.
| _ -> OptPop v, back v
(* Two functions that do some computations *)
let foo () = 0
let bar () = 1
(* Make sure a pop/push sequence is optimized *)
let prog1 () =
let v = create_opt () in
let v1 = push_opt v .<foo ()>. in
let v2 = push_opt v1 .<bar ()>. in
let v3, res = pop_opt v2 in
res
(* .<Some csp_x_3>. *)
(* Make sure a get is correctly staged *)
let prog2 () =
let v = create_from_opt (of_list [0; 2; 4; 5]) in
let v1, res = pop_opt v in
res
(* csp_get_opt_4 csp_vec_3 (((csp_length_2 csp_vec_1) - 0) - 1)>. *)
let _ = prog1 ()