Source file Choice.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
(* Copyright (c) 2013, Simon Cruanes *)

(** {1 Backtracking monad} *)

(** A choice among values of type 'a. It is implemented using a
    success continuation, and a failure continuation
    (SKFT is Success Failure Kontinuation) *)
type 'a t = {
  skf : 'b. ('a, 'b) sk -> 'b fk -> 'b;
} [@@unboxed]

(** Success continuation *)
and ('a,'b) sk = 'a -> 'b fk -> 'b

(** Failure continuation *)
and 'a fk = unit -> 'a

type 'a choice = 'a t

let return x = { skf = (fun sk fk -> sk x fk) }

let fail = { skf = fun _sk fk -> fk () }

let cons x c =
  { skf = fun sk fk -> sk x (fun () -> c.skf sk fk) }

let mplus a b =
  { skf=(fun sk fk ->
        let fk' () = b.skf sk fk in  (* on failure of a, try b *)
        a.skf sk fk')
  }

let rec of_list l = match l with
  | [] -> fail
  | x :: l' ->
    { skf=fun sk fk ->
          sk x (fun () -> (of_list l').skf sk fk)
    }

let delay f =
  { skf=(fun sk fk -> (f ()).skf sk fk) }

let bind f x =
  { skf=fun sk fk ->
        x.skf (fun val_x fk -> (f val_x).skf sk fk) fk
  }

let (>>=) x f = bind f x

let rec from_fun f =
  match f () with
    | None -> fail
    | Some x ->
      { skf=(fun sk fk ->
            let fk' () = (from_fun f).skf sk fk in
            sk x fk')
      }

(* reflect operator, the inverse of msplit. It appends the first
 * element (if any) to the remaining ones *)
let reflect opt = match opt with
  | None -> fail
  | Some (x, c) ->
    { skf=(fun sk fk ->
          let fk' () = c.skf sk fk in
          sk x fk')
    }

(* msplit operator, the base for other combinators. It returns
    the first solution, if any. *)
let msplit (a : 'a t) : ('a * 'a t) option t =
  a.skf
    (fun x fk -> return (Some (x, fk () >>= reflect)))
    (fun () -> return None)

let rec interleave a b =
  msplit a >>= function
  | None -> b
  | Some (val_a, a') ->
    let c = interleave b a' in
    { skf=(fun sk fk ->
          let fk' () = c.skf sk fk in
          sk val_a fk')
    }

let rec fair_bind f x =
  msplit x >>= function
  | None -> fail
  | Some (val_x, x') ->
    interleave (f val_x) (fair_bind f x')

let ite c th el =
  msplit c >>= function
  | None -> el
  | Some (val_c, c') ->
    mplus (th val_c) (c' >>= th)

let map f c =
  {skf=(fun sk fk -> c.skf (fun x -> sk (f x)) fk)}

let product a b =
  {skf=(fun sk fk ->
      a.skf
        (fun x fk' ->
           let sk' y fk' = sk (x,y) fk' in
           b.skf sk' fk')
        fk)
  }

let fmap f c = {
  skf=fun sk fk ->
    c.skf (fun x fk -> match f x with
      | Some x -> sk x fk
      | None -> fk()
    ) fk
}

let filter p c = {
  skf=fun sk fk ->
    c.skf
      (fun x fk -> if p x then sk x fk else fk())
      fk
}

let once a = {
  skf=fun sk fk ->
    a.skf (fun x _fk -> sk x fk) fk
}

let rec take n c = match n with
  | 0 -> fail
  | 1 -> c
  | _ ->
    assert (n > 0);
    msplit c >>= function
    | None -> fail
    | Some (val_c, c') ->
      mplus (return val_c) (take (n-1) c')

let run_one c =
  c.skf (fun x _ -> Some x) (fun () -> None)

let run_n n c =
  let l = ref []
  and n = ref n in
  c.skf
    (fun val_c fk ->
       l := val_c :: !l;
       decr n;
       if !n = 0 then !l else fk ())
    (fun () -> !l)

let iter c k =
  c.skf
    (fun val_c fk ->
       let continue = k val_c in
       if continue then fk () else ())
    (fun () -> ())

let fold f acc c =
  let acc = ref acc in
  c.skf
    (fun x fk ->
       acc := f !acc x;
       fk ())
    (fun () -> !acc)

let count c =
  let n = ref 0 in
  c.skf (fun _ fk -> incr n; fk ()) (fun () -> !n)

let run_all c = fold (fun acc x -> x::acc) [] c

let to_list c = List.rev (run_all c)

let to_seq (c:'a t) : 'a Seq.t =
  let loop c () =
    c.skf
      (fun x fk ->
         Seq.Cons (x, fk))
      (fun () -> Seq.Nil)
  in
  loop c

let is_empty c =
  c.skf (fun _ _ -> false) (fun () -> true)

let forall c =
  c.skf
    (fun ans fk -> if ans then fk () else false)
    (fun () -> true)

let exists c =
  c.skf
    (fun ans fk -> if ans then true else fk())
    (fun () -> false)

let (>>-) x f = fair_bind f x
let (++) = mplus
let (<|>) = interleave

let lift f c = {
  skf=fun sk fk ->
    c.skf (fun x fk -> sk (f x) fk) fk
}

let lift2 f a b = {
  skf=fun sk fk ->
    a.skf
      (fun xa fk ->
         b.skf (fun xb fk -> sk (f xa xb) fk) fk
      )
      fk
}

let liftFair f c =
  c >>- fun x -> return (f x)

let liftFair2 f a b =
  a >>- fun x -> b >>- fun y -> return (f x y)

let pure = return

let app f_gen x_gen = {
  skf=fun sk fk ->
    f_gen.skf
      (fun f fk ->
         x_gen.skf
           (fun x fk -> sk (f x) fk)
           fk
      ) fk
}

let ($$) = app

let guard = function
  | true -> return ()
  | false -> fail

module Enum = struct
  type 'a t = 'a item choice

  and 'a item =
    | End
    | Item of 'a * 'a t

  let next e = e

  let empty = return End

  let cons1 x e = {
    skf=fun sk fk -> sk (Item (x,e)) fk
  }

  let cons head e = {
    skf= fun sk fk ->
      head.skf
        (fun x fk -> sk (Item (x, e)) fk)
        fk
  }

  let rec of_list l = match l with
    | [] -> return End
    | x :: l' ->
      return (Item (x, of_list l'))

  let rec zip a b =
    lift2
      (fun x y -> match x, y with
         | Item (xa, a'), Item (xb, b') ->
           Item ((xa,xb), zip a' b')
         | End, _
         | _, End -> End
      ) a b 

  let count e =
    let n = ref 0 in
    let rec count e =
      e.skf
        (fun x fk -> match x with
           | End -> incr n
           | Item (_, e') -> count e'; fk ())
        (fun () -> ())
    in count e; !n

  let to_lists e =
    let rec conv acc e =
      e >>= function
      | End -> return (List.rev acc)
      | Item (x, e') ->
        conv (x::acc) e'
    in conv [] e

    (*
  let to_lists e =
    let rec next acc e =
      e.skf
        (fun item fk1 ->
          match item with
          | End ->
            {
              skf=fun sk fk -> sk (List.rev acc) (fun () -> (fk1()).skf sk fk)
            }
          | Item (x, e_sub) ->
              next (x::acc) e_sub
              ++
              fk1()
        )
        (fun () -> fail)
    in
    next [] e
    *)

  let to_list_list e =
    to_list (to_lists e)
end

module List = struct
  let rec suffixes l = match l with
    | [] -> return []
    | _::l' ->
      { skf=(fun sk fk -> sk l (fun () -> (suffixes l').skf sk fk)); }

  type 'a tree =
    | Empty
    | Leaf of 'a
    | Node of 'a tree * 'a tree

  let rec _tree_of_list = function
    | [] -> Empty
    | x :: l' -> Node (Leaf x, _tree_of_list l')

  let _end = return Enum.End

  (* choose element among [t]. [rest] is elements not to choose from *)
  let rec choose_first rest t = match t with
    | Empty ->
      begin match rest with
        | Empty -> _end
        | Leaf _
        | Node _ -> fail
      end
    | Leaf x -> return (Enum.Item (x, permute_rec rest))
    | Node (l, r) ->
      (choose_first (Node (rest, r)) l)
      ++
        (choose_first (Node (l, rest)) r)
  and permute_rec = function
    | Empty -> return Enum.End
    | Leaf x -> return (Enum.Item (x, _end))
    | Node (l, r) ->
      choose_first l r
      ++
        choose_first r l

  let permutations l =
    let tree = _tree_of_list l in
    permute_rec tree

  let combinations n l =
    let m = List.length l in
    (* choose [n] elements among the [m] ones of [l] *)
    let rec choose_first n m l = match l with
      | _ when n > m -> fail
      | _ when n=m -> Enum.of_list l
      | [] -> fail
      | x :: l' ->
        cons
          (Enum.Item (x, choose_first (n-1)(m-1) l'))
          (choose_first n (m-1) l')
    in
    choose_first n m l
end

module Array = struct
  let _tree_of_arr a : _ List.tree =
    let t = ref List.Empty in
    for i = Array.length a -1 downto 0 do
      t := Node (Leaf a.(i), !t)
    done;
    !t

  let permutations a =
    let tree = _tree_of_arr a in
    List.permute_rec tree

  let combinations n a =
    List.combinations n (Array.to_list a)
end