diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/evaluators/FOL.ml | 43 | ||||
| -rw-r--r-- | lib/kernel/Generator.ml | 6 |
2 files changed, 21 insertions, 28 deletions
diff --git a/lib/evaluators/FOL.ml b/lib/evaluators/FOL.ml index e28222b..a7685bd 100644 --- a/lib/evaluators/FOL.ml +++ b/lib/evaluators/FOL.ml @@ -187,32 +187,23 @@ let rec modes = index ) let project description relation = - let open Utilities.Result in - let* cursor = Protocols.Enumerable.cursor relation in - let produce ~yield = - let* scan = Protocols.Cursor.require cursor in - let rec distinct seen = - let* next = Protocols.Cursor.next scan in - match next with - | None -> Ok () - | Some tuple -> - let tuple = - { tuple with - attributes= - BatMap.String.filter - (fun name _ -> BatMap.String.mem name description) - tuple.Concepts.Tuple.attributes } - in - let key = Concepts.Tuple.hash tuple |> Concepts.Hash.to_raw_string in - if BatSet.String.mem key seen then distinct seen - else begin - yield tuple; - distinct (BatSet.String.add key seen) - end - in - distinct BatSet.String.empty - in - Ok (Kernel.Generator.cursor_of ~finally:(fun () -> Protocols.Handle.release cursor) produce) + let seen = ref BatSet.String.empty in + Protocols.Enumerable.cursor relation + |> Result.map + (Kernel.Generator.filter_map (fun tuple -> + let tuple = + { tuple with + attributes= + BatMap.String.filter + (fun name _ -> BatMap.String.mem name description) + tuple.Concepts.Tuple.attributes } + in + let key = Concepts.Tuple.hash tuple |> Concepts.Hash.to_raw_string in + if BatSet.String.mem key !seen then None + else begin + seen := BatSet.String.add key !seen; + Some tuple + end ) ) let agrees binding {Concepts.Tuple.attributes; _} = BatMap.String.for_all diff --git a/lib/kernel/Generator.ml b/lib/kernel/Generator.ml index a037b3d..4fd1ce0 100644 --- a/lib/kernel/Generator.ml +++ b/lib/kernel/Generator.ml @@ -54,7 +54,7 @@ class producer_cursor produce finally = let cursor_of ?finally produce = new producer_cursor produce finally |> Protocols.Handle.make -let filter keep cursor = +let filter_map f cursor = cursor_of ~finally:(fun () -> Protocols.Handle.release cursor) (fun ~yield -> @@ -65,7 +65,9 @@ let filter keep cursor = match next with | None -> Ok () | Some tuple -> - if keep tuple then yield tuple; + Option.iter yield (f tuple); pass () in pass () ) + +let filter keep = filter_map (fun tuple -> if keep tuple then Some tuple else None) |
