]> matita.cs.unibo.it Git - helm.git/blob - matita/components/content_pres/content2presMatcher.ml
ee62e06e696da3bac0573f286da335d3a6cc188a
[helm.git] / matita / components / content_pres / content2presMatcher.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 open Printf
29
30 module Ast = NotationPt
31 module Env = NotationEnv
32 module Pp = NotationPp
33 module Util = NotationUtil
34
35 let get_tag term0 =
36   let subterms = ref [] in
37   let map_term t =
38     subterms := t :: !subterms ; 
39     Ast.Implicit `JustOne
40   in
41   let rec aux t = 
42     NotationUtil.visit_ast 
43       ~map_xref_option:(fun _ -> None)
44       ~map_case_indty:(fun _ -> None)
45       ~map_case_outtype:(fun _ _ -> None)
46       ~special_k map_term t
47   and special_k = function
48     | Ast.AttributedTerm (_, t) -> aux t
49     | _ -> assert false
50   in
51   let term_mask = aux term0 in
52   let tag = Hashtbl.hash term_mask in
53   tag, List.rev !subterms
54
55 module Matcher21 =
56 struct
57   module Pattern21 =
58   struct
59     type pattern_t = Ast.term
60     type term_t = Ast.term
61     let rec classify = function
62       | Ast.AttributedTerm (_, t) -> classify t
63       | Ast.Variable _ -> PatternMatcher.Variable
64       | Ast.Magic _
65       | Ast.Layout _
66       | Ast.Literal _ -> assert false
67       | _ -> PatternMatcher.Constructor
68     let tag_of_pattern = get_tag
69     let tag_of_term t = get_tag t
70
71     (* Debugging only *)
72     (*CSC: new NCicPp.status is the best I can do now *)
73     let string_of_term = NotationPp.pp_term (new NCicPp.status)
74     let string_of_pattern = NotationPp.pp_term (new NCicPp.status)
75   end
76
77   module M = PatternMatcher.Matcher (Pattern21)
78
79   let extract_magic term =
80     let magic_map = ref [] in
81     let add_magic m =
82       let name = Util.fresh_name () in
83       magic_map := (name, m) :: !magic_map;
84       Ast.Variable (Ast.TermVar (name,Ast.Level 0))
85     in
86     let rec aux = function
87       | Ast.AttributedTerm (_, t) -> assert false
88       | Ast.Literal _
89       | Ast.Layout _ -> assert false
90       | Ast.Variable v -> Ast.Variable v
91       | Ast.Magic m -> add_magic m
92       | t -> Util.visit_ast aux t
93     in
94     let term' = aux term in
95     term', !magic_map
96
97   let env_of_matched pl tl =
98     try
99       List.map2
100         (fun p t ->
101           match p, t with
102           | Ast.Variable (Ast.TermVar (name,(Ast.Self l|Ast.Level l))), _ ->
103               name, (Env.TermType l, Env.TermValue t)
104           | Ast.Variable (Ast.NumVar name), (Ast.Num (s, _)) ->
105               name, (Env.NumType, Env.NumValue s)
106           | Ast.Variable (Ast.IdentVar name), (Ast.Ident (s, None)) ->
107               name, (Env.StringType, Env.StringValue (Env.Ident s))
108           | _ -> assert false (* activate the DEBUGGING CODE below *))
109         pl tl
110     with Invalid_argument _ -> assert false
111
112   let rec compiler rows =
113     let rows', magic_maps =
114       List.split
115         (List.map
116           (fun (p, pid) ->
117             let p', map = extract_magic p in
118             (p', pid), (pid, map))
119           rows)
120     in
121     let magichecker map =
122       List.fold_left
123         (fun f (name, m) ->
124           let m_checker = compile_magic m in
125           (fun env ctors ->
126             match m_checker (Env.lookup_term env name) env ctors with
127             | None -> None
128             | Some (env, ctors) -> f env ctors))
129         (fun env ctors -> Some (env, ctors))
130         map
131     in
132     let magichooser candidates =
133       List.fold_left
134         (fun f (pid, pl, checker) ->
135           (fun matched_terms constructors ->
136             let env = env_of_matched pl matched_terms in
137             match checker env constructors with
138             | None -> f matched_terms constructors
139             | Some (env, ctors') ->
140                 let magic_map =
141                   try List.assoc pid magic_maps with Not_found -> assert false
142                 in
143                 let env' = Env.remove_names env (List.map fst magic_map) in
144                 Some (env', ctors', pid)))
145         (fun _ _ -> None)
146         (List.rev candidates)
147     in
148     let match_cb rows =
149       let candidates =
150         List.map
151           (fun (pl, pid) ->
152             let magic_map =
153               try List.assoc pid magic_maps with Not_found -> assert false
154             in
155             pid, pl, magichecker magic_map)
156           rows
157       in
158       magichooser candidates
159     in
160 (* DEBUGGING CODE 
161 fun input ->
162 let (fst,_)::_ = rows in
163 prerr_endline ("RIGA: " ^ NotationPp.pp_term (new NCicPp.status) fst);
164 prerr_endline ("CONTRO: " ^ NotationPp.pp_term (new NCicPp.status) input);
165 *)
166     M.compiler rows' match_cb (fun _ -> None)
167 (* DEBUGGING CODE 
168 input
169 *)
170
171   and compile_magic = function
172     | Ast.Fold (kind, p_base, names, p_rec) ->
173         let p_rec_decls = Env.declarations_of_term p_rec in
174           (* LUCA: p_rec_decls should not contain "names" *)
175         let acc_name = try List.hd names with Failure _ -> assert false in
176         let compiled_base = compiler [p_base, 0]
177         and compiled_rec = compiler [p_rec, 0] in
178           (fun term env ctors ->
179              let aux_base term =
180                match compiled_base term with
181                  | None -> None
182                  | Some (env', ctors', _) -> Some (env', ctors', [])
183              in
184              let rec aux term =
185                match compiled_rec term with
186                  | None -> aux_base term
187                  | Some (env', ctors', _) ->
188                      begin
189                        let acc = Env.lookup_term env' acc_name in
190                        let env'' = Env.remove_name env' acc_name in
191                          match aux acc with
192                            | None -> aux_base term
193                            | Some (base_env, ctors', rec_envl) -> 
194                                let ctors'' = ctors' @ ctors in
195                                Some (base_env, ctors'',env'' :: rec_envl)
196                      end
197              in
198                match aux term with
199                  | None -> None
200                  | Some (base_env, ctors, rec_envl) ->
201                      let env' =
202                        base_env @ Env.coalesce_env p_rec_decls rec_envl @ env
203                        (* @ env LUCA!!! *)
204                      in
205                      Some (env', ctors))
206
207     | Ast.Default (p_some, p_none) ->  (* p_none can't bound names *)
208         let p_some_decls = Env.declarations_of_term p_some in
209         let p_none_decls = Env.declarations_of_term p_none in
210         let p_opt_decls =
211           List.filter
212             (fun decl -> not (List.mem decl p_none_decls))
213             p_some_decls
214         in
215         let none_env = List.map Env.opt_binding_of_name p_opt_decls in
216         let compiled = compiler [p_some, 0] in
217         (fun term env ctors ->
218           match compiled term with
219           | None -> Some (none_env, ctors) (* LUCA: @ env ??? *)
220           | Some (env', ctors', 0) ->
221               let env' =
222                 List.map
223                   (fun (name, (ty, v)) as binding ->
224                     if List.exists (fun (name', _) -> name = name') p_opt_decls
225                     then Env.opt_binding_some binding
226                     else binding)
227                   env'
228               in
229               Some (env' @ env, ctors' @ ctors)
230           | _ -> assert false)
231
232     | Ast.If (p_test, p_true, p_false) ->
233         let compiled_test = compiler [p_test, 0]
234         and compiled_true = compiler [p_true, 0]
235         and compiled_false = compiler [p_false, 0] in
236           (fun term env ctors ->
237              let branch =
238                match compiled_test term with
239                | None -> compiled_false
240                | Some _ -> compiled_true
241              in
242              match branch term with
243              | None -> None
244              | Some (env', ctors', _) -> Some (env' @ env, ctors' @ ctors))
245
246     | Ast.Fail -> (fun _ _ _ -> None)
247
248     | _ -> assert false
249 end
250