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