]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationMatcher.ml
packaging cleanup: get rid of ancient debhelpers, use dh_install
[helm.git] / helm / ocaml / cic_notation / cicNotationMatcher.ml
index 08a4617f7c97cb672017e5215c9f9f4e2db78100..b9809335cf491d493c683dd81f69fe842398ac42 100644 (file)
@@ -25,6 +25,7 @@
 
 open Printf
 
+module Pp = CicNotationPp
 module Pt = CicNotationPt
 module Env = CicNotationEnv
 module Util = CicNotationUtil
@@ -117,14 +118,26 @@ struct
 
   let variable_closure k =
     (fun matched_terms terms ->
-      prerr_endline "variable_closure";
       match terms with
       | hd :: tl -> k (hd :: matched_terms) tl
       | _ -> assert false)
 
+  let success_closure ks k =
+    (fun matched_terms terms ->
+       match ks matched_terms with
+          None ->
+            begin
+              (* the match has failed, we rollback the last matched term
+               * into the unmatched ones and call the failure continuation
+               *)
+              match matched_terms with
+                  hd :: tl -> k tl (hd :: terms)
+                | _ -> assert false
+            end
+        | Some v -> Some v)
+
   let constructor_closure ks k =
     (fun matched_terms terms ->
-      prerr_endline "constructor_closure";
       match terms with
       | t :: tl ->
           (try
@@ -139,8 +152,7 @@ struct
       if t = [] then
         k
       else if are_empty t then
-        let res = match_cb (matched t) in
-        (fun matched_terms _ -> res matched_terms)
+        success_closure (match_cb (matched t)) k
       else
         match horizontal_split t with
         | _, [], _ -> assert false
@@ -183,13 +195,12 @@ struct
   struct
     type pattern_t = Pt.term
     type term_t = Pt.term
-    let classify = function
+    let rec classify = function
+      | Pt.AttributedTerm (_, t) -> classify t
       | Pt.Variable _ -> Variable
       | Pt.Magic _
       | Pt.Layout _
-      | Pt.Literal _ as t ->
-          prerr_endline (CicNotationPp.pp_term t);
-          assert false
+      | Pt.Literal _ as t -> assert false
       | _ -> Constructor
     let tag_of_pattern = CicNotationTag.get_tag
     let tag_of_term = CicNotationTag.get_tag
@@ -205,7 +216,7 @@ struct
       Pt.Variable (Pt.TermVar name)
     in
     let rec aux = function
-      | Pt.AttributedTerm (_, t) -> aux t
+      | Pt.AttributedTerm (_, t) -> assert false
       | Pt.Literal _
       | Pt.Layout _ -> assert false
       | Pt.Variable v -> Pt.Variable v
@@ -244,7 +255,8 @@ struct
           (fun env ->
             match m_checker (Env.lookup_term env name) env with
             | None -> None
-            | Some env' -> f env'))
+            | Some env' ->
+               f env'))
         (fun env -> Some env)
         map
     in
@@ -255,12 +267,16 @@ struct
             let env = env_of_matched pl matched_terms in
             match checker env with
             | None -> f matched_terms
-            | Some env -> Some (env, pid)))
+            | Some env ->
+               let magic_map =
+                 try List.assoc pid magic_maps with Not_found -> assert false
+               in
+               let env' = Env.remove_names env (List.map fst magic_map) in
+               Some (env', pid)))
         (fun _ -> None)
-        candidates
+        (List.rev candidates)
     in
     let match_cb rows =
-      prerr_endline (sprintf "match_cb on %d row(s)" (List.length rows));
       let candidates =
         List.map
           (fun (pl, pid) ->
@@ -270,44 +286,67 @@ struct
             pid, pl, magichecker magic_map)
           rows
       in
-      magichooser candidates
+       magichooser candidates
     in
     M.compiler rows' match_cb (fun _ -> None)
 
   and compile_magic = function
     | Pt.Fold (kind, p_base, names, p_rec) ->
         let p_rec_decls = Env.declarations_of_term p_rec in
+         (* LUCA: p_rec_decls should not contain "names" *)
         let acc_name = try List.hd names with Failure _ -> assert false in
-        let compiled = compiler [p_base, 0; p_rec, 1] in
-        (fun term env ->
-          let rec aux term =
-            match compiled term with
-            | None -> None
-            | Some (env', 0) -> Some (env', [])
-            | Some (env', 1) ->
-                begin
-                  let acc = Env.lookup_term env' acc_name in
-                  let env'' = Env.remove env' acc_name in
-                  match aux acc with
-                  | None -> None
-                  | Some (base_env, rec_envl) -> 
-                      Some (base_env, env'' :: rec_envl )
-                end
-            | _ -> assert false
-          in
-            match aux term with
-            | None -> None
-            | Some (base_env, rec_envl) ->
-                Some (base_env @ Env.coalesce_env p_rec_decls rec_envl))
+       let compiled_base = compiler [p_base, 0]
+       and compiled_rec = compiler [p_rec, 0] in
+         (fun term env ->
+            let aux_base term =
+              match compiled_base term with
+                | None -> None
+                | Some (env', _) -> Some (env', [])
+            in
+            let rec aux term =
+              match compiled_rec term with
+                | None -> aux_base term
+                | Some (env', _) ->
+                    begin
+                       let acc = Env.lookup_term env' acc_name in
+                       let env'' = Env.remove_name env' acc_name in
+                        match aux acc with
+                          | None -> aux_base term
+                          | Some (base_env, rec_envl) -> 
+                              Some (base_env, env'' :: rec_envl)
+                     end
+            in
+               match aux term with
+                | None -> None
+                | Some (base_env, rec_envl) ->
+                     Some (base_env @ Env.coalesce_env p_rec_decls rec_envl @ env)) (* @ env LUCA!!! *)
+
     | Pt.Default (p_some, p_none) ->  (* p_none can't bound names *)
         let p_some_decls = Env.declarations_of_term p_some in
         let none_env = List.map Env.opt_binding_of_name p_some_decls in
         let compiled = compiler [p_some, 0] in
         (fun term env ->
           match compiled term with
-          | None -> Some none_env
+          | None -> Some none_env (* LUCA: @ env ??? *)
           | Some (env', 0) -> Some (List.map Env.opt_binding_some env' @ env)
           | _ -> assert false)
+
+    | Pt.If (p_test, p_true, p_false) ->
+       let compiled_test = compiler [p_test, 0]
+       and compiled_true = compiler [p_true, 0]
+       and compiled_false = compiler [p_false, 0] in
+         (fun term env ->
+            let branch =
+              match compiled_test term with
+                | None -> compiled_false
+                | Some _ -> compiled_true
+            in
+              match branch term with
+                | None -> None
+                | Some (env', _) -> Some (env' @ env))
+
+    | Pt.Fail -> (fun _ _ -> None)
+
     | _ -> assert false
 end
 
@@ -317,7 +356,7 @@ struct
   struct
     type cic_mask_t =
       Blob
-    | Uri of string
+    | Uri of UriManager.uri
     | Appl of cic_mask_t list
 
     let uri_of_term t = CicUtil.uri_of_term (Deannotate.deannotate_term t)
@@ -335,7 +374,8 @@ struct
       Hashtbl.hash mask, tl
 
     let mask_of_appl_pattern = function
-      | Pt.UriPattern s -> Uri s, []
+      | Pt.UriPattern uri -> Uri uri, []
+      | Pt.ImplicitPattern
       | Pt.VarPattern _ -> Blob, []
       | Pt.ApplPattern pl -> Appl (List.map (fun _ -> Blob) pl), pl
 
@@ -347,21 +387,23 @@ struct
     type term_t = Cic.annterm
 
     let classify = function
+      | Pt.ImplicitPattern
       | Pt.VarPattern _ -> Variable
-      | _ -> Constructor
+      | Pt.UriPattern _
+      | Pt.ApplPattern _ -> Constructor
   end
 
   module M = Matcher (Pattern32)
 
   let compiler rows =
     let match_cb rows =
-      prerr_endline (sprintf "match_cb on %d row(s)" (List.length rows));
       let pl, pid = try List.hd rows with Not_found -> assert false in
       (fun matched_terms ->
         let env =
           List.map2
             (fun p t ->
               match p with
+              | Pt.ImplicitPattern -> Util.fresh_name (), t
               | Pt.VarPattern name -> name, t
               | _ -> assert false)
             pl matched_terms