+ M.VVar "universe"
+ )
+ | None -> M.True
+ in
+
+ let q_where_rel = function
+ Some l ->
+ M.And
+ (M.Sub(M.VVar "rel_positions", M.Property(false, M.RefineExact, ("refRel", ["position"]), M.RefOf(M.RVar "uri0"))),
+ M.Sub(M.VVar "rel_depths", M.Property(false, M.RefineExact, ("refRel", ["depth"]), M.RefOf(M.RVar "uri0"))))
+ | None -> M.True
+ in
+
+ let q_where_sort = function
+ Some l ->
+ M.And
+ (M.And
+ (M.Sub(M.VVar "sort_positions", M.Property(false, M.RefineExact, ("refSort", ["position"]), M.RefOf(M.RVar "uri0"))),
+ M.Sub(M.VVar "sort_depths", M.Property(false, M.RefineExact, ("refSort", ["depth"]), M.RefOf(M.RVar "uri0")))),
+ M.Sub(M.VVar "sorts", M.Property(false, M.RefineExact, ("refSort", ["sort"]), M.RefOf(M.RVar "uri0"))))
+ | None -> M.True
+ in
+
+ let q_where cr =
+ let (cr_o,cr_r,cr_s) = cr in
+ M.And(M.And(q_where_obj cr_o, q_where_rel cr_r), q_where_sort cr_s)
+
+ in
+
+ let uri_of_entry (r, p, d) = r in
+
+ let build_select_obj (r, pos, dep) =
+ match dep with
+ None -> M.Select
+ ("uri",
+ M.Relation (false, M.RefineExact, ("backPointer", []),
+ M.Ref (M.Const [r]), [assign "pos" "position"]),
+ M.Ex ["uri"]
+ ((M.Sub (M.Const [pos], M.Record ("uri", in_path "pos")))))
+ | Some dep -> let string_dep = string_of_int dep in
+ M.Select
+ ("uri",
+ M.Relation (false, M.RefineExact, ("backPointer", []),
+ M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
+ M.Ex ["uri"]
+ (M.And
+ ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
+ (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
+ in
+
+ let build_select_rel (pos, dep) =
+ M.Select
+ ("uri",
+ M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
+ M.Ex ["uri"]
+ (M.And
+ ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
+ (M.Sub (M.Const [dep], M.Record ("uri", in_path "d"))))))
+ in
+
+ let build_select_sort (pos, dep, sor) =
+ M.Select
+ ("uri",
+ M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
+ M.Ex ["uri"]
+ (M.And
+ ((M.And
+ ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
+ (M.Sub (M.Const [dep], M.Record ("uri", in_path "d"))))),
+ (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
+ in
+
+ let rec build_intersect_obj = function
+ [] -> M.Pattern (M.Const ["[.]*"])
+ | [hd] -> build_select_obj hd
+ | hd :: tl -> M.Intersect (build_select_obj hd, build_intersect_obj tl)
+ in
+
+ let rec build_intersect_rel = function
+ [] -> M.Ref(M.Const [])
+ | [hd] -> build_select_rel hd
+ | hd :: tl -> M.Intersect (build_select_rel hd, build_intersect_rel tl)
+ in
+
+ let rec build_intersect_sort = function
+ [] -> M.Ref(M.Const [])
+ | [hd] -> build_select_sort hd
+ | hd :: tl -> M.Intersect (build_select_sort hd, build_intersect_sort tl)
+ in
+
+ let build_intersect must =
+ let tostring_sort (a,b,c) =
+ let b1 = string_of_int b in
+ (a,b1,c)
+ in
+ let tostring_rel (a,b) =
+ let b1 = string_of_int b in
+ (a,b1)
+ in
+
+ let (l1,l2,l3) = must in
+ match (l1,l2,l3) with
+ l1,[],[] -> build_intersect_obj l1
+ | [],l2,[] -> let lrel = List.map tostring_rel l2 in
+ build_intersect_rel lrel
+ | [],[],l3 -> let lsort = List.map tostring_sort l3 in
+ build_intersect_sort lsort
+ | l1,l2,[] -> let lrel = List.map tostring_rel l2 in
+ M.Intersect (build_intersect_obj l1, build_intersect_rel lrel)
+ | l1,[],l3 -> let lsort = List.map tostring_sort l3 in
+ M.Intersect (build_intersect_obj l1, build_intersect_sort lsort)
+ | [],l2,l3 -> let lrel = List.map tostring_rel l2 in
+ let lsort = List.map tostring_sort l3 in
+ M.Intersect (build_intersect_rel lrel, build_intersect_sort lsort)
+ | _ -> let lrel = List.map tostring_rel l2 in
+ let lsort = List.map tostring_sort l3 in
+ M.Intersect (M.Intersect (build_intersect_obj l1, build_intersect_rel lrel), build_intersect_sort lsort)
+ in
+
+ let q_in = build_intersect must_use in
+ let q_select = M.Select ("uri0", q_in, q_where can_use) in
+
+ let (cr_o,cr_r,cr_s) = can_use in
+
+ let universe =
+ match cr_o with
+ None -> M.Const []
+ | Some cr_o -> M.Const (List.map uri_of_entry cr_o)
+ in
+
+ let tfst (a,b,c) = a in
+ let tsnd (a,b,c) = b in
+ let trd (a,b,c) = c in
+
+ let opos =
+ match cr_o with
+ None -> M.Const []
+ | Some cr_o -> M.Const (List.map tsnd cr_o) in
+
+ let odep =
+ match cr_o with
+ None -> M.Const []
+ | Some cr_o -> let lofc = List.map trd cr_o in
+ let f l c =
+ match c with
+ None -> l
+ | Some c -> l@[c]
+ in
+ let lof_dep_int = List.fold_left f [] lofc in
+ M.Const(List.map string_of_int lof_dep_int)