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