aboutsummaryrefslogtreecommitdiffstats
path: root/lib/evaluators/FOL.ml
blob: 70cf9deda86e15664557ce39613876ae9bb26c58 (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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Error = struct
  open Concepts.Condition

  let unknown_relation name =
    condition "unknown-relation" "A plan named a relation that does not exist under this snapshot"
      ("name" |=| Concepts.Value.String name)

  let cancelled () =
    condition "evaluation-cancelled" "An evaluation was stopped before it produced its result" empty
end

type plan = Base of string

module Program = Protocols.Program.Make (struct
  type t = plan
end)

let execute context = function
  | Base name ->
     let open Utilities.Result in
     match context.Protocols.Context.status () with
     | `Cancelled | `Exhausted -> Error (Error.cancelled ())
     | `Live ->
        let* found = Runtime.Context.resolve context name in
        let* handle = Option.to_result ~none:(Error.unknown_relation name) found in
        Fun.protect
          ~finally:(fun () -> Protocols.Handle.release handle)
          (fun () ->
            let* enumerable = Protocols.Enumerable.require handle in
            Protocols.Enumerable.enumerate enumerable context)

class evaluator = object (self)
  inherit Kernel.Lifecycle.null
  inherit Kernel.Identity.of_id

  method invoke ~program context = execute context program
  method protocols : Protocols.Handle.protocol list = [Program.make self]
end

let make () = new evaluator |> Protocols.Handle.make