1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 module Ast = CicNotationPt
31 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
33 type interpretation_id = int
35 let idref id t = Ast.AttributedTerm (`IdRef id, t)
38 { sort: (Cic.id, Ast.sort_kind) Hashtbl.t;
39 uri: (Cic.id, UriManager.uri) Hashtbl.t;
43 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
45 | Cic.InductiveDefinition (l,_,_,_) -> l
48 let name_of_inductive_type uri i =
49 let types = get_types uri in
50 let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
53 (* returns <name, type> pairs *)
54 let constructors_of_inductive_type uri i =
55 let types = get_types uri in
56 let (_, _, _, constructors) =
57 try List.nth types i with Not_found -> assert false
61 (* returns name only *)
62 let constructor_of_inductive_type uri i j =
64 fst (List.nth (constructors_of_inductive_type uri i) (j-1))
65 with Not_found -> assert false)
67 let ast_of_acic0 term_info acic k =
68 let k = k term_info in
69 let id_to_uris = term_info.uri in
70 let register_uri id uri = Hashtbl.add id_to_uris id uri in
73 Hashtbl.find term_info.sort id
75 prerr_endline (sprintf "warning: sort of id %s not found, using Type" id);
76 `Type (CicUniv.fresh ())
78 let aux_substs substs =
81 (fun (uri, annterm) -> (UriManager.name_of_uri uri, k annterm))
84 let aux_context context =
88 | Some annterm -> Some (k annterm))
92 | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
93 | Cic.AVar (id,uri,substs) ->
95 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
96 | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, aux_context l))
97 | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
98 | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
99 | Cic.ASort (id,Cic.Type u) -> idref id (Ast.Sort (`Type u))
100 | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
101 | Cic.AImplicit (id, Some `Hole) -> idref id Ast.UserInput
102 | Cic.AImplicit (id, _) -> idref id Ast.Implicit
103 | Cic.AProd (id,n,s,t) ->
105 match sort_of_id id with
106 | `Set | `Type _ -> `Pi
107 | `Prop | `CProp -> `Forall
109 idref id (Ast.Binder (binder_kind,
110 (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
111 | Cic.ACast (id,v,t) -> idref id (Ast.Cast (k v, k t))
112 | Cic.ALambda (id,n,s,t) ->
113 idref id (Ast.Binder (`Lambda,
114 (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
115 | Cic.ALetIn (id,n,s,t) ->
116 idref id (Ast.LetIn ((CicNotationUtil.name_of_cic_name n, None),
118 | Cic.AAppl (aid,args) -> idref aid (Ast.Appl (List.map k args))
119 | Cic.AConst (id,uri,substs) ->
121 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
122 | Cic.AMutInd (id,uri,i,substs) ->
123 let name = name_of_inductive_type uri i in
124 let uri_str = UriManager.string_of_uri uri in
125 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (i+1) in
126 register_uri id (UriManager.uri_of_string puri_str);
127 idref id (Ast.Ident (name, aux_substs substs))
128 | Cic.AMutConstruct (id,uri,i,j,substs) ->
129 let name = constructor_of_inductive_type uri i j in
130 let uri_str = UriManager.string_of_uri uri in
131 let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
132 register_uri id (UriManager.uri_of_string puri_str);
133 idref id (Ast.Ident (name, aux_substs substs))
134 | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
135 let name = name_of_inductive_type uri typeno in
136 let uri_str = UriManager.string_of_uri uri in
137 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
139 UriManager.uri_of_string
140 (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
142 let case_indty = name, Some (UriManager.uri_of_string puri_str) in
143 let constructors = constructors_of_inductive_type uri typeno in
144 let rec eat_branch ty pat =
146 | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
147 let (cv, rhs) = eat_branch t t' in
148 (CicNotationUtil.name_of_cic_name name, Some (k s)) :: cv, rhs
155 (fun (name, ty) pat ->
157 let (capture_variables, rhs) = eat_branch ty pat in
158 ((name, Some (ctor_puri !j), capture_variables), rhs))
159 constructors patterns
160 with Invalid_argument _ -> assert false
162 idref id (Ast.Case (k te, Some case_indty, Some (k ty), patterns))
163 | Cic.AFix (id, no, funs) ->
166 (fun (_, n, decr_idx, ty, bo) ->
167 ((Ast.Ident (n, None), Some (k ty)), k bo, decr_idx))
172 (match List.nth defs no with
173 | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
175 with Not_found -> assert false
177 idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
178 | Cic.ACoFix (id, no, funs) ->
181 (fun (_, n, ty, bo) ->
182 ((Ast.Ident (n, None), Some (k ty)), k bo, 0))
187 (match List.nth defs no with
188 | (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
190 with Not_found -> assert false
192 idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
196 (* persistent state *)
198 let level2_patterns32 = Hashtbl.create 211
199 let interpretations = Hashtbl.create 211 (* symb -> id list ref *)
201 let compiled32 = ref None
202 let pattern32_matrix = ref []
204 let get_compiled32 () =
205 match !compiled32 with
206 | None -> assert false
207 | Some f -> Lazy.force f
209 let set_compiled32 f = compiled32 := Some f
212 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
214 let instantiate32 term_info idrefs env symbol args =
215 let rec instantiate_arg = function
216 | Ast.IdentArg (n, name) ->
217 let t = (try List.assoc name env with Not_found -> assert false) in
218 let rec count_lambda = function
219 | Ast.AttributedTerm (_, t) -> count_lambda t
220 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
223 let rec add_lambda t n =
225 let name = CicNotationUtil.fresh_name () in
226 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
227 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
231 add_lambda t (n - count_lambda t)
234 let symbol = Ast.Symbol (symbol, 0) in
235 add_idrefs idrefs symbol
237 if args = [] then head
238 else Ast.Appl (head :: List.map instantiate_arg args)
240 let rec ast_of_acic1 term_info annterm =
241 let id_to_uris = term_info.uri in
242 let register_uri id uri = Hashtbl.add id_to_uris id uri in
243 match (get_compiled32 ()) annterm with
244 | None -> ast_of_acic0 term_info annterm ast_of_acic1
245 | Some (env, ctors, pid) ->
249 let idref = CicUtil.id_of_annterm annterm in
252 (CicUtil.uri_of_term (Deannotate.deannotate_term annterm))
253 with Invalid_argument _ -> ());
258 List.map (fun (name, term) -> (name, ast_of_acic1 term_info term)) env
260 let _, symbol, args, _ =
262 Hashtbl.find level2_patterns32 pid
263 with Not_found -> assert false
265 let ast = instantiate32 term_info idrefs env' symbol args in
266 Ast.AttributedTerm (`IdRef (CicUtil.id_of_annterm annterm), ast)
268 let load_patterns32 t =
270 HExtlib.filter_map (function (true, ap, id) -> Some (ap, id) | _ -> None) t
272 set_compiled32 (lazy (Acic2astMatcher.Matcher32.compiler t))
274 let ast_of_acic id_to_sort annterm =
275 debug_print (lazy ("ast_of_acic <- "
276 ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
277 let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
278 let ast = ast_of_acic1 term_info annterm in
279 debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
283 let counter = ref ~-1 in
288 let add_interpretation dsc (symbol, args) appl_pattern =
289 let id = fresh_id () in
290 Hashtbl.add level2_patterns32 id (dsc, symbol, args, appl_pattern);
291 pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
292 load_patterns32 !pattern32_matrix;
294 let ids = Hashtbl.find interpretations symbol in
296 with Not_found -> Hashtbl.add interpretations symbol (ref [id]));
299 let get_all_interpretations () =
301 (function (_, _, id) ->
304 Hashtbl.find level2_patterns32 id
305 with Not_found -> assert false
310 let get_active_interpretations () =
311 HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
314 let set_active_interpretations ids =
315 let pattern32_matrix' =
318 | (_, ap, id) when List.mem id ids -> (true, ap, id)
319 | (_, ap, id) -> (false, ap, id))
322 pattern32_matrix := pattern32_matrix';
323 load_patterns32 !pattern32_matrix
325 exception Interpretation_not_found
327 let lookup_interpretations symbol =
330 (List.sort Pervasives.compare
333 let (dsc, _, args, appl_pattern) =
335 Hashtbl.find level2_patterns32 id
336 with Not_found -> assert false
338 dsc, args, appl_pattern)
339 !(Hashtbl.find interpretations symbol)))
340 with Not_found -> raise Interpretation_not_found
342 let remove_interpretation id =
344 let _, symbol, _, _ = Hashtbl.find level2_patterns32 id in
345 let ids = Hashtbl.find interpretations symbol in
346 ids := List.filter ((<>) id) !ids;
347 Hashtbl.remove level2_patterns32 id;
348 with Not_found -> raise Interpretation_not_found);
350 List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
351 load_patterns32 !pattern32_matrix
353 let _ = load_patterns32 []
355 let instantiate_appl_pattern env appl_pattern =
357 try List.assoc name env
359 prerr_endline (sprintf "Name %s not found" name);
362 let rec aux = function
363 | Ast.UriPattern uri -> CicUtil.term_of_uri uri
364 | Ast.ImplicitPattern -> Cic.Implicit None
365 | Ast.VarPattern name -> lookup name
366 | Ast.ApplPattern terms -> Cic.Appl (List.map aux terms)