]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_cic_content/ncic2astMatcher.ml
- most of cic/ removed
[helm.git] / matita / components / ng_cic_content / ncic2astMatcher.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id: acic2astMatcher.ml 9271 2008-11-28 18:28:58Z fguidi $ *)
27
28 module Ast = NotationPt
29 module Util = NotationUtil
30
31 let reference_of_oxuri = ref (fun _ -> assert false);;
32 let set_reference_of_oxuri f = reference_of_oxuri := f;;
33
34 module Matcher32 =
35 struct
36   module Pattern32 =
37   struct
38     type cic_mask_t =
39       Blob
40     | NRef of NReference.reference
41     | Appl of cic_mask_t list
42
43     let mask_of_cic = function
44       | NCic.Appl tl -> Appl (List.map (fun _ -> Blob) tl), tl
45       | NCic.Const nref -> NRef nref, []
46       | _ -> Blob, []
47
48     let tag_of_term t =
49       let mask, tl = mask_of_cic t in
50       Hashtbl.hash mask, tl
51
52     let mask_of_appl_pattern = function
53       | Ast.UriPattern uri -> NRef (!reference_of_oxuri uri), []
54       | Ast.NRefPattern nref -> NRef nref, []
55       | Ast.ImplicitPattern
56       | Ast.VarPattern _ -> Blob, []
57       | Ast.ApplPattern pl -> Appl (List.map (fun _ -> Blob) pl), pl
58
59     let tag_of_pattern p =
60       let mask, pl = mask_of_appl_pattern p in
61       Hashtbl.hash mask, pl
62
63     type pattern_t = Ast.cic_appl_pattern
64     type term_t = NCic.term
65
66     let string_of_pattern = NotationPp.pp_cic_appl_pattern
67     let string_of_term t =
68      (*CSC: ??? *)
69      NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t
70
71     let classify = function
72       | Ast.ImplicitPattern
73       | Ast.VarPattern _ -> PatternMatcher.Variable
74       | Ast.UriPattern _
75       | Ast.NRefPattern _
76       | Ast.ApplPattern _ -> PatternMatcher.Constructor
77   end
78
79   module M = PatternMatcher.Matcher (Pattern32)
80
81   let compiler rows =
82     let match_cb rows matched_terms constructors =
83      HExtlib.list_findopt
84       (fun (pl,pid) _ ->
85         let env =
86           try
87             List.map2
88               (fun p t ->
89                 match p with
90                 | Ast.ImplicitPattern -> Util.fresh_name (), t
91                 | Ast.VarPattern name -> name, t
92                 | _ -> assert false)
93               pl matched_terms
94           with Invalid_argument _ -> assert false in
95         let rec check_non_linear_patterns =
96          function
97             [] -> true
98           | (name,t)::tl ->
99              List.for_all
100               (fun (name',t') ->
101                 name <> name' || NCicReduction.alpha_eq [] [] [] t t'
102               ) tl && check_non_linear_patterns tl
103         in
104          if check_non_linear_patterns env then
105           Some (env, constructors, pid)
106          else
107           None
108       ) rows
109     in
110     M.compiler rows match_cb (fun () -> None)
111 end
112