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