Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2014 01:36
Show Gist options
  • Save anonymous/cbfe96b920f08208e1dc to your computer and use it in GitHub Desktop.
Save anonymous/cbfe96b920f08208e1dc to your computer and use it in GitHub Desktop.
Unification doesn't happen between the InnerBag type and the ValueBag type. What's wrong?
module IntSet = Set.Make(struct type t = int let compare = compare end)
let list_filter_idxs idx l =
snd @@
List.fold_right (fun x (i, acc) ->
if IntSet.mem i idx then (i+1, x::acc)
else (i+1, acc)
) l (0, [])
type id_t = string
module type OrderedKeyType =
sig
type t
val compare: t -> t -> int
val filter_idxs : IntSet.t -> t -> t
end
(* multimap index *)
type index_t = {
mm_indices: IntSet.t; (* tuple indices *)
mm_comp_fn: string option;
mm_submaps: index_t list;
}
(* ----- Bag functions ----- *)
module IBag = struct
module type S = sig
type elt
type t
val empty : t
end
module Make(Ord : OrderedKeyType) = struct
module InnerMap = Map.Make(Ord)
type elt = Ord.t
type t = int InnerMap.t
let empty = InnerMap.empty
end
end
(* ------ Multimap functions ------ *)
module IMultimap = struct
module type S = sig
type elt
type t
module InnerBag : IBag.S
val slice : IntSet.t list -> [< `EQ | `GT | `LT ] list -> elt -> t -> InnerBag.t
end
module Make(OrdKey: OrderedKeyType) = struct
module InnerMap : (Map.S with type key = OrdKey.t) = Map.Make(OrdKey)
module InnerBag : (IBag.S with type elt = OrdKey.t) = IBag.Make(OrdKey)
type elt = OrdKey.t
type vindex_t = index_t * content InnerMap.t
(* top level type *)
and t = vindex_t list
and content = CMap of t | CBag of InnerBag.t
let rec slice idx_ids comps xs mm =
let error x = failwith @@ "(slice):"^x in
match idx_ids, comps with
| idx_id::rem_ids, comp::rem_comps ->
begin try
(* TODO: change to map here to find faster? *)
let idx, map = List.find (fun (i,_) -> IntSet.equal i.mm_indices idx_id) mm in
let key = OrdKey.filter_idxs idx.mm_indices xs in
let find_fn = InnerMap.find in
begin try
match find_fn key map with
| CMap v -> slice rem_ids rem_comps xs v
| CBag b -> b
with
Not_found -> InnerBag.empty
end
with
Not_found -> error @@ "no corresponding index found"
end
| [], _ -> error @@ "no index provided"
| _, [] -> error @@ "no comps provided"
end
end
module K3Values = struct
(* Interpreter representation of values *)
module rec OrderedKey : OrderedKeyType = struct
type t = Value.value_t
let compare = compare
let filter_idxs idxs = function
| Value.VTuple l -> Value.VTuple(list_filter_idxs idxs l)
| _ -> invalid_arg "not a vtuple"
end
and ValueBag : IBag.S with type elt = Value.value_t = IBag.Make(OrderedKey)
and ValueMMap : IMultimap.S with type elt = Value.value_t
and type InnerBag.elt = Value.value_t = IMultimap.Make(OrderedKey)
and Value : sig
type value_t =
| VInt of int
| VTuple of value_t list
| VBag of ValueBag.t
| VMultimap of ValueMMap.t
end = Value
open Value
(* Global collection functions for values *)
(* only for multimap *)
let v_slice_idx = fun err_fn idxs comps pat c -> match c, comps with
| VMultimap mm, VTuple comps' ->
let comp_fn = function
| VInt (-1) -> `LT
| VInt 1 -> `GT
| _ -> `EQ
in
let comps'' = List.map comp_fn comps' in
VBag(ValueMMap.slice idxs comps'' pat mm)
| _ -> err_fn "v_slice_idx" "bad format"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment