Evaluate typed queries over persistent maps

This commit is contained in:
sneeker committed 2017-02-17 18:21:00 +00:00
1 parent 3091746467
commit 4dc7c9e4d2
9 files changed
+464 -2

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
type t =
| VUnit
| VInt of int
| VBool of bool
| VString of string
| VTuple of t list
| VRecord of string * (string * t) list
| VCollection of t Delta_runtime.Pure_map.t
let rec to_string value =
match value with
| VUnit -> "unit"
| VInt number -> string_of_int number
| VBool true -> "true"
| VBool false -> "false"
| VString text -> Printf.sprintf "%S" text
| VTuple items -> "(tuple " ^ Util.join " " (List.map to_string items) ^ ")"
| VRecord (name, fields) ->
Printf.sprintf "(record:%s %s)" name
(Util.join " " (List.map (fun (label, field) -> "(" ^ label ^ " " ^ to_string field ^ ")") fields))
| VCollection map ->
let entries =
List.map (fun (key, item) -> Printf.sprintf "(%d %s)" key (to_string item))
(Delta_runtime.Pure_map.bindings map)
in
"(collection " ^ Util.join " " entries ^ ")"
let collection_of_list entries =
List.fold_left
(fun map (key, value) ->
if Delta_runtime.Pure_map.mem key map then
Diagnostic.error Location.none "duplicate key %d in the initial input" key;
Delta_runtime.Pure_map.add key value map)
Delta_runtime.Pure_map.empty entries
let collection_to_list map = Delta_runtime.Pure_map.bindings map
let equal (left : t) (right : t) = left = right
let field record label =
match record with
| VRecord (_, fields) -> Util.assoc_opt label fields
| _ -> None
let is_collection = function VCollection _ -> true | _ -> false