]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_disambiguation/nCicDisambiguate.ml
- new syntax for let rec/corec with flavor specifier (tested on lambdadelta/ground_2/)
[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=false) ~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           NReference.reference_of_string "cic:/fake_indty.ind(0,0,0)"
171          in
172           NCic.Match (indtype_ref, cic_outtype, cic_term,
173            (List.map do_branch branches))
174         else
175          let indtype_ref =
176           match indty_ident with
177           | Some (indty_ident, _) ->
178              (match Disambiguate.resolve ~env ~mk_choice 
179                 (DT.Id indty_ident) (`Args []) with
180               | NCic.Const (NReference.Ref (_,NReference.Ind _) as r) -> r
181               | NCic.Implicit _ ->
182                  raise (Disambiguate.Try_again 
183                   (lazy "The type of the term to be matched is still unknown"))
184               | t ->
185                 raise (DisambiguateTypes.Invalid_choice 
186                   (lazy (loc,"The type of the term to be matched "^
187                           "is not (co)inductive: " ^ status#ppterm 
188                           ~metasenv:[] ~subst:[] ~context:[] t))))
189           | None ->
190               let rec fst_constructor =
191                 function
192                    (Ast.Pattern (head, _, _), _) :: _ -> head
193                  | (Ast.Wildcard, _) :: tl -> fst_constructor tl
194                  | [] -> raise (DT.Invalid_choice (lazy (loc,"The type "^
195                      "of the term to be matched cannot be determined "^
196                      "because it is an inductive type without constructors "^
197                      "or because all patterns use wildcards")))
198               in
199 (*
200               DisambiguateTypes.Environment.iter
201                   (fun k v ->
202                       prerr_endline
203                         (DisambiguateTypes.string_of_domain_item k ^ " => " ^
204                         description_of_alias v)) env; 
205 *)
206               (match Disambiguate.resolve ~env ~mk_choice
207                 (DT.Id (fst_constructor branches)) (`Args []) with
208               | NCic.Const (NReference.Ref (_,NReference.Con _) as r) -> 
209                    let b,_,_,_,_= NCicEnvironment.get_checked_indtys status r in
210                    NReference.mk_indty b r
211               | NCic.Implicit _ ->
212                  raise (Disambiguate.Try_again 
213                   (lazy "The type of the term to be matched is still unknown"))
214               | t ->
215                 raise (DisambiguateTypes.Invalid_choice 
216                   (lazy (loc, 
217                   "The type of the term to be matched is not (co)inductive: " 
218                   ^ status#ppterm ~metasenv:[] ~subst:[] ~context:[] t))))
219          in
220          let _,leftsno,itl,_,indtyp_no =
221           NCicEnvironment.get_checked_indtys status indtype_ref in
222          let _,_,_,cl =
223           try
224            List.nth itl indtyp_no
225           with _ -> assert false in
226          let rec count_prod t =
227                  match NCicReduction.whd status ~subst:[] [] t with
228                NCic.Prod (_, _, t) -> 1 + (count_prod t)
229              | _ -> 0 
230          in 
231          let rec sort branches cl =
232           match cl with
233              [] ->
234               let rec analyze unused unrecognized useless =
235                function
236                   [] ->
237                    if unrecognized != [] then
238                     raise (DisambiguateTypes.Invalid_choice
239                      (lazy
240                        (loc,"Unrecognized constructors: " ^
241                         String.concat " " unrecognized)))
242                    else if useless > 0 then
243                     raise (DisambiguateTypes.Invalid_choice
244                      (lazy
245                        (loc,"The last " ^ string_of_int useless ^
246                         "case" ^ if useless > 1 then "s are" else " is" ^
247                         " unused")))
248                    else
249                     []
250                 | (Ast.Wildcard,_)::tl when not unused ->
251                     analyze true unrecognized useless tl
252                 | (Ast.Pattern (head,_,_),_)::tl when not unused ->
253                     analyze unused (head::unrecognized) useless tl
254                 | _::tl -> analyze unused unrecognized (useless + 1) tl
255               in
256                analyze false [] 0 branches
257            | (_,name,ty)::cltl ->
258               let rec find_and_remove =
259                function
260                   [] ->
261                    raise
262                     (DisambiguateTypes.Invalid_choice
263                      (lazy (loc, "Missing case: " ^ name)))
264                 | ((Ast.Wildcard, _) as branch :: _) as branches ->
265                     branch, branches
266                 | (Ast.Pattern (name',_,_),_) as branch :: tl
267                    when name = name' ->
268                     branch,tl
269                 | branch::tl ->
270                    let found,rest = find_and_remove tl in
271                     found, branch::rest
272               in
273                let branch,tl = find_and_remove branches in
274                match branch with
275                   Ast.Pattern (name,y,args),term ->
276                    if List.length args = count_prod ty - leftsno then
277                     ((name,y,args),term)::sort tl cltl
278                    else
279                     raise
280                      (DisambiguateTypes.Invalid_choice
281                       (lazy (loc,"Wrong number of arguments for " ^ name)))
282                 | Ast.Wildcard,term ->
283                    let rec mk_lambdas =
284                     function
285                        0 -> term
286                      | n ->
287                         NotationPt.Binder
288                          (`Lambda, (NotationPt.Ident ("_", None), None),
289                            mk_lambdas (n - 1))
290                    in
291                     (("wildcard",None,[]),
292                      mk_lambdas (count_prod ty - leftsno)) :: sort tl cltl
293          in
294           let branches = sort branches cl in
295            NCic.Match (indtype_ref, cic_outtype, cic_term,
296             (List.map do_branch branches))
297     | NotationPt.Cast (t1, t2) ->
298         let cic_t1 = aux ~localize loc context t1 in
299         let cic_t2 = aux ~localize loc context t2 in
300         NCic.LetIn ("_",cic_t2,cic_t1, NCic.Rel 1)
301     | NotationPt.LetIn ((name, typ), def, body) ->
302         let cic_def = aux ~localize loc context def in
303         let cic_name = cic_name_of_name name in
304         let cic_typ =
305           match typ with
306           | None -> NCic.Implicit `Type
307           | Some t -> aux ~localize loc context t
308         in
309         let cic_body = aux ~localize loc (cic_name :: context) body in
310         NCic.LetIn (cic_name, cic_typ, cic_def, cic_body)
311     | NotationPt.Ident _
312     | NotationPt.Uri _
313     | NotationPt.NRef _ when is_path -> raise Disambiguate.PathNotWellFormed
314     | NotationPt.Ident (name, subst) ->
315        assert (subst = None);
316        (try
317              NCic.Rel (find_in_context name context)
318        with Not_found -> 
319          try NCic.Const (List.assoc name obj_context)
320          with Not_found ->
321             Disambiguate.resolve ~env ~mk_choice (DT.Id name) (`Args []))
322     | NotationPt.Uri (uri, subst) ->
323        assert (subst = None);
324        (try
325          NCic.Const (NReference.reference_of_string uri)
326         with NRef.IllFormedReference _ ->
327          NotationPt.fail loc "Ill formed reference")
328     | NotationPt.NRef nref -> NCic.Const nref
329     | NotationPt.NCic t ->  t
330     | NotationPt.Implicit `Vector -> NCic.Implicit `Vector
331     | NotationPt.Implicit `JustOne -> NCic.Implicit `Term
332     | NotationPt.Implicit (`Tagged s) -> NCic.Implicit (`Tagged s)
333     | NotationPt.UserInput -> NCic.Implicit `Hole
334     | NotationPt.Num (num, i) -> 
335         Disambiguate.resolve ~env ~mk_choice (DT.Num i) (`Num_arg num)
336     | NotationPt.Meta (index, subst) ->
337         let cic_subst =
338          List.map
339           (function None -> assert false| Some t -> aux ~localize loc context t)
340           subst
341         in
342          NCic.Meta (index, (0, NCic.Ctx cic_subst))
343     | NotationPt.Sort `Prop -> NCic.Sort NCic.Prop
344     | NotationPt.Sort `Set -> NCic.Sort (NCic.Type
345        [`Type,NUri.uri_of_string "cic:/matita/pts/Type.univ"])
346     | NotationPt.Sort (`NType s) -> NCic.Sort (NCic.Type
347        [`Type,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
348     | NotationPt.Sort (`NCProp s) -> NCic.Sort (NCic.Type
349        [`CProp,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
350     | NotationPt.Symbol (symbol, instance) ->
351         Disambiguate.resolve ~env ~mk_choice 
352          (DT.Symbol (symbol, instance)) (`Args [])
353     | NotationPt.Variable _
354     | NotationPt.Magic _
355     | NotationPt.Layout _
356     | NotationPt.Literal _ -> assert false (* god bless Bologna *)
357   and aux_option ~localize loc context annotation = function
358     | None -> NCic.Implicit annotation
359     | Some (NotationPt.AttributedTerm (`Loc loc, term)) ->
360         let res = aux_option ~localize loc context annotation (Some term) in
361         if localize then 
362           NCicUntrusted.NCicHash.add localization_tbl res loc;
363         res
364     | Some (NotationPt.AttributedTerm (_, term)) ->
365         aux_option ~localize loc context annotation (Some term)
366     | Some NotationPt.Implicit `JustOne -> NCic.Implicit annotation
367     | Some NotationPt.Implicit `Vector -> NCic.Implicit `Vector
368     | Some term -> aux ~localize loc context term
369   in
370    (fun ~context -> aux ~localize:true HExtlib.dummy_floc context),
371    (fun ~context -> aux_option ~localize:true HExtlib.dummy_floc context)
372 ;;
373
374 let interpretate_term status ?(create_dummy_ids=false) ~context ~env ~uri
375  ~is_path ast ~obj_context ~localization_tbl ~mk_choice
376 =
377   let context = List.map fst context in
378   fst 
379     (interpretate_term_and_interpretate_term_option status
380       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path
381       ~localization_tbl)
382     ~context ast
383 ;;
384
385 let interpretate_term_option status ?(create_dummy_ids=false) ~context ~env ~uri
386  ~is_path ~localization_tbl ~mk_choice ~obj_context
387 =
388   let context = List.map fst context in
389   snd 
390     (interpretate_term_and_interpretate_term_option status
391       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path
392       ~localization_tbl)
393     ~context 
394 ;;
395
396 let disambiguate_path status path =
397   let localization_tbl = NCicUntrusted.NCicHash.create 23 in
398   fst
399     (interpretate_term_and_interpretate_term_option status
400     ~obj_context:[] ~mk_choice:(fun _ -> assert false)
401     ~create_dummy_ids:true ~env:DisambiguateTypes.Environment.empty
402     ~uri:None ~is_path:true ~localization_tbl) ~context:[] path
403 ;;
404
405 let ncic_name_of_ident = function
406   | Ast.Ident (name, None) -> name
407   | _ -> assert false
408 ;;
409
410 let interpretate_obj status
411 (*      ?(create_dummy_ids=false)  *)
412      ~context ~env ~uri ~is_path obj ~localization_tbl ~mk_choice 
413 =
414  assert (context = []);
415  assert (is_path = false);
416  let interpretate_term ~obj_context =
417   interpretate_term ~mk_choice ~localization_tbl ~obj_context in
418  let interpretate_term_option ~obj_context =
419    interpretate_term_option ~mk_choice ~localization_tbl ~obj_context in
420  let uri = match uri with | None -> assert false | Some u -> u in
421  match obj with
422  | NotationPt.Theorem (name, ty, bo, attrs) ->
423      let _, flavour, _ = attrs in
424      let ty' = 
425        interpretate_term status
426          ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false ty 
427      in
428      let height = (* XXX calculate *) 0 in
429      uri, height, [], [], 
430      (match bo,flavour with
431       | None,`Axiom -> 
432           NCic.Constant ([],name,None,ty',attrs)
433       | Some _,`Axiom -> assert false
434       | None,_ ->
435           NCic.Constant ([],name,Some (NCic.Implicit `Term),ty',attrs)
436       | Some bo,_ ->
437           let bo = 
438             interpretate_term status
439              ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false bo
440           in
441           NCic.Constant ([],name,Some bo,ty',attrs))
442  | NotationPt.Inductive (params,tyl,source) ->
443     let context,params =
444      let context,res =
445       List.fold_left
446        (fun (context,res) (name,t) ->
447          let t =
448           match t with
449              None -> NotationPt.Implicit `JustOne
450            | Some t -> t in
451          let name = cic_name_of_name name in
452          let t =
453           interpretate_term status ~obj_context:[] ~context ~env ~uri:None
454            ~is_path:false t
455          in
456           (name,NCic.Decl t)::context,(name,t)::res
457        ) ([],[]) params
458      in
459       context,List.rev res in
460     let add_params =
461      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
462     let leftno = List.length params in
463     let _,inductive,_,_ = try List.hd tyl with Failure _ -> assert false in
464     let obj_context =
465      snd (
466       List.fold_left
467        (fun (i,res) (name,_,_,_) ->
468          let nref =
469           NReference.reference_of_spec uri (NReference.Ind (inductive,i,leftno))
470          in
471           i+1,(name,nref)::res)
472        (0,[]) tyl) in
473     let tyl =
474      List.map
475       (fun (name,_,ty,cl) ->
476         let ty' =
477          add_params
478          (interpretate_term status ~obj_context:[] ~context ~env ~uri:None
479            ~is_path:false ty) in
480         let cl' =
481          List.map
482           (fun (name,ty) ->
483             let ty' =
484              add_params
485               (interpretate_term status ~obj_context ~context ~env ~uri:None
486                 ~is_path:false ty) in
487             let relevance = [] in
488              relevance,name,ty'
489           ) cl in
490         let relevance = [] in
491          relevance,name,ty',cl'
492       ) tyl
493     in
494      let height = (* XXX calculate *) 0 in
495      let attrs = source, `Regular in
496      uri, height, [], [], 
497      NCic.Inductive (inductive,leftno,tyl,attrs)
498  | NotationPt.Record (params,name,ty,fields,source) ->
499     let context,params =
500      let context,res =
501       List.fold_left
502        (fun (context,res) (name,t) ->
503          let t =
504           match t with
505              None -> NotationPt.Implicit `JustOne
506            | Some t -> t in
507          let name = cic_name_of_name name in
508          let t =
509           interpretate_term status ~obj_context:[] ~context ~env ~uri:None
510            ~is_path:false t
511          in
512           (name,NCic.Decl t)::context,(name,t)::res
513        ) ([],[]) params
514      in
515       context,List.rev res in
516     let add_params =
517      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
518     let leftno = List.length params in
519     let ty' =
520      add_params
521       (interpretate_term status ~obj_context:[] ~context ~env ~uri:None
522         ~is_path:false ty) in
523     let nref =
524      NReference.reference_of_spec uri (NReference.Ind (true,0,leftno)) in
525     let obj_context = [name,nref] in
526     let fields' =
527      snd (
528       List.fold_left
529        (fun (context,res) (name,ty,_coercion,_arity) ->
530          let ty =
531           interpretate_term status ~obj_context ~context ~env ~uri:None
532            ~is_path:false ty in
533          let context' = (name,NCic.Decl ty)::context in
534           context',(name,ty)::res
535        ) (context,[]) fields) in
536     let concl =
537      let mutind = NCic.Const nref in
538      if params = [] then mutind
539      else
540       NCic.Appl
541        (mutind::mk_rels (List.length params) (List.length fields)) in
542     let con =
543      List.fold_left (fun t (name,ty) -> NCic.Prod (name,ty,t)) concl fields' in
544     let con' = add_params con in
545     let relevance = [] in
546     let tyl = [relevance,name,ty',[relevance,"mk_" ^ name,con']] in
547     let field_names = List.map (fun (x,_,y,z) -> x,y,z) fields in
548      let height = (* XXX calculate *) 0 in
549      let attrs = source, `Record field_names in
550      uri, height, [], [], 
551      NCic.Inductive (true,leftno,tyl,attrs)
552  | NotationPt.LetRec (kind, defs, attrs) ->
553      let inductive = kind = `Inductive in
554      let _,obj_context =
555        List.fold_left
556          (fun (i,acc) (_,(name,_),_,k) -> 
557           (i+1, 
558             (ncic_name_of_ident name, NReference.reference_of_spec uri 
559              (if inductive then NReference.Fix (i,k,0)
560               else NReference.CoFix i)) :: acc))
561          (0,[]) defs
562      in
563      let inductiveFuns =
564        List.map
565          (fun (params, (name, typ), body, decr_idx) ->
566            let add_binders kind t =
567             List.fold_right
568              (fun var t -> 
569                 NotationPt.Binder (kind, var, t)) params t
570            in
571            let cic_body =
572              interpretate_term status
573                ~obj_context ~context ~env ~uri:None ~is_path:false
574                (add_binders `Lambda body) 
575            in
576            let cic_type =
577              interpretate_term_option status
578                ~obj_context:[]
579                ~context ~env ~uri:None ~is_path:false `Type
580                (HExtlib.map_option (add_binders `Pi) typ)
581            in
582            ([],ncic_name_of_ident name, decr_idx, cic_type, cic_body))
583          defs
584      in
585      let height = (* XXX calculate *) 0 in
586      uri, height, [], [], 
587      NCic.Fixpoint (inductive,inductiveFuns,attrs)
588 ;;
589
590 let disambiguate_term (status: #NCicCoercion.status) ~context ~metasenv ~subst
591  ~expty ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
592  ~aliases ~universe ~lookup_in_library (text,prefix_len,term) 
593 =
594   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
595    let res,b =
596     MultiPassDisambiguator.disambiguate_thing
597      ~freshen_thing:NotationUtil.freshen_term
598      ~context ~metasenv ~initial_ugraph:() ~aliases
599      ~mk_implicit ~description_of_alias ~fix_instance
600      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
601      ~universe ~uri:None ~pp_thing:(NotationPp.pp_term status)
602      ~passes:(MultiPassDisambiguator.passes ())
603      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_term
604      ~interpretate_thing:(interpretate_term status ~obj_context:[] ~mk_choice (?create_dummy_ids:None))
605      ~refine_thing:(refine_term status) (text,prefix_len,term)
606      ~mk_localization_tbl ~expty ~subst
607    in
608     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
609 ;;
610
611 let disambiguate_obj (status: #NCicCoercion.status)
612    ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
613    ~aliases ~universe ~lookup_in_library ~uri (text,prefix_len,obj) 
614  =
615   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
616    let res,b =
617     MultiPassDisambiguator.disambiguate_thing
618      ~freshen_thing:NotationUtil.freshen_obj
619      ~context:[] ~metasenv:[] ~subst:[] ~initial_ugraph:() ~aliases
620      ~mk_implicit ~description_of_alias ~fix_instance
621      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
622      ~universe 
623      ~uri:(Some uri)
624      ~pp_thing:(NotationPp.pp_obj (NotationPp.pp_term status))
625      ~passes:(MultiPassDisambiguator.passes ())
626      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_obj
627      ~interpretate_thing:(interpretate_obj status ~mk_choice)
628      ~refine_thing:(refine_obj status) 
629      (text,prefix_len,obj)
630      ~mk_localization_tbl ~expty:`XTNone
631    in
632     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
633 ;;
634 (*
635 let _ = 
636 let mk_type n = 
637   if n = 0 then
638      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
639   else
640      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
641 in
642 let mk_cprop n = 
643   if n = 0 then 
644     [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
645   else
646     [false, NUri.uri_of_string ("cic:/matita/pts/CProp"^string_of_int n^".univ")]
647 in
648          NCicEnvironment.add_constraint true (mk_type 0) (mk_type 1);
649          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_cprop 1);
650          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_type 1);
651          NCicEnvironment.add_constraint true (mk_type 0) (mk_cprop 1);
652          NCicEnvironment.add_constraint false (mk_cprop 0) (mk_type 0);
653          NCicEnvironment.add_constraint false (mk_type 0) (mk_cprop 0);
654
655          NCicEnvironment.add_constraint true (mk_type 1) (mk_type 2);
656          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_cprop 2);
657          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_type 2);
658          NCicEnvironment.add_constraint true (mk_type 1) (mk_cprop 2);
659          NCicEnvironment.add_constraint false (mk_cprop 1) (mk_type 1);
660          NCicEnvironment.add_constraint false (mk_type 1) (mk_cprop 1);
661
662          NCicEnvironment.add_constraint true (mk_type 2) (mk_type 3);
663          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_cprop 3);
664          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_type 3);
665          NCicEnvironment.add_constraint true (mk_type 2) (mk_cprop 3);
666          NCicEnvironment.add_constraint false (mk_cprop 2) (mk_type 2);
667          NCicEnvironment.add_constraint false (mk_type 2) (mk_cprop 2);
668
669          NCicEnvironment.add_constraint false (mk_cprop 3) (mk_type 3);
670          NCicEnvironment.add_constraint false (mk_type 3) (mk_cprop 3);
671
672 ;;
673 *)
674