]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/cic_disambiguation/cicDisambiguate.ml
e52db62ddf293312f8707cf8cbde58a40528485f
[helm.git] / helm / software / components / cic_disambiguation / cicDisambiguate.ml
1 (*
2 Copyright (C) 1999-2006, HELM Team.
3
4 This file is part of HELM, an Hypertextual, Electronic
5 Library of Mathematics, developed at the Computer Science
6 Department, University of Bologna, Italy.
7
8 HELM is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 HELM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301 USA.
22
23 For details, see the HELM web site: http://helm.cs.unibo.it/
24 *)
25
26 open Printf
27 module Ast = CicNotationPt
28
29 let debug = false
30 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
31
32 let rec string_context_of_context =
33  List.map
34   (function
35    | Cic.Name n -> Some n
36    | Cic.Anonymous -> Some "_")
37 ;;
38
39 let refine_term metasenv subst context uri ~use_coercions
40  term expty ugraph ~localization_tbl =
41 (*   if benchmark then incr actual_refinements; *)
42   assert (uri=None);
43   debug_print (lazy (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppterm term)));
44   let saved_use_coercions = !CicRefine.insert_coercions in
45   try
46     CicRefine.insert_coercions := use_coercions;
47     let term = 
48       match expty with
49       | None -> term
50       | Some ty -> Cic.Cast(term,ty)
51     in
52     let term', _, metasenv',ugraph1 = 
53             CicRefine.type_of_aux' metasenv context term ugraph ~localization_tbl
54     in
55     let term' = 
56       match expty, term' with
57       | None,_ -> term'
58       | Some _,Cic.Cast (term',_) -> term'
59       | _ -> assert false 
60     in
61      CicRefine.insert_coercions := saved_use_coercions;
62      (Disambiguate.Ok (term', metasenv',[],ugraph1))
63   with
64    exn ->
65     CicRefine.insert_coercions := saved_use_coercions;
66     let rec process_exn loc =
67      function
68         HExtlib.Localized (loc,exn) -> process_exn loc exn
69       | CicRefine.Uncertain msg ->
70           debug_print (lazy ("UNCERTAIN!!! [" ^ (Lazy.force msg) ^ "] " ^ CicPp.ppterm term)) ;
71           Disambiguate.Uncertain (lazy (loc,Lazy.force msg))
72       | CicRefine.RefineFailure msg ->
73           debug_print (lazy (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
74             (CicPp.ppterm term) (Lazy.force msg)));
75           Disambiguate.Ko (lazy (loc,Lazy.force msg))
76      | exn -> raise exn
77     in
78      process_exn Stdpp.dummy_loc exn
79
80 let refine_obj metasenv subst context uri ~use_coercions obj _ ugraph
81  ~localization_tbl =
82    assert (context = []);
83    assert (metasenv = []);
84    assert (subst = []);
85    debug_print (lazy (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppobj obj))) ;
86    let saved_use_coercions = !CicRefine.insert_coercions in
87    try
88      CicRefine.insert_coercions := use_coercions;
89      let obj', metasenv,ugraph =
90        CicRefine.typecheck metasenv uri obj ~localization_tbl
91      in
92       CicRefine.insert_coercions := saved_use_coercions;
93       (Disambiguate.Ok (obj', metasenv,[],ugraph))
94    with
95      exn ->
96       CicRefine.insert_coercions := saved_use_coercions;
97       let rec process_exn loc =
98        function
99           HExtlib.Localized (loc,exn) -> process_exn loc exn
100         | CicRefine.Uncertain msg ->
101             debug_print (lazy ("UNCERTAIN!!! [" ^ 
102               (Lazy.force msg) ^ "] " ^ CicPp.ppobj obj)) ;
103             Disambiguate.Uncertain (lazy (loc,Lazy.force msg))
104         | CicRefine.RefineFailure msg ->
105             debug_print (lazy (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
106               (CicPp.ppobj obj) (Lazy.force msg))) ;
107             Disambiguate.Ko (lazy (loc,Lazy.force msg))
108        | exn -> raise exn
109       in
110        process_exn Stdpp.dummy_loc exn
111 ;;
112
113 let interpretate_term ~mk_choice ?(create_dummy_ids=false) ~context ~env ~uri
114  ~is_path ast ~localization_tbl ~obj_context
115 =
116   (* create_dummy_ids shouldbe used only for interpretating patterns *)
117   assert (uri = None);
118   let rec aux ~localize loc context = function
119     | CicNotationPt.AttributedTerm (`Loc loc, term) ->
120         let res = aux ~localize loc context term in
121          if localize then Cic.CicHash.add localization_tbl res loc;
122          res
123     | CicNotationPt.AttributedTerm (_, term) -> aux ~localize loc context term
124     | CicNotationPt.Appl (CicNotationPt.Symbol (symb, i) :: args) ->
125         let cic_args = List.map (aux ~localize loc context) args in
126         Disambiguate.resolve ~mk_choice ~env 
127           (DisambiguateTypes.Symbol (symb, i)) (`Args cic_args)
128     | CicNotationPt.Appl terms ->
129        Cic.Appl (List.map (aux ~localize loc context) terms)
130     | CicNotationPt.Binder (binder_kind, (var, typ), body) ->
131         let cic_type = aux_option ~localize loc context (Some `Type) typ in
132         let cic_name = CicNotationUtil.cic_name_of_name var in
133         let cic_body = aux ~localize loc (cic_name :: context) body in
134         (match binder_kind with
135         | `Lambda -> Cic.Lambda (cic_name, cic_type, cic_body)
136         | `Pi
137         | `Forall -> Cic.Prod (cic_name, cic_type, cic_body)
138         | `Exists ->
139             Disambiguate.resolve ~mk_choice ~env
140              (DisambiguateTypes.Symbol ("exists", 0))
141              (`Args [ cic_type; Cic.Lambda (cic_name, cic_type, cic_body) ]))
142     | CicNotationPt.Case (term, indty_ident, outtype, branches) ->
143         let cic_term = aux ~localize loc context term in
144         let cic_outtype = aux_option ~localize loc context None outtype in
145         let do_branch ((head, _, args), term) =
146          let rec do_branch' context = function
147            | [] -> aux ~localize loc context term
148            | (name, typ) :: tl ->
149                let cic_name = CicNotationUtil.cic_name_of_name name in
150                let cic_body = do_branch' (cic_name :: context) tl in
151                let typ =
152                  match typ with
153                  | None -> Cic.Implicit (Some `Type)
154                  | Some typ -> aux ~localize loc context typ
155                in
156                Cic.Lambda (cic_name, typ, cic_body)
157          in
158           do_branch' context args
159         in
160         let indtype_uri, indtype_no =
161           if create_dummy_ids then
162             (UriManager.uri_of_string "cic:/fake_indty.con", 0)
163           else
164           match indty_ident with
165           | Some (indty_ident, _) ->
166              (match 
167                Disambiguate.resolve ~mk_choice ~env 
168                 (DisambiguateTypes.Id indty_ident) (`Args [])
169               with
170               | Cic.MutInd (uri, tyno, _) -> (uri, tyno)
171               | Cic.Implicit _ ->
172                  raise (Disambiguate.Try_again 
173                    (lazy "The type of the term to be matched
174                   is still unknown"))
175               | _ ->
176                 raise (DisambiguateTypes.Invalid_choice (lazy (loc,"The type of the term to be matched is not (co)inductive!"))))
177           | None ->
178               let rec fst_constructor =
179                 function
180                    (Ast.Pattern (head, _, _), _) :: _ -> head
181                  | (Ast.Wildcard, _) :: tl -> fst_constructor tl
182                  | [] -> raise (DisambiguateTypes.Invalid_choice (lazy (loc,"The type of the term to be matched cannot be determined because it is an inductive type without constructors or because all patterns use wildcards")))
183               in
184               (match Disambiguate.resolve ~mk_choice ~env
185                 (DisambiguateTypes.Id (fst_constructor branches))
186                 (`Args []) with
187               | Cic.MutConstruct (indtype_uri, indtype_no, _, _) ->
188                   (indtype_uri, indtype_no)
189               | Cic.Implicit _ ->
190                  raise (Disambiguate.Try_again (lazy "The type of the term to be matched
191                   is still unknown"))
192               | _ ->
193                 raise (DisambiguateTypes.Invalid_choice (lazy (loc,"The type of the term to be matched is not (co)inductive!"))))
194         in
195         let branches =
196          if create_dummy_ids then
197           List.map
198            (function
199                Ast.Wildcard,term -> ("wildcard",None,[]), term
200              | Ast.Pattern _,_ ->
201                 raise (DisambiguateTypes.Invalid_choice (lazy (loc, "Syntax error: the left hand side of a branch patterns must be \"_\"")))
202            ) branches
203          else
204          match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph indtype_uri) with
205             Cic.InductiveDefinition (il,_,leftsno,_) ->
206              let _,_,_,cl =
207               try
208                List.nth il indtype_no
209               with _ -> assert false
210              in
211               let rec count_prod t =
212                 match CicReduction.whd [] t with
213                     Cic.Prod (_, _, t) -> 1 + (count_prod t)
214                   | _ -> 0 
215               in 
216               let rec sort branches cl =
217                match cl with
218                   [] ->
219                    let rec analyze unused unrecognized useless =
220                     function
221                        [] ->
222                         if unrecognized != [] then
223                          raise (DisambiguateTypes.Invalid_choice
224                           (lazy (loc,
225                             ("Unrecognized constructors: " ^
226                              String.concat " " unrecognized))))
227                         else if useless > 0 then
228                          raise (DisambiguateTypes.Invalid_choice
229                            (lazy (loc,
230                             ("The last " ^ string_of_int useless ^
231                              "case" ^ if useless > 1 then "s are" else " is" ^
232                              " unused"))))
233                         else
234                          []
235                      | (Ast.Wildcard,_)::tl when not unused ->
236                          analyze true unrecognized useless tl
237                      | (Ast.Pattern (head,_,_),_)::tl when not unused ->
238                          analyze unused (head::unrecognized) useless tl
239                      | _::tl -> analyze unused unrecognized (useless + 1) tl
240                    in
241                     analyze false [] 0 branches
242                 | (name,ty)::cltl ->
243                    let rec find_and_remove =
244                     function
245                        [] ->
246                         raise
247                          (DisambiguateTypes.Invalid_choice
248                           (lazy (loc, ("Missing case: " ^ name))))
249                      | ((Ast.Wildcard, _) as branch :: _) as branches ->
250                          branch, branches
251                      | (Ast.Pattern (name',_,_),_) as branch :: tl
252                         when name = name' ->
253                          branch,tl
254                      | branch::tl ->
255                         let found,rest = find_and_remove tl in
256                          found, branch::rest
257                    in
258                     let branch,tl = find_and_remove branches in
259                     match branch with
260                        Ast.Pattern (name,y,args),term ->
261                         if List.length args = count_prod ty - leftsno then
262                          ((name,y,args),term)::sort tl cltl
263                         else
264                          raise
265                           (DisambiguateTypes.Invalid_choice
266                             (lazy (loc,"Wrong number of arguments for " ^
267                             name)))
268                      | Ast.Wildcard,term ->
269                         let rec mk_lambdas =
270                          function
271                             0 -> term
272                           | n ->
273                              CicNotationPt.Binder
274                               (`Lambda, (CicNotationPt.Ident ("_", None), None),
275                                 mk_lambdas (n - 1))
276                         in
277                          (("wildcard",None,[]),
278                           mk_lambdas (count_prod ty - leftsno)) :: sort tl cltl
279               in
280                sort branches cl
281           | _ -> assert false
282         in
283         Cic.MutCase (indtype_uri, indtype_no, cic_outtype, cic_term,
284           (List.map do_branch branches))
285     | CicNotationPt.Cast (t1, t2) ->
286         let cic_t1 = aux ~localize loc context t1 in
287         let cic_t2 = aux ~localize loc context t2 in
288         Cic.Cast (cic_t1, cic_t2)
289     | CicNotationPt.LetIn ((name, typ), def, body) ->
290         let cic_def = aux ~localize loc context def in
291         let cic_name = CicNotationUtil.cic_name_of_name name in
292         let cic_typ =
293           match typ with
294           | None -> Cic.Implicit (Some `Type)
295           | Some t -> aux ~localize loc context t
296         in
297         let cic_body = aux ~localize loc (cic_name :: context) body in
298         Cic.LetIn (cic_name, cic_def, cic_typ, cic_body)
299     | CicNotationPt.LetRec (kind, defs, body) ->
300         let context' =
301           List.fold_left
302             (fun acc (_, (name, _), _, _) ->
303               CicNotationUtil.cic_name_of_name name :: acc)
304             context defs
305         in
306         let cic_body =
307          let unlocalized_body = aux ~localize:false loc context' body in
308          match unlocalized_body with
309             Cic.Rel n when n <= List.length defs -> `AvoidLetInNoAppl n
310           | Cic.Appl (Cic.Rel n::l) when n <= List.length defs ->
311              (try
312                let l' =
313                 List.map
314                  (function t ->
315                    let t',subst,metasenv =
316                     CicMetaSubst.delift_rels [] [] (List.length defs) t
317                    in
318                     assert (subst=[]);
319                     assert (metasenv=[]);
320                     t') l
321                in
322                 (* We can avoid the LetIn. But maybe we need to recompute l'
323                    so that it is localized *)
324                 if localize then
325                  match body with
326                     CicNotationPt.AttributedTerm (_,CicNotationPt.Appl(_::l)) ->
327                      (* since we avoid the letin, the context has no
328                       * recfuns in it *)
329                      let l' = List.map (aux ~localize loc context) l in
330                       `AvoidLetIn (n,l')
331                   | _ -> assert false
332                 else
333                  `AvoidLetIn (n,l')
334               with
335                CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
336                 if localize then
337                  `AddLetIn (aux ~localize loc context' body)
338                 else
339                  `AddLetIn unlocalized_body)
340           | _ ->
341              if localize then
342               `AddLetIn (aux ~localize loc context' body)
343              else
344               `AddLetIn unlocalized_body
345         in
346         let inductiveFuns =
347           List.map
348             (fun (params, (name, typ), body, decr_idx) ->
349               let add_binders kind t =
350                List.fold_right
351                 (fun var t -> CicNotationPt.Binder (kind, var, t)) params t
352               in
353               let cic_body =
354                aux ~localize loc context' (add_binders `Lambda body) in
355               let typ =
356                match typ with
357                   Some typ -> typ
358                 | None-> CicNotationPt.Implicit `JustOne in
359               let cic_type =
360                aux_option ~localize loc context (Some `Type)
361                 (Some (add_binders `Pi typ)) in
362               let name =
363                 match CicNotationUtil.cic_name_of_name name with
364                 | Cic.Anonymous ->
365                     CicNotationPt.fail loc
366                       "Recursive functions cannot be anonymous"
367                 | Cic.Name name -> name
368               in
369               (name, decr_idx, cic_type, cic_body))
370             defs
371         in
372         let fix_or_cofix n =
373          match kind with
374             `Inductive -> Cic.Fix (n,inductiveFuns)
375           | `CoInductive ->
376               let coinductiveFuns =
377                 List.map
378                  (fun (name, _, typ, body) -> name, typ, body)
379                  inductiveFuns
380               in
381                Cic.CoFix (n,coinductiveFuns)
382         in
383          let counter = ref ~-1 in
384          let build_term funs (var,_,ty,_) t =
385           incr counter;
386           Cic.LetIn (Cic.Name var, fix_or_cofix !counter, ty, t)
387          in
388           (match cic_body with
389               `AvoidLetInNoAppl n ->
390                 let n' = List.length inductiveFuns - n in
391                  fix_or_cofix n'
392             | `AvoidLetIn (n,l) ->
393                 let n' = List.length inductiveFuns - n in
394                  Cic.Appl (fix_or_cofix n'::l)
395             | `AddLetIn cic_body ->         
396                 List.fold_right (build_term inductiveFuns) inductiveFuns
397                  cic_body)
398     | CicNotationPt.Ident _
399     | CicNotationPt.Uri _
400     | CicNotationPt.NRef _ when is_path -> raise Disambiguate.PathNotWellFormed
401     | CicNotationPt.NRef _ -> assert false
402     | CicNotationPt.Ident (name,subst)
403     | CicNotationPt.Uri (name, subst) as ast ->
404         let is_uri = function CicNotationPt.Uri _ -> true | _ -> false in
405         (try
406           if is_uri ast then raise Not_found;(* don't search the env for URIs *)
407           let index =
408            Disambiguate.find_in_context name (string_context_of_context context)
409           in
410           if subst <> None then
411             CicNotationPt.fail loc "Explicit substitutions not allowed here";
412           Cic.Rel index
413         with Not_found ->
414           let cic =
415             if is_uri ast then  (* we have the URI, build the term out of it *)
416               try
417                 CicUtil.term_of_uri (UriManager.uri_of_string name)
418               with UriManager.IllFormedUri _ ->
419                 CicNotationPt.fail loc "Ill formed URI"
420             else
421              try
422               List.assoc name obj_context
423              with
424               Not_found ->
425                Disambiguate.resolve ~mk_choice ~env
426                 (DisambiguateTypes.Id name) (`Args [])
427           in
428           let mk_subst uris =
429             let ids_to_uris =
430               List.map (fun uri -> UriManager.name_of_uri uri, uri) uris
431             in
432             (match subst with
433             | Some subst ->
434                 List.map
435                   (fun (s, term) ->
436                     (try
437                       List.assoc s ids_to_uris, aux ~localize loc context term
438                      with Not_found ->
439                        raise (DisambiguateTypes.Invalid_choice (lazy (loc, "The provided explicit named substitution is trying to instantiate a named variable the object is not abstracted on")))))
440                   subst
441             | None -> List.map (fun uri -> uri, Cic.Implicit None) uris)
442           in
443           (try 
444             match cic with
445             | Cic.Const (uri, []) ->
446                 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
447                 let uris = CicUtil.params_of_obj o in
448                 Cic.Const (uri, mk_subst uris)
449             | Cic.Var (uri, []) ->
450                 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
451                 let uris = CicUtil.params_of_obj o in
452                 Cic.Var (uri, mk_subst uris)
453             | Cic.MutInd (uri, i, []) ->
454                (try
455                  let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
456                  let uris = CicUtil.params_of_obj o in
457                  Cic.MutInd (uri, i, mk_subst uris)
458                 with
459                  CicEnvironment.Object_not_found _ ->
460                   (* if we are here it is probably the case that during the
461                      definition of a mutual inductive type we have met an
462                      occurrence of the type in one of its constructors.
463                      However, the inductive type is not yet in the environment
464                   *)
465                   (*here the explicit_named_substituion is assumed to be of length 0 *)
466                   Cic.MutInd (uri,i,[]))
467             | Cic.MutConstruct (uri, i, j, []) ->
468                 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
469                 let uris = CicUtil.params_of_obj o in
470                 Cic.MutConstruct (uri, i, j, mk_subst uris)
471             | Cic.Meta _ | Cic.Implicit _ as t ->
472 (*
473                 debug_print (lazy (sprintf
474                   "Warning: %s must be instantiated with _[%s] but we do not enforce it"
475                   (CicPp.ppterm t)
476                   (String.concat "; "
477                     (List.map
478                       (fun (s, term) -> s ^ " := " ^ CicNotationPtPp.pp_term term)
479                       subst))));
480 *)
481                 t
482             | _ ->
483               raise (DisambiguateTypes.Invalid_choice (lazy (loc, "??? Can this happen?")))
484            with 
485              CicEnvironment.CircularDependency _ -> 
486                raise (DisambiguateTypes.Invalid_choice (lazy (loc,"Circular dependency in the environment")))))
487     | CicNotationPt.Implicit `Vector -> assert false
488     | CicNotationPt.Implicit `JustOne -> Cic.Implicit None
489     | CicNotationPt.UserInput -> Cic.Implicit (Some `Hole)
490     | CicNotationPt.Num (num, i) ->
491        Disambiguate.resolve ~mk_choice ~env
492         (DisambiguateTypes.Num i) (`Num_arg num)
493     | CicNotationPt.Meta (index, subst) ->
494         let cic_subst =
495           List.map
496             (function
497                 None -> None
498               | Some term -> Some (aux ~localize loc context term))
499             subst
500         in
501         Cic.Meta (index, cic_subst)
502     | CicNotationPt.Sort `Prop -> Cic.Sort Cic.Prop
503     | CicNotationPt.Sort `Set -> Cic.Sort Cic.Set
504     | CicNotationPt.Sort (`Type u) -> Cic.Sort (Cic.Type u)
505     | CicNotationPt.Sort (`NType _) -> Cic.Sort (Cic.Type (CicUniv.fresh ()))
506     | CicNotationPt.Sort (`NCProp _) -> Cic.Sort (Cic.CProp (CicUniv.fresh ()))
507     | CicNotationPt.Sort (`CProp u) -> Cic.Sort (Cic.CProp u)
508     | CicNotationPt.Symbol (symbol, instance) ->
509         Disambiguate.resolve ~mk_choice ~env
510          (DisambiguateTypes.Symbol (symbol, instance)) (`Args [])
511     | CicNotationPt.Variable _
512     | CicNotationPt.Magic _
513     | CicNotationPt.Layout _
514     | CicNotationPt.Literal _ -> assert false (* god bless Bologna *)
515   and aux_option ~localize loc context annotation = function
516     | None -> Cic.Implicit annotation
517     | Some term -> aux ~localize loc context term
518   in
519    aux ~localize:true HExtlib.dummy_floc context ast
520
521 let interpretate_path ~context path =
522  let localization_tbl = Cic.CicHash.create 23 in
523   (* here we are throwing away useful localization informations!!! *)
524   fst (
525    interpretate_term ~mk_choice:(fun _ -> assert false) ~create_dummy_ids:true 
526     ~context ~env:DisambiguateTypes.Environment.empty ~uri:None ~is_path:true
527     path ~localization_tbl ~obj_context:[], localization_tbl)
528
529 let interpretate_obj ~mk_choice ~context ~env ~uri ~is_path obj
530  ~localization_tbl
531 =
532  assert (context = []);
533  assert (is_path = false);
534  let interpretate_term ?(obj_context=[]) =
535   interpretate_term ~mk_choice ~localization_tbl ~obj_context in
536  match obj with
537   | CicNotationPt.Inductive (params,tyl) ->
538      let uri = match uri with Some uri -> uri | None -> assert false in
539      let context,params =
540       let context,res =
541        List.fold_left
542         (fun (context,res) (name,t) ->
543           let t =
544            match t with
545               None -> CicNotationPt.Implicit `JustOne
546             | Some t -> t in
547           let name = CicNotationUtil.cic_name_of_name name in
548            name::context,(name, interpretate_term context env None false t)::res
549         ) ([],[]) params
550       in
551        context,List.rev res in
552      let add_params =
553       List.fold_right (fun (name,ty) t -> Cic.Prod (name,ty,t)) params in
554      let obj_context =
555       snd (
556        List.fold_left
557         (*here the explicit_named_substituion is assumed to be of length 0 *)
558         (fun (i,res) (name,_,_,_) -> i + 1,(name,Cic.MutInd (uri,i,[]))::res)
559         (0,[]) tyl) in
560      let tyl =
561       List.map
562        (fun (name,b,ty,cl) ->
563          let ty' = add_params (interpretate_term context env None false ty) in
564          let cl' =
565           List.map
566            (fun (name,ty) ->
567              let ty' =
568               add_params
569                (interpretate_term ~obj_context ~context ~env ~uri:None
570                  ~is_path:false ty)
571              in
572               name,ty'
573            ) cl
574          in
575           name,b,ty',cl'
576        ) tyl
577      in
578       Cic.InductiveDefinition (tyl,[],List.length params,[])
579   | CicNotationPt.Record (params,name,ty,fields) ->
580      let uri = match uri with Some uri -> uri | None -> assert false in
581      let context,params =
582       let context,res =
583        List.fold_left
584         (fun (context,res) (name,t) ->
585           let t =
586            match t with
587               None -> CicNotationPt.Implicit `JustOne
588             | Some t -> t in
589           let name = CicNotationUtil.cic_name_of_name name in
590            name::context,(name, interpretate_term context env None false t)::res
591         ) ([],[]) params
592       in
593        context,List.rev res in
594      let add_params =
595       List.fold_right
596        (fun (name,ty) t -> Cic.Prod (name,ty,t)) params in
597      let ty' = add_params (interpretate_term context env None false ty) in
598      let fields' =
599       snd (
600        List.fold_left
601         (fun (context,res) (name,ty,_coercion,arity) ->
602           let context' = Cic.Name name :: context in
603            context',(name,interpretate_term context env None false ty)::res
604         ) (context,[]) fields) in
605      let concl =
606       (*here the explicit_named_substituion is assumed to be of length 0 *)
607       let mutind = Cic.MutInd (uri,0,[]) in
608       if params = [] then mutind
609       else
610        Cic.Appl
611         (mutind::CicUtil.mk_rels (List.length params) (List.length fields)) in
612      let con =
613       List.fold_left
614        (fun t (name,ty) -> Cic.Prod (Cic.Name name,ty,t))
615        concl fields' in
616      let con' = add_params con in
617      let tyl = [name,true,ty',["mk_" ^ name,con']] in
618      let field_names = List.map (fun (x,_,y,z) -> x,y,z) fields in
619       Cic.InductiveDefinition
620        (tyl,[],List.length params,[`Class (`Record field_names)])
621   | CicNotationPt.Theorem (flavour, name, ty, bo) ->
622      let attrs = [`Flavour flavour] in
623      let ty' = interpretate_term [] env None false ty in
624      (match bo,flavour with
625         None,`Axiom ->
626          Cic.Constant (name,None,ty',[],attrs)
627       | Some bo,`Axiom -> assert false
628       | None,_ ->
629          Cic.CurrentProof (name,[],Cic.Implicit None,ty',[],attrs)
630       | Some bo,_ ->
631          let bo' = Some (interpretate_term [] env None false bo) in
632           Cic.Constant (name,bo',ty',[],attrs))
633 ;;
634
635 let interpretate_term ~mk_choice ?(create_dummy_ids=false) ~context ~env ~uri
636  ~is_path ast ~localization_tbl
637 =
638   let context =
639    List.map (function None -> Cic.Anonymous | Some (n,_) -> n) context
640   in
641    interpretate_term ~mk_choice ~create_dummy_ids ~context ~env ~uri ~is_path
642     ast ~localization_tbl ~obj_context:[]
643 ;;
644
645 let string_context_of_context =
646   List.map (function None -> None | Some (Cic.Name n,_) -> Some n | Some
647   (Cic.Anonymous,_) -> Some "_");;
648
649 let disambiguate_term ~context ~metasenv ~subst ~expty 
650   ?(initial_ugraph = CicUniv.oblivion_ugraph)
651   ~mk_implicit ~description_of_alias ~mk_choice
652   ~aliases ~universe ~lookup_in_library (text,prefix_len,term)
653 =
654   let mk_localization_tbl x = Cic.CicHash.create x in
655    MultiPassDisambiguator.disambiguate_thing ~context ~metasenv ~subst
656     ~initial_ugraph ~aliases ~string_context_of_context
657     ~universe ~lookup_in_library ~mk_implicit ~description_of_alias
658     ~uri:None ~pp_thing:CicNotationPp.pp_term
659     ~domain_of_thing:Disambiguate.domain_of_term
660     ~interpretate_thing:(interpretate_term (?create_dummy_ids:None) ~mk_choice)
661     ~refine_thing:refine_term (text,prefix_len,term)
662     ~mk_localization_tbl
663     ~expty
664     ~freshen_thing:CicNotationUtil.freshen_term
665     ~passes:(MultiPassDisambiguator.passes ())
666
667 let disambiguate_obj ~mk_implicit ~description_of_alias ~mk_choice
668  ~aliases ~universe ~lookup_in_library ~uri (text,prefix_len,obj)
669 =
670   let mk_localization_tbl x = Cic.CicHash.create x in
671   MultiPassDisambiguator.disambiguate_thing ~context:[] ~metasenv:[] ~subst:[] 
672     ~aliases ~universe ~uri ~string_context_of_context
673     ~pp_thing:(CicNotationPp.pp_obj CicNotationPp.pp_term)
674     ~domain_of_thing:Disambiguate.domain_of_obj
675     ~lookup_in_library ~mk_implicit ~description_of_alias
676     ~initial_ugraph:CicUniv.empty_ugraph
677     ~interpretate_thing:(interpretate_obj ~mk_choice)
678     ~refine_thing:refine_obj
679     ~mk_localization_tbl
680     ~expty:None
681     ~passes:(MultiPassDisambiguator.passes ())
682     ~freshen_thing:CicNotationUtil.freshen_obj
683     (text,prefix_len,obj)