]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_content/acic2astMatcher.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / acic_content / acic2astMatcher.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$ *)
27
28 module Ast = CicNotationPt
29 module Util = CicNotationUtil
30
31 module Matcher32 =
32 struct
33   module Pattern32 =
34   struct
35     type cic_mask_t =
36       Blob
37     | Uri of UriManager.uri
38     | NRef of NReference.reference
39     | Appl of cic_mask_t list
40
41     let uri_of_term t = CicUtil.uri_of_term (Deannotate.deannotate_term t)
42
43     let mask_of_cic = function
44       | Cic.AAppl (_, tl) -> Appl (List.map (fun _ -> Blob) tl), tl
45       | Cic.AConst (_, _, [])
46       | Cic.AVar (_, _, [])
47       | Cic.AMutInd (_, _, _, [])
48       | Cic.AMutConstruct (_, _, _, _, []) as t -> Uri (uri_of_term t), []
49       | _ -> Blob, []
50
51     let tag_of_term t =
52       let mask, tl = mask_of_cic t in
53       Hashtbl.hash mask, tl
54
55     let mask_of_appl_pattern = function
56       | Ast.NRefPattern nref -> NRef nref, []
57       | Ast.UriPattern uri -> Uri uri, []
58       | Ast.ImplicitPattern
59       | Ast.VarPattern _ -> Blob, []
60       | Ast.ApplPattern pl -> Appl (List.map (fun _ -> Blob) pl), pl
61
62     let tag_of_pattern p =
63       let mask, pl = mask_of_appl_pattern p in
64       Hashtbl.hash mask, pl
65
66     type pattern_t = Ast.cic_appl_pattern
67     type term_t = Cic.annterm
68
69     let string_of_pattern = CicNotationPp.pp_cic_appl_pattern
70     let string_of_term t = CicPp.ppterm (Deannotate.deannotate_term t)
71
72     let classify = function
73       | Ast.ImplicitPattern
74       | Ast.VarPattern _ -> PatternMatcher.Variable
75       | Ast.UriPattern _
76       | Ast.NRefPattern _
77       | Ast.ApplPattern _ -> PatternMatcher.Constructor
78   end
79
80   module M = PatternMatcher.Matcher (Pattern32)
81
82   let compiler rows =
83     let match_cb rows matched_terms constructors =
84      HExtlib.list_findopt
85       (fun (pl,pid) _ ->
86         let env =
87           try
88             List.map2
89               (fun p t ->
90                 match p with
91                 | Ast.ImplicitPattern -> Util.fresh_name (), t
92                 | Ast.VarPattern name -> name, t
93                 | _ -> assert false)
94               pl matched_terms
95           with Invalid_argument _ -> assert false in
96         let rec check_non_linear_patterns =
97          function
98             [] -> true
99           | (name,t)::tl ->
100              List.for_all
101               (fun (name',t') ->
102                 name <> name' ||
103                 CicUtil.alpha_equivalence
104                  (Deannotate.deannotate_term t) (Deannotate.deannotate_term t')
105               ) tl && check_non_linear_patterns tl
106         in
107          if check_non_linear_patterns env then
108           Some (env, constructors, pid)
109          else
110           None
111       ) rows
112     in
113     M.compiler rows match_cb (fun () -> None)
114 end
115