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