]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/mathql_interpreter/union.ml
First very-very-very-very-alfa release of a MathQL Interpreter implemented
[helm.git] / helm / ocaml / mathql_interpreter / union.ml
diff --git a/helm/ocaml/mathql_interpreter/union.ml b/helm/ocaml/mathql_interpreter/union.ml
new file mode 100644 (file)
index 0000000..bf402a2
--- /dev/null
@@ -0,0 +1,93 @@
+
+(*
+ * implementazione del comando UNION
+ *)
+
+(*
+ * 
+ *)
+let xres_fill_context hr h1 l1 =
+ match l1 with
+    [] -> []
+ |   _ ->
+     let hh = List.combine h1 l1
+     in
+      List.map
+       (fun x ->
+        if (List.mem_assoc x hh) then
+        List.assoc x hh
+       else
+        ""
+       )
+       hr
+;;
+
+(*
+ * implementazione del comando UNION
+ *)
+let union_ex alist1 alist2 =
+ let head1 = List.hd alist1
+ and tail1 = List.tl alist1
+ and head2 = List.hd alist2
+ and tail2 = List.tl alist2 (* e fin qui ... *)
+ in
+  match (head1, head2) with
+     ([], _) -> assert false (* gli header non devono mai essere vuoti *)
+  |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
+  |  (_,  _) -> let headr = (head2 @
+                            (List.find_all
+                            (function t -> not (List.mem t head2))
+                            head1)
+                           ) in (* header del risultato finale *)
+      List.append (* il risultato finale e' la concatenazione ...*)
+       [headr] (* ... dell'header costruito prima ...*)
+       (match (tail1, tail2) with (* e di una coda "unione" *)
+           ([], _) -> tail2 (* va bene perche' l'altra lista e' vuota *)
+        |  (_, []) -> tail1 (* va bene perche' l'altra lista e' vuota *)
+        |  (_,  _) ->
+           let first = (* parte dell'unione che riguarda solo il primo set *)
+            List.map (fun l -> [List.hd l] @
+                      xres_fill_context
+                       (List.tl headr) (List.tl head1) (List.tl l)
+                     ) tail1
+            in
+            List.fold_left
+             (fun par x ->
+              let y = (* elemento candidato ad entrare *)
+               [List.hd x]
+               @
+               xres_fill_context
+                (List.tl headr) (List.tl head2) (List.tl x)
+              in
+               par @ if (List.find_all (fun t -> t = y) par) = [] then
+                      [y]
+                     else
+                      []
+             )
+             first
+             tail2
+(*          first @
+            List.map (fun l -> [List.hd l] @
+                      xres_fill_context
+                       (List.tl headr) (List.tl head2) (List.tl l)
+                     ) tail2
+*)
+       ) (* match *)
+;;
+
+(** TEST **)
+
+(*
+let h1 = ["retVal";     "a";      "b"];;
+let l1 = ["pippo";      "3";      "3"];;
+let l3 = ["pluto";      "7";      "8"]
+let r1 = [h1; l1; l3];;
+
+(*let h2 = ["retVal";               "b";      "c"];;
+let l2 = ["pippo";                "3";      "1"];;*)
+let h2 = ["retVal";     "a";      "b"];;
+let l2 = ["pippo";      "3";      "3"];;
+let r2 = [h2; l2];;
+
+List.map (fun l -> List.iter print_endline l) (xres_union (r1, r2));;
+*)