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