]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_disambiguation/nCicDisambiguate.ml
Use of standard OCaml syntax
[helm.git] / matita / components / ng_disambiguation / nCicDisambiguate.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 module P = Printf
15 module DT =  DisambiguateTypes
16 module Ast = NotationPt
17 module NRef = NReference 
18
19 let debug = ref false;;
20 let debug_print s = if !debug then prerr_endline (Lazy.force s);;
21
22 let cic_name_of_name = function
23   | Ast.Ident (n, None) ->  n
24   | _ -> assert false
25 ;;
26
27 let rec mk_rels howmany from =
28   match howmany with 
29   | 0 -> []
30   | _ -> (NCic.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
31 ;;
32
33 let refine_term (status: #NCicCoercion.status) metasenv subst context uri ~use_coercions term expty _
34  ~localization_tbl
35 =
36   assert (uri=None);
37   debug_print (lazy (P.sprintf "TEST_INTERPRETATION: %s" 
38     (status#ppterm ~metasenv ~subst ~context term)));
39   try
40     let localise t = 
41       try NCicUntrusted.NCicHash.find localization_tbl t
42       with Not_found -> 
43         prerr_endline ("NOT LOCALISED" ^ status#ppterm ~metasenv ~subst ~context t);
44         (*assert false*) HExtlib.dummy_floc
45     in
46     let metasenv, subst, term, _ = 
47       NCicRefiner.typeof 
48         (status#set_coerc_db 
49           (if use_coercions then status#coerc_db else NCicCoercion.empty_db))
50         metasenv subst context term expty ~localise 
51     in
52      Disambiguate.Ok (term, metasenv, subst, ())
53   with
54   | NCicRefiner.Uncertain loc_msg ->
55       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
56         status#ppterm ~metasenv ~subst ~context term)) ;
57       Disambiguate.Uncertain loc_msg
58   | NCicRefiner.RefineFailure loc_msg ->
59       debug_print (lazy (P.sprintf "PRUNED:\nterm%s\nmessage:%s"
60         (status#ppterm ~metasenv ~subst ~context term) (snd(Lazy.force loc_msg))));
61       Disambiguate.Ko loc_msg
62 ;;
63
64 let refine_obj status metasenv subst _context _uri ~use_coercions obj _ _ugraph
65  ~localization_tbl 
66 =
67   (*prerr_endline ((P.sprintf "TEST_INTERPRETATION: %s" (status#ppobj obj)));*)
68   assert (metasenv=[]);
69   assert (subst=[]);
70   let localise t = 
71     try NCicUntrusted.NCicHash.find localization_tbl t
72     with Not_found -> 
73       (*assert false*)HExtlib.dummy_floc
74   in
75   try
76     let obj =
77       NCicRefiner.typeof_obj
78         (status#set_coerc_db
79            (if use_coercions then status#coerc_db 
80             else NCicCoercion.empty_db))
81         obj ~localise 
82     in
83       Disambiguate.Ok (obj, [], [], ())
84   with
85   | NCicRefiner.Uncertain loc_msg ->
86       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
87         status#ppobj obj)) ;
88       Disambiguate.Uncertain loc_msg
89   | NCicRefiner.RefineFailure loc_msg ->
90       debug_print (lazy (P.sprintf "PRUNED:\nobj: %s\nmessage: %s"
91         (status#ppobj obj) (snd(Lazy.force loc_msg))));
92       Disambiguate.Ko loc_msg
93 ;;
94   
95
96   (* TODO move it to Cic *)
97 let find_in_context name context =
98   let rec aux acc = function
99     | [] -> raise Not_found
100     | hd :: _ when hd = name -> acc
101     | _ :: tl ->  aux (acc + 1) tl
102   in
103   aux 1 context
104
105 let interpretate_term_and_interpretate_term_option (status: #NCic.status)
106   ~create_dummy_ids ~obj_context ~mk_choice ~env ~uri ~is_path
107   ~localization_tbl 
108 =
109   (* create_dummy_ids shouldbe used only for interpretating patterns *)
110   assert (uri = None);
111
112   let rec aux ~localize loc context = function
113     | NotationPt.AttributedTerm (`Loc loc, term) ->
114         let res = aux ~localize loc context term in
115         if localize then 
116          NCicUntrusted.NCicHash.add localization_tbl res loc;
117        res
118     | NotationPt.AttributedTerm (_, term) -> aux ~localize loc context term
119     | NotationPt.Appl 
120         (NotationPt.AttributedTerm (att, inner)::args)->
121         aux ~localize loc context 
122           (NotationPt.AttributedTerm (att,NotationPt.Appl (inner :: args)))
123     | NotationPt.Appl (NotationPt.Appl inner :: args) ->
124         aux ~localize loc context (NotationPt.Appl (inner @ args))
125     | NotationPt.Appl (NotationPt.Symbol (symb, i) :: args) ->
126         let cic_args = List.map (aux ~localize loc context) args in
127         Disambiguate.resolve ~mk_choice ~env (DT.Symbol (symb, i)) (`Args cic_args)
128     | NotationPt.Appl terms ->
129        NCic.Appl (List.map (aux ~localize loc context) terms)
130     | NotationPt.Binder (binder_kind, (var, typ), body) ->
131         let cic_type = aux_option ~localize loc context `Type typ in
132         let cic_name = 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 -> NCic.Lambda (cic_name, cic_type, cic_body)
136         | `Pi
137         | `Forall -> NCic.Prod (cic_name, cic_type, cic_body)
138         | `Exists ->
139             Disambiguate.resolve ~env ~mk_choice (DT.Symbol ("exists", 0))
140               (`Args [ cic_type; NCic.Lambda (cic_name, cic_type, cic_body) ]))
141     | NotationPt.Case (term, indty_ident, outtype, branches) ->
142         let cic_term = aux ~localize loc context term in
143         let cic_outtype = aux_option ~localize loc context `Term outtype in
144         let do_branch ((_, _, args), term) =
145          let rec do_branch' context = function
146            | [] -> aux ~localize loc context term
147            | (name, typ) :: tl ->
148                let cic_name = cic_name_of_name name in
149                let cic_body = do_branch' (cic_name :: context) tl in
150                let typ =
151                  match typ with
152                  | None -> NCic.Implicit `Type
153                  | Some typ -> aux ~localize loc context typ
154                in
155                NCic.Lambda (cic_name, typ, cic_body)
156          in
157           do_branch' context args
158         in
159         if create_dummy_ids then
160          let branches =
161           List.map
162            (function
163                Ast.Wildcard,term -> ("wildcard",None,[]), term
164              | Ast.Pattern _,_ ->
165                 raise (DisambiguateTypes.Invalid_choice 
166                  (lazy (loc, "Syntax error: the left hand side of a "^
167                    "branch pattern must be \"_\"")))
168            ) branches in
169          let indtype_ref =
170           let uri = NUri.uri_of_string "cic:/matita/dummy/indty.ind" in 
171           NRef.reference_of_spec uri (NRef.Ind (true,1,1))
172          in
173           NCic.Match (indtype_ref, cic_outtype, cic_term,
174            (List.map do_branch branches))
175         else
176          let indtype_ref =
177           match indty_ident with
178           | Some (indty_ident, _) ->
179              (match Disambiguate.resolve ~env ~mk_choice 
180                 (DT.Id indty_ident) (`Args []) with
181               | NCic.Const (NRef.Ref (_,NRef.Ind _) as r) -> r
182               | NCic.Implicit _ ->
183                  raise (Disambiguate.Try_again 
184                   (lazy "The type of the term to be matched is still unknown"))
185               | t ->
186                 raise (DisambiguateTypes.Invalid_choice 
187                   (lazy (loc,"The type of the term to be matched "^
188                           "is not (co)inductive: " ^ status#ppterm 
189                           ~metasenv:[] ~subst:[] ~context:[] t))))
190           | None ->
191               let rec fst_constructor =
192                 function
193                    (Ast.Pattern (head, _, _), _) :: _ -> head
194                  | (Ast.Wildcard, _) :: tl -> fst_constructor tl
195                  | [] -> raise (DT.Invalid_choice (lazy (loc,"The type "^
196                      "of the term to be matched cannot be determined "^
197                      "because it is an inductive type without constructors "^
198                      "or because all patterns use wildcards")))
199               in
200 (*
201               DisambiguateTypes.Environment.iter
202                   (fun k v ->
203                       prerr_endline
204                         (DisambiguateTypes.string_of_domain_item k ^ " => " ^
205                         description_of_alias v)) env; 
206 *)
207               (match Disambiguate.resolve ~env ~mk_choice
208                 (DT.Id (fst_constructor branches)) (`Args []) with
209               | NCic.Const (NRef.Ref (_,NRef.Con _) as r) -> 
210                    let b,_,_,_,_= NCicEnvironment.get_checked_indtys status r in
211                    NRef.mk_indty b r
212               | NCic.Implicit _ ->
213                  raise (Disambiguate.Try_again 
214                   (lazy "The type of the term to be matched is still unknown"))
215               | t ->
216                 raise (DisambiguateTypes.Invalid_choice 
217                   (lazy (loc, 
218                   "The type of the term to be matched is not (co)inductive: " 
219                   ^ status#ppterm ~metasenv:[] ~subst:[] ~context:[] t))))
220          in
221          let _,leftsno,itl,_,indtyp_no =
222           NCicEnvironment.get_checked_indtys status indtype_ref in
223          let _,_,_,cl =
224           try
225            List.nth itl indtyp_no
226           with _ -> assert false in
227          let rec count_prod t =
228                  match NCicReduction.whd status ~subst:[] [] t with
229                NCic.Prod (_, _, t) -> 1 + (count_prod t)
230              | _ -> 0 
231          in 
232          let rec sort branches cl =
233           match cl with
234              [] ->
235               let rec analyze unused unrecognized useless =
236                function
237                   [] ->
238                    if unrecognized != [] then
239                     raise (DisambiguateTypes.Invalid_choice
240                      (lazy
241                        (loc,"Unrecognized constructors: " ^
242                         String.concat " " unrecognized)))
243                    else if useless > 0 then
244                     raise (DisambiguateTypes.Invalid_choice
245                      (lazy
246                        (loc,"The last " ^ string_of_int useless ^
247                         "case" ^ if useless > 1 then "s are" else " is" ^
248                         " unused")))
249                    else
250                     []
251                 | (Ast.Wildcard,_)::tl when not unused ->
252                     analyze true unrecognized useless tl
253                 | (Ast.Pattern (head,_,_),_)::tl when not unused ->
254                     analyze unused (head::unrecognized) useless tl
255                 | _::tl -> analyze unused unrecognized (useless + 1) tl
256               in
257                analyze false [] 0 branches
258            | (_,name,ty)::cltl ->
259               let rec find_and_remove =
260                function
261                   [] ->
262                    raise
263                     (DisambiguateTypes.Invalid_choice
264                      (lazy (loc, "Missing case: " ^ name)))
265                 | ((Ast.Wildcard, _) as branch :: _) as branches ->
266                     branch, branches
267                 | (Ast.Pattern (name',_,_),_) as branch :: tl
268                    when name = name' ->
269                     branch,tl
270                 | branch::tl ->
271                    let found,rest = find_and_remove tl in
272                     found, branch::rest
273               in
274                let branch,tl = find_and_remove branches in
275                match branch with
276                   Ast.Pattern (name,y,args),term ->
277                    if List.length args = count_prod ty - leftsno then
278                     ((name,y,args),term)::sort tl cltl
279                    else
280                     raise
281                      (DisambiguateTypes.Invalid_choice
282                       (lazy (loc,"Wrong number of arguments for " ^ name)))
283                 | Ast.Wildcard,term ->
284                    let rec mk_lambdas =
285                     function
286                        0 -> term
287                      | n ->
288                         NotationPt.Binder
289                          (`Lambda, (NotationPt.Ident ("_", None), None),
290                            mk_lambdas (n - 1))
291                    in
292                     (("wildcard",None,[]),
293                      mk_lambdas (count_prod ty - leftsno)) :: sort tl cltl
294          in
295           let branches = sort branches cl in
296            NCic.Match (indtype_ref, cic_outtype, cic_term,
297             (List.map do_branch branches))
298     | NotationPt.Cast (t1, t2) ->
299         let cic_t1 = aux ~localize loc context t1 in
300         let cic_t2 = aux ~localize loc context t2 in
301         NCic.LetIn ("_",cic_t2,cic_t1, NCic.Rel 1)
302     | NotationPt.LetIn ((name, typ), def, body) ->
303         let cic_def = aux ~localize loc context def in
304         let cic_name = cic_name_of_name name in
305         let cic_typ =
306           match typ with
307           | None -> NCic.Implicit `Type
308           | Some t -> aux ~localize loc context t
309         in
310         let cic_body = aux ~localize loc (cic_name :: context) body in
311         NCic.LetIn (cic_name, cic_typ, cic_def, cic_body)
312     | NotationPt.Ident _
313     | NotationPt.Uri _
314     | NotationPt.NRef _ when is_path -> raise Disambiguate.PathNotWellFormed
315     | NotationPt.Ident (name, subst) ->
316        assert (subst = None);
317        (try
318              NCic.Rel (find_in_context name context)
319        with Not_found -> 
320          try NCic.Const (List.assoc name obj_context)
321          with Not_found ->
322             Disambiguate.resolve ~env ~mk_choice (DT.Id name) (`Args []))
323     | NotationPt.Uri (uri, subst) ->
324        assert (subst = None);
325        (try
326          NCic.Const (NRef.reference_of_string uri)
327         with NRef.IllFormedReference _ ->
328          NotationPt.fail loc "Ill formed reference")
329     | NotationPt.NRef nref -> NCic.Const nref
330     | NotationPt.NCic t ->  t
331     | NotationPt.Implicit `Vector -> NCic.Implicit `Vector
332     | NotationPt.Implicit `JustOne -> NCic.Implicit `Term
333     | NotationPt.Implicit (`Tagged s) -> NCic.Implicit (`Tagged s)
334     | NotationPt.UserInput -> NCic.Implicit `Hole
335     | NotationPt.Num (num, i) -> 
336         Disambiguate.resolve ~env ~mk_choice (DT.Num i) (`Num_arg num)
337     | NotationPt.Meta (index, subst) ->
338         let cic_subst =
339          List.map
340           (function None -> assert false| Some t -> aux ~localize loc context t)
341           subst
342         in
343          NCic.Meta (index, (0, NCic.Ctx cic_subst))
344     | NotationPt.Sort `Prop -> NCic.Sort NCic.Prop
345     | NotationPt.Sort `Set -> NCic.Sort (NCic.Type
346        [`Type,NUri.uri_of_string "cic:/matita/pts/Type.univ"])
347     | NotationPt.Sort (`NType s) -> NCic.Sort (NCic.Type
348        [`Type,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
349     | NotationPt.Sort (`NCProp s) -> NCic.Sort (NCic.Type
350        [`CProp,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
351     | NotationPt.Symbol (symbol, instance) ->
352         Disambiguate.resolve ~env ~mk_choice 
353          (DT.Symbol (symbol, instance)) (`Args [])
354     | NotationPt.Variable _
355     | NotationPt.Magic _
356     | NotationPt.Layout _
357     | NotationPt.Literal _ -> assert false (* god bless Bologna *)
358   and aux_option ~localize loc context annotation = function
359     | None -> NCic.Implicit annotation
360     | Some (NotationPt.AttributedTerm (`Loc loc, term)) ->
361         let res = aux_option ~localize loc context annotation (Some term) in
362         if localize then 
363           NCicUntrusted.NCicHash.add localization_tbl res loc;
364         res
365     | Some (NotationPt.AttributedTerm (_, term)) ->
366         aux_option ~localize loc context annotation (Some term)
367     | Some NotationPt.Implicit `JustOne -> NCic.Implicit annotation
368     | Some NotationPt.Implicit `Vector -> NCic.Implicit `Vector
369     | Some term -> aux ~localize loc context term
370   in
371    (fun ~context -> aux ~localize:true HExtlib.dummy_floc context),
372    (fun ~context -> aux_option ~localize:true HExtlib.dummy_floc context)
373 ;;
374
375 let interpretate_term status ?(create_dummy_ids=false) ~context ~env ~uri
376  ~is_path ast ~obj_context ~localization_tbl ~mk_choice
377 =
378   let context = List.map fst context in
379   fst 
380     (interpretate_term_and_interpretate_term_option status
381       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path
382       ~localization_tbl)
383     ~context ast
384 ;;
385
386 let interpretate_term_option status ?(create_dummy_ids=false) ~context ~env ~uri
387  ~is_path ~localization_tbl ~mk_choice ~obj_context
388 =
389   let context = List.map fst context in
390   snd 
391     (interpretate_term_and_interpretate_term_option status
392       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path
393       ~localization_tbl)
394     ~context 
395 ;;
396
397 let disambiguate_path status path =
398   let localization_tbl = NCicUntrusted.NCicHash.create 23 in
399   fst
400     (interpretate_term_and_interpretate_term_option status
401     ~obj_context:[] ~mk_choice:(fun _ -> assert false)
402     ~create_dummy_ids:true ~env:DisambiguateTypes.Environment.empty
403     ~uri:None ~is_path:true ~localization_tbl) ~context:[] path
404 ;;
405
406 let ncic_name_of_ident = function
407   | Ast.Ident (name, None) -> name
408   | _ -> assert false
409 ;;
410
411 let interpretate_obj status
412 (*      ?(create_dummy_ids=false)  *)
413      ~context ~env ~uri ~is_path obj ~localization_tbl ~mk_choice 
414 =
415  assert (context = []);
416  assert (is_path = false);
417  let interpretate_term ~obj_context =
418   interpretate_term ~mk_choice ~localization_tbl ~obj_context in
419  let interpretate_term_option ~obj_context =
420    interpretate_term_option ~mk_choice ~localization_tbl ~obj_context in
421  let uri = match uri with | None -> assert false | Some u -> u in
422  match obj with
423  | NotationPt.Theorem (name, ty, bo, attrs) ->
424      let _, flavour, _ = attrs in
425      let ty' = 
426        interpretate_term status
427          ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false ty 
428      in
429      let height = (* XXX calculate *) 0 in
430      uri, height, [], [], 
431      (match bo,flavour with
432       | None,`Axiom -> 
433           NCic.Constant ([],name,None,ty',attrs)
434       | Some _,`Axiom -> assert false
435       | None,_ ->
436           NCic.Constant ([],name,Some (NCic.Implicit `Term),ty',attrs)
437       | Some bo,_ ->
438           let bo = 
439             interpretate_term status
440              ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false bo
441           in
442           NCic.Constant ([],name,Some bo,ty',attrs))
443  | NotationPt.Inductive (params,tyl,source) ->
444     let context,params =
445      let context,res =
446       List.fold_left
447        (fun (context,res) (name,t) ->
448          let t =
449           match t with
450              None -> NotationPt.Implicit `JustOne
451            | Some t -> t in
452          let name = cic_name_of_name name in
453          let t =
454           interpretate_term status ~obj_context:[] ~context ~env ~uri:None
455            ~is_path:false t
456          in
457           (name,NCic.Decl t)::context,(name,t)::res
458        ) ([],[]) params
459      in
460       context,List.rev res in
461     let add_params =
462      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
463     let leftno = List.length params in
464     let _,inductive,_,_ = try List.hd tyl with Failure _ -> assert false in
465     let obj_context =
466      snd (
467       List.fold_left
468        (fun (i,res) (name,_,_,_) ->
469          let nref =
470           NRef.reference_of_spec uri (NRef.Ind (inductive,i,leftno))
471          in
472           i+1,(name,nref)::res)
473        (0,[]) tyl) in
474     let tyl =
475      List.map
476       (fun (name,_,ty,cl) ->
477         let ty' =
478          add_params
479          (interpretate_term status ~obj_context:[] ~context ~env ~uri:None
480            ~is_path:false ty) in
481         let cl' =
482          List.map
483           (fun (name,ty) ->
484             let ty' =
485              add_params
486               (interpretate_term status ~obj_context ~context ~env ~uri:None
487                 ~is_path:false ty) in
488             let relevance = [] in
489              relevance,name,ty'
490           ) cl in
491         let relevance = [] in
492          relevance,name,ty',cl'
493       ) tyl
494     in
495      let height = (* XXX calculate *) 0 in
496      let attrs = source, `Regular in
497      uri, height, [], [], 
498      NCic.Inductive (inductive,leftno,tyl,attrs)
499  | NotationPt.Record (params,name,ty,fields,source) ->
500     let context,params =
501      let context,res =
502       List.fold_left
503        (fun (context,res) (name,t) ->
504          let t =
505           match t with
506              None -> NotationPt.Implicit `JustOne
507            | Some t -> t in
508          let name = cic_name_of_name name in
509          let t =
510           interpretate_term status ~obj_context:[] ~context ~env ~uri:None
511            ~is_path:false t
512          in
513           (name,NCic.Decl t)::context,(name,t)::res
514        ) ([],[]) params
515      in
516       context,List.rev res in
517     let add_params =
518      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
519     let leftno = List.length params in
520     let ty' =
521      add_params
522       (interpretate_term status ~obj_context:[] ~context ~env ~uri:None
523         ~is_path:false ty) in
524     let nref =
525      NRef.reference_of_spec uri (NRef.Ind (true,0,leftno)) in
526     let obj_context = [name,nref] in
527     let fields' =
528      snd (
529       List.fold_left
530        (fun (context,res) (name,ty,_coercion,_arity) ->
531          let ty =
532           interpretate_term status ~obj_context ~context ~env ~uri:None
533            ~is_path:false ty in
534          let context' = (name,NCic.Decl ty)::context in
535           context',(name,ty)::res
536        ) (context,[]) fields) in
537     let concl =
538      let mutind = NCic.Const nref in
539      if params = [] then mutind
540      else
541       NCic.Appl
542        (mutind::mk_rels (List.length params) (List.length fields)) in
543     let con =
544      List.fold_left (fun t (name,ty) -> NCic.Prod (name,ty,t)) concl fields' in
545     let con' = add_params con in
546     let relevance = [] in
547     let tyl = [relevance,name,ty',[relevance,"mk_" ^ name,con']] in
548     let field_names = List.map (fun (x,_,y,z) -> x,y,z) fields in
549      let height = (* XXX calculate *) 0 in
550      let attrs = source, `Record field_names in
551      uri, height, [], [], 
552      NCic.Inductive (true,leftno,tyl,attrs)
553  | NotationPt.LetRec (kind, defs, attrs) ->
554      let inductive = kind = `Inductive in
555      let _,obj_context =
556        List.fold_left
557          (fun (i,acc) (_,(name,_),_,k) -> 
558           (i+1, 
559             (ncic_name_of_ident name, NRef.reference_of_spec uri 
560              (if inductive then NRef.Fix (i,k,0)
561               else NRef.CoFix i)) :: acc))
562          (0,[]) defs
563      in
564      let inductiveFuns =
565        List.map
566          (fun (params, (name, typ), body, decr_idx) ->
567            let add_binders kind t =
568             List.fold_right
569              (fun var t -> 
570                 NotationPt.Binder (kind, var, t)) params t
571            in
572            let cic_body =
573              interpretate_term status
574                ~obj_context ~context ~env ~uri:None ~is_path:false
575                (add_binders `Lambda body) 
576            in
577            let cic_type =
578              interpretate_term_option status
579                ~obj_context:[]
580                ~context ~env ~uri:None ~is_path:false `Type
581                (HExtlib.map_option (add_binders `Pi) typ)
582            in
583            ([],ncic_name_of_ident name, decr_idx, cic_type, cic_body))
584          defs
585      in
586      let height = (* XXX calculate *) 0 in
587      uri, height, [], [], 
588      NCic.Fixpoint (inductive,inductiveFuns,attrs)
589 ;;
590
591 let disambiguate_term (status: #NCicCoercion.status) ~context ~metasenv ~subst
592  ~expty ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
593  ~aliases ~universe ~lookup_in_library (text,prefix_len,term) 
594 =
595   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
596    let res,b =
597     MultiPassDisambiguator.disambiguate_thing
598      ~freshen_thing:NotationUtil.freshen_term
599      ~context ~metasenv ~initial_ugraph:() ~aliases
600      ~mk_implicit ~description_of_alias ~fix_instance
601      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
602      ~universe ~uri:None ~pp_thing:(NotationPp.pp_term status)
603      ~passes:(MultiPassDisambiguator.passes ())
604      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_term
605      ~interpretate_thing:(interpretate_term status ~obj_context:[] ~mk_choice ?create_dummy_ids:None)
606      ~refine_thing:(refine_term status) (text,prefix_len,term)
607      ~mk_localization_tbl ~expty ~subst
608    in
609     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
610 ;;
611
612 let disambiguate_obj (status: #NCicCoercion.status)
613    ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
614    ~aliases ~universe ~lookup_in_library ~uri (text,prefix_len,obj) 
615  =
616   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
617    let res,b =
618     MultiPassDisambiguator.disambiguate_thing
619      ~freshen_thing:NotationUtil.freshen_obj
620      ~context:[] ~metasenv:[] ~subst:[] ~initial_ugraph:() ~aliases
621      ~mk_implicit ~description_of_alias ~fix_instance
622      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
623      ~universe 
624      ~uri:(Some uri)
625      ~pp_thing:(NotationPp.pp_obj (NotationPp.pp_term status))
626      ~passes:(MultiPassDisambiguator.passes ())
627      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_obj
628      ~interpretate_thing:(interpretate_obj status ~mk_choice)
629      ~refine_thing:(refine_obj status) 
630      (text,prefix_len,obj)
631      ~mk_localization_tbl ~expty:`XTNone
632    in
633     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
634 ;;
635 (*
636 let _ = 
637 let mk_type n = 
638   if n = 0 then
639      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
640   else
641      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
642 in
643 let mk_cprop n = 
644   if n = 0 then 
645     [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
646   else
647     [false, NUri.uri_of_string ("cic:/matita/pts/CProp"^string_of_int n^".univ")]
648 in
649          NCicEnvironment.add_constraint true (mk_type 0) (mk_type 1);
650          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_cprop 1);
651          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_type 1);
652          NCicEnvironment.add_constraint true (mk_type 0) (mk_cprop 1);
653          NCicEnvironment.add_constraint false (mk_cprop 0) (mk_type 0);
654          NCicEnvironment.add_constraint false (mk_type 0) (mk_cprop 0);
655
656          NCicEnvironment.add_constraint true (mk_type 1) (mk_type 2);
657          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_cprop 2);
658          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_type 2);
659          NCicEnvironment.add_constraint true (mk_type 1) (mk_cprop 2);
660          NCicEnvironment.add_constraint false (mk_cprop 1) (mk_type 1);
661          NCicEnvironment.add_constraint false (mk_type 1) (mk_cprop 1);
662
663          NCicEnvironment.add_constraint true (mk_type 2) (mk_type 3);
664          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_cprop 3);
665          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_type 3);
666          NCicEnvironment.add_constraint true (mk_type 2) (mk_cprop 3);
667          NCicEnvironment.add_constraint false (mk_cprop 2) (mk_type 2);
668          NCicEnvironment.add_constraint false (mk_type 2) (mk_cprop 2);
669
670          NCicEnvironment.add_constraint false (mk_cprop 3) (mk_type 3);
671          NCicEnvironment.add_constraint false (mk_type 3) (mk_cprop 3);
672
673 ;;
674 *)
675