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/
30 module Ast = CicNotationPt
33 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
35 type interpretation_id = int
37 let idref id t = Ast.AttributedTerm (`IdRef id, t)
40 { sort: (Cic.id, Ast.sort_kind) Hashtbl.t;
41 uri: (Cic.id, UriManager.uri) Hashtbl.t;
45 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
47 | Cic.InductiveDefinition (l,_,lpsno,_) -> l, lpsno
50 let name_of_inductive_type uri i =
51 let types, _ = get_types uri in
52 let (name, _, _, _) = try List.nth types i with Not_found -> assert false in
55 (* returns <name, type> pairs *)
56 let constructors_of_inductive_type uri i =
57 let types, _ = get_types uri in
58 let (_, _, _, constructors) =
59 try List.nth types i with Not_found -> assert false
63 (* returns name only *)
64 let constructor_of_inductive_type uri i j =
66 fst (List.nth (constructors_of_inductive_type uri i) (j-1))
67 with Not_found -> assert false)
69 (* returns the number of left parameters *)
70 let left_params_no_of_inductive_type uri =
73 let ast_of_acic0 ~output_type term_info acic k =
74 let k = k term_info in
75 let id_to_uris = term_info.uri in
76 let register_uri id uri = Hashtbl.add id_to_uris id uri in
79 Hashtbl.find term_info.sort id
81 prerr_endline (sprintf "warning: sort of id %s not found, using Type" id);
82 `Type (CicUniv.fresh ())
84 let aux_substs substs =
87 (fun (uri, annterm) -> (UriManager.name_of_uri uri, k annterm))
90 let aux_context context =
94 | Some annterm -> Some (k annterm))
98 | Cic.ARel (id,_,_,b) -> idref id (Ast.Ident (b, None))
99 | Cic.AVar (id,uri,substs) ->
101 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
102 | Cic.AMeta (id,n,l) -> idref id (Ast.Meta (n, aux_context l))
103 | Cic.ASort (id,Cic.Prop) -> idref id (Ast.Sort `Prop)
104 | Cic.ASort (id,Cic.Set) -> idref id (Ast.Sort `Set)
105 | Cic.ASort (id,Cic.Type u) -> idref id (Ast.Sort (`Type u))
106 | Cic.ASort (id,Cic.CProp) -> idref id (Ast.Sort `CProp)
107 | Cic.AImplicit (id, Some `Hole) -> idref id Ast.UserInput
108 | Cic.AImplicit (id, _) -> idref id Ast.Implicit
109 | Cic.AProd (id,n,s,t) ->
111 match sort_of_id id with
112 | `Set | `Type _ -> `Pi
113 | `Prop | `CProp -> `Forall
115 idref id (Ast.Binder (binder_kind,
116 (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
117 | Cic.ACast (id,v,t) -> idref id (Ast.Cast (k v, k t))
118 | Cic.ALambda (id,n,s,t) ->
119 idref id (Ast.Binder (`Lambda,
120 (CicNotationUtil.name_of_cic_name n, Some (k s)), k t))
121 | Cic.ALetIn (id,n,s,ty,t) ->
122 idref id (Ast.LetIn ((CicNotationUtil.name_of_cic_name n, Some (k ty)),
124 | Cic.AAppl (aid,(Cic.AConst _ as he::tl as args))
125 | Cic.AAppl (aid,(Cic.AMutInd _ as he::tl as args))
126 | Cic.AAppl (aid,(Cic.AMutConstruct _ as he::tl as args)) as t ->
133 let (res,len) as res' = aux tl in
139 match fst (aux l) with
142 | Ast.AttributedTerm (_,(Ast.Appl l))::tl ->
143 idref aid (Ast.Appl (l@tl))
144 | l -> idref aid (Ast.Appl l)
146 (match LibraryObjects.destroy_nat t with
147 | Some n -> idref aid (Ast.Num (string_of_int n, -1))
149 let deannot_he = Deannotate.deannotate_term he in
150 if CoercDb.is_a_coercion' deannot_he && !Acic2content.hide_coercions
152 (match CoercDb.is_a_coercion_to_funclass deannot_he with
153 | None -> idref aid (last_n 1 (List.map k tl))
154 | Some i -> idref aid (last_n (i+1) (List.map k tl)))
156 idref aid (Ast.Appl (List.map k args)))
157 | Cic.AAppl (aid,args) ->
158 idref aid (Ast.Appl (List.map k args))
159 | Cic.AConst (id,uri,substs) ->
161 idref id (Ast.Ident (UriManager.name_of_uri uri, aux_substs substs))
162 | Cic.AMutInd (id,uri,i,substs) ->
163 let name = name_of_inductive_type uri i in
164 let uri_str = UriManager.string_of_uri uri in
165 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (i+1) in
166 register_uri id (UriManager.uri_of_string puri_str);
167 idref id (Ast.Ident (name, aux_substs substs))
168 | Cic.AMutConstruct (id,uri,i,j,substs) ->
169 let name = constructor_of_inductive_type uri i j in
170 let uri_str = UriManager.string_of_uri uri in
171 let puri_str = sprintf "%s#xpointer(1/%d/%d)" uri_str (i + 1) j in
172 register_uri id (UriManager.uri_of_string puri_str);
173 idref id (Ast.Ident (name, aux_substs substs))
174 | Cic.AMutCase (id,uri,typeno,ty,te,patterns) ->
175 let name = name_of_inductive_type uri typeno in
176 let uri_str = UriManager.string_of_uri uri in
177 let puri_str = sprintf "%s#xpointer(1/%d)" uri_str (typeno+1) in
179 UriManager.uri_of_string
180 (sprintf "%s#xpointer(1/%d/%d)" uri_str (typeno+1) j)
182 let case_indty = name, Some (UriManager.uri_of_string puri_str) in
183 let constructors = constructors_of_inductive_type uri typeno in
184 let lpsno = left_params_no_of_inductive_type uri in
185 let rec eat_branch n ty pat =
187 | Cic.Prod (_, _, t), _ when n > 0 -> eat_branch (pred n) t pat
188 | Cic.Prod (_, _, t), Cic.ALambda (_, name, s, t') ->
189 let (cv, rhs) = eat_branch 0 t t' in
190 (CicNotationUtil.name_of_cic_name name, Some (k s)) :: cv, rhs
197 (fun (name, ty) pat ->
199 let name,(capture_variables,rhs) =
200 match output_type with
201 `Term -> name, eat_branch lpsno ty pat
202 | `Pattern -> "_", ([], k pat)
204 Ast.Pattern (name, Some (ctor_puri !j), capture_variables), rhs
205 ) constructors patterns
206 with Invalid_argument _ -> assert false
209 match output_type with
211 | `Term -> Some case_indty
213 idref id (Ast.Case (k te, indty, Some (k ty), patterns))
214 | Cic.AFix (id, no, funs) ->
217 (fun (_, n, decr_idx, ty, bo) ->
221 Cic.ALambda (_,name,so,ta) ->
222 let params,rest = aux ta in
223 (CicNotationUtil.name_of_cic_name name,Some (k so))::
233 | n,Cic.AProd (_,_,_,ta) -> eat_pis (n - 1,ta)
235 (* I should do a whd here, but I have no context *)
238 eat_pis ((List.length params),ty)
240 (params,(Ast.Ident (n, None), Some (k ty)), k bo, decr_idx))
245 (match List.nth defs no with
246 | _, (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
248 with Not_found -> assert false
250 idref id (Ast.LetRec (`Inductive, defs, Ast.Ident (name, None)))
251 | Cic.ACoFix (id, no, funs) ->
254 (fun (_, n, ty, bo) ->
258 Cic.ALambda (_,name,so,ta) ->
259 let params,rest = aux ta in
260 (CicNotationUtil.name_of_cic_name name,Some (k so))::
270 | n,Cic.AProd (_,_,_,ta) -> eat_pis (n - 1,ta)
272 (* I should do a whd here, but I have no context *)
275 eat_pis ((List.length params),ty)
277 (params,(Ast.Ident (n, None), Some (k ty)), k bo, 0))
282 (match List.nth defs no with
283 | _, (Ast.Ident (n, _), _), _, _ when n <> "_" -> n
285 with Not_found -> assert false
287 idref id (Ast.LetRec (`CoInductive, defs, Ast.Ident (name, None)))
291 (* persistent state *)
293 let level2_patterns32 = Hashtbl.create 211
294 let interpretations = Hashtbl.create 211 (* symb -> id list ref *)
296 let compiled32 = ref None
297 let pattern32_matrix = ref []
299 let get_compiled32 () =
300 match !compiled32 with
301 | None -> assert false
302 | Some f -> Lazy.force f
304 let set_compiled32 f = compiled32 := Some f
307 List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
309 let instantiate32 term_info idrefs env symbol args =
310 let rec instantiate_arg = function
311 | Ast.IdentArg (n, name) ->
312 let t = (try List.assoc name env with Not_found -> assert false) in
313 let rec count_lambda = function
314 | Ast.AttributedTerm (_, t) -> count_lambda t
315 | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
318 let rec add_lambda t n =
320 let name = CicNotationUtil.fresh_name () in
321 Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
322 Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
326 add_lambda t (n - count_lambda t)
329 let symbol = Ast.Symbol (symbol, 0) in
330 add_idrefs idrefs symbol
332 if args = [] then head
333 else Ast.Appl (head :: List.map instantiate_arg args)
335 let rec ast_of_acic1 ~output_type term_info annterm =
336 let id_to_uris = term_info.uri in
337 let register_uri id uri = Hashtbl.add id_to_uris id uri in
338 match (get_compiled32 ()) annterm with
340 ast_of_acic0 ~output_type term_info annterm (ast_of_acic1 ~output_type)
341 | Some (env, ctors, pid) ->
345 let idref = CicUtil.id_of_annterm annterm in
348 (CicUtil.uri_of_term (Deannotate.deannotate_term annterm))
349 with Invalid_argument _ -> ());
355 (fun (name, term) -> name, ast_of_acic1 ~output_type term_info term) env
357 let _, symbol, args, _ =
359 Hashtbl.find level2_patterns32 pid
360 with Not_found -> assert false
362 let ast = instantiate32 term_info idrefs env' symbol args in
363 Ast.AttributedTerm (`IdRef (CicUtil.id_of_annterm annterm), ast)
365 let load_patterns32 t =
367 HExtlib.filter_map (function (true, ap, id) -> Some (ap, id) | _ -> None) t
369 set_compiled32 (lazy (Acic2astMatcher.Matcher32.compiler t))
371 let ast_of_acic ~output_type id_to_sort annterm =
372 debug_print (lazy ("ast_of_acic <- "
373 ^ CicPp.ppterm (Deannotate.deannotate_term annterm)));
374 let term_info = { sort = id_to_sort; uri = Hashtbl.create 211 } in
375 let ast = ast_of_acic1 ~output_type term_info annterm in
376 debug_print (lazy ("ast_of_acic -> " ^ CicNotationPp.pp_term ast));
379 let counter = ref ~-1
380 let reset () = counter := ~-1;;
386 let add_interpretation dsc (symbol, args) appl_pattern =
387 let id = fresh_id () in
388 Hashtbl.add level2_patterns32 id (dsc, symbol, args, appl_pattern);
389 pattern32_matrix := (true, appl_pattern, id) :: !pattern32_matrix;
390 load_patterns32 !pattern32_matrix;
392 let ids = Hashtbl.find interpretations symbol in
394 with Not_found -> Hashtbl.add interpretations symbol (ref [id]));
397 let get_all_interpretations () =
399 (function (_, _, id) ->
402 Hashtbl.find level2_patterns32 id
403 with Not_found -> assert false
408 let get_active_interpretations () =
409 HExtlib.filter_map (function (true, _, id) -> Some id | _ -> None)
412 let set_active_interpretations ids =
413 let pattern32_matrix' =
416 | (_, ap, id) when List.mem id ids -> (true, ap, id)
417 | (_, ap, id) -> (false, ap, id))
420 pattern32_matrix := pattern32_matrix';
421 load_patterns32 !pattern32_matrix
423 exception Interpretation_not_found
425 let lookup_interpretations symbol =
428 (List.sort Pervasives.compare
431 let (dsc, _, args, appl_pattern) =
433 Hashtbl.find level2_patterns32 id
434 with Not_found -> assert false
436 dsc, args, appl_pattern)
437 !(Hashtbl.find interpretations symbol)))
438 with Not_found -> raise Interpretation_not_found
440 let remove_interpretation id =
442 let _, symbol, _, _ = Hashtbl.find level2_patterns32 id in
443 let ids = Hashtbl.find interpretations symbol in
444 ids := List.filter ((<>) id) !ids;
445 Hashtbl.remove level2_patterns32 id;
446 with Not_found -> raise Interpretation_not_found);
448 List.filter (fun (_, _, id') -> id <> id') !pattern32_matrix;
449 load_patterns32 !pattern32_matrix
451 let _ = load_patterns32 []
453 let instantiate_appl_pattern env appl_pattern =
455 try List.assoc name env
457 prerr_endline (sprintf "Name %s not found" name);
460 let rec aux = function
461 | Ast.UriPattern uri -> CicUtil.term_of_uri uri
462 | Ast.ImplicitPattern -> Cic.Implicit None
463 | Ast.VarPattern name -> lookup name
464 | Ast.ApplPattern terms -> Cic.Appl (List.map aux terms)