]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/content2presMatcher.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / 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 `JustOne
40   in
41   let rec aux t = 
42     CicNotationUtil.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     let string_of_term = CicNotationPp.pp_term
71     let string_of_pattern = CicNotationPp.pp_term
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 s)
105           | _ -> assert false)
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     M.compiler rows' match_cb (fun _ -> None)
158
159   and compile_magic = function
160     | Ast.Fold (kind, p_base, names, p_rec) ->
161         let p_rec_decls = Env.declarations_of_term p_rec in
162           (* LUCA: p_rec_decls should not contain "names" *)
163         let acc_name = try List.hd names with Failure _ -> assert false in
164         let compiled_base = compiler [p_base, 0]
165         and compiled_rec = compiler [p_rec, 0] in
166           (fun term env ctors ->
167              let aux_base term =
168                match compiled_base term with
169                  | None -> None
170                  | Some (env', ctors', _) -> Some (env', ctors', [])
171              in
172              let rec aux term =
173                match compiled_rec term with
174                  | None -> aux_base term
175                  | Some (env', ctors', _) ->
176                      begin
177                        let acc = Env.lookup_term env' acc_name in
178                        let env'' = Env.remove_name env' acc_name in
179                          match aux acc with
180                            | None -> aux_base term
181                            | Some (base_env, ctors', rec_envl) -> 
182                                let ctors'' = ctors' @ ctors in
183                                Some (base_env, ctors'',env'' :: rec_envl)
184                      end
185              in
186                match aux term with
187                  | None -> None
188                  | Some (base_env, ctors, rec_envl) ->
189                      let env' =
190                        base_env @ Env.coalesce_env p_rec_decls rec_envl @ env
191                        (* @ env LUCA!!! *)
192                      in
193                      Some (env', ctors))
194
195     | Ast.Default (p_some, p_none) ->  (* p_none can't bound names *)
196         let p_some_decls = Env.declarations_of_term p_some in
197         let p_none_decls = Env.declarations_of_term p_none in
198         let p_opt_decls =
199           List.filter
200             (fun decl -> not (List.mem decl p_none_decls))
201             p_some_decls
202         in
203         let none_env = List.map Env.opt_binding_of_name p_opt_decls in
204         let compiled = compiler [p_some, 0] in
205         (fun term env ctors ->
206           match compiled term with
207           | None -> Some (none_env, ctors) (* LUCA: @ env ??? *)
208           | Some (env', ctors', 0) ->
209               let env' =
210                 List.map
211                   (fun (name, (ty, v)) as binding ->
212                     if List.exists (fun (name', _) -> name = name') p_opt_decls
213                     then Env.opt_binding_some binding
214                     else binding)
215                   env'
216               in
217               Some (env' @ env, ctors' @ ctors)
218           | _ -> assert false)
219
220     | Ast.If (p_test, p_true, p_false) ->
221         let compiled_test = compiler [p_test, 0]
222         and compiled_true = compiler [p_true, 0]
223         and compiled_false = compiler [p_false, 0] in
224           (fun term env ctors ->
225              let branch =
226                match compiled_test term with
227                | None -> compiled_false
228                | Some _ -> compiled_true
229              in
230              match branch term with
231              | None -> None
232              | Some (env', ctors', _) -> Some (env' @ env, ctors' @ ctors))
233
234     | Ast.Fail -> (fun _ _ _ -> None)
235
236     | _ -> assert false
237 end
238