aboutsummaryrefslogtreecommitdiffstats
path: root/lib/protocols/Enumerable.ml
blob: 76b4e86ca1b231872039c00b28c895579cb3aaf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module Error = struct
  open Concepts.Condition

  let not_enumerable () =
    condition "not-enumerable"
      "A relation was used where the expression must iterate its members, but it does not support \
       enumeration. A procedural relation may decide membership without being able to produce it."
      empty
end

class type implementation = object
  method enumerate : (Handle.t, Concepts.Condition.condition) result
end

type Handle.protocol += Enumerable of implementation
type t = implementation

let make impl = Enumerable (impl :> implementation)
let from handle = Handle.into handle (function Enumerable impl -> Some impl | _ -> None)
let require handle = from handle |> Option.to_result ~none:(Error.not_enumerable ())
let enumerate i = Handle.invoke i (fun o -> o#enumerate)
let cursor handle = Result.bind (require handle) enumerate
let scan handle f = Result.bind (cursor handle) (fun cursor -> Cursor.consuming cursor f)