]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_disambiguation/nCicDisambiguate.ml
Invariant dropped: NotationPt.NCic t can now contain metas.
[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 open Printf
15
16 open DisambiguateTypes
17
18 module Ast = NotationPt
19 module NRef = NReference 
20
21 let debug_print s = prerr_endline (Lazy.force s);;
22 let debug_print _ = ();;
23
24 let cic_name_of_name = function
25   | Ast.Ident (n, None) ->  n
26   | _ -> assert false
27 ;;
28
29 let rec mk_rels howmany from =
30   match howmany with 
31   | 0 -> []
32   | _ -> (NCic.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
33 ;;
34
35 let refine_term 
36  metasenv subst context uri ~rdb ~use_coercions term expty _ ~localization_tbl=
37   assert (uri=None);
38   debug_print (lazy (sprintf "TEST_INTERPRETATION: %s" 
39     (NCicPp.ppterm ~metasenv ~subst ~context term)));
40   try
41     let localise t = 
42       try NCicUntrusted.NCicHash.find localization_tbl t
43       with Not_found -> 
44         prerr_endline ("NOT LOCALISED" ^ NCicPp.ppterm ~metasenv ~subst ~context t);
45         (*assert false*) HExtlib.dummy_floc
46     in
47     let metasenv, subst, term, _ = 
48       NCicRefiner.typeof 
49         (rdb#set_coerc_db 
50           (if use_coercions then rdb#coerc_db else NCicCoercion.empty_db))
51         metasenv subst context term expty ~localise 
52     in
53      Disambiguate.Ok (term, metasenv, subst, ())
54   with
55   | NCicRefiner.Uncertain loc_msg ->
56       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
57         NCicPp.ppterm ~metasenv ~subst ~context term)) ;
58       Disambiguate.Uncertain loc_msg
59   | NCicRefiner.RefineFailure loc_msg ->
60       debug_print (lazy (sprintf "PRUNED:\nterm%s\nmessage:%s"
61         (NCicPp.ppterm ~metasenv ~subst ~context term) (snd(Lazy.force loc_msg))));
62       Disambiguate.Ko loc_msg
63 ;;
64
65 let refine_obj 
66   ~rdb metasenv subst _context _uri 
67   ~use_coercions obj _ _ugraph ~localization_tbl 
68 =
69   assert (metasenv=[]);
70   assert (subst=[]);
71   let localise t = 
72     try NCicUntrusted.NCicHash.find localization_tbl t
73     with Not_found -> 
74       (*assert false*)HExtlib.dummy_floc
75   in
76   try
77     let obj =
78       NCicRefiner.typeof_obj
79         (rdb#set_coerc_db
80            (if use_coercions then rdb#coerc_db 
81             else NCicCoercion.empty_db))
82         obj ~localise 
83     in
84       Disambiguate.Ok (obj, [], [], ())
85   with
86   | NCicRefiner.Uncertain loc_msg ->
87       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
88         NCicPp.ppobj obj)) ;
89       Disambiguate.Uncertain loc_msg
90   | NCicRefiner.RefineFailure loc_msg ->
91       debug_print (lazy (sprintf "PRUNED:\nobj: %s\nmessage: %s"
92         (NCicPp.ppobj obj) (snd(Lazy.force loc_msg))));
93       Disambiguate.Ko loc_msg
94 ;;
95   
96
97   (* TODO move it to Cic *)
98 let find_in_context name context =
99   let rec aux acc = function
100     | [] -> raise Not_found
101     | hd :: _ when hd = name -> acc
102     | _ :: tl ->  aux (acc + 1) tl
103   in
104   aux 1 context
105
106 let interpretate_term_and_interpretate_term_option 
107   ?(create_dummy_ids=false) 
108     ~obj_context ~mk_choice ~env ~uri ~is_path ~localization_tbl 
109 =
110   (* create_dummy_ids shouldbe used only for interpretating patterns *)
111   assert (uri = None);
112
113   let rec aux ~localize loc context = function
114     | NotationPt.AttributedTerm (`Loc loc, term) ->
115         let res = aux ~localize loc context term in
116         if localize then 
117          NCicUntrusted.NCicHash.add localization_tbl res loc;
118        res
119     | NotationPt.AttributedTerm (_, term) -> aux ~localize loc context term
120     | NotationPt.Appl (NotationPt.Appl inner :: args) ->
121         aux ~localize loc context (NotationPt.Appl (inner @ args))
122     | NotationPt.Appl 
123         (NotationPt.AttributedTerm (att,(NotationPt.Appl inner))::args)->
124         aux ~localize loc context 
125           (NotationPt.AttributedTerm (att,NotationPt.Appl (inner @ args)))
126     | NotationPt.Appl (NotationPt.Symbol (symb, i) :: args) ->
127         let cic_args = List.map (aux ~localize loc context) args in
128         Disambiguate.resolve ~mk_choice ~env (Symbol (symb, i)) (`Args cic_args)
129     | NotationPt.Appl terms ->
130        NCic.Appl (List.map (aux ~localize loc context) terms)
131     | NotationPt.Binder (binder_kind, (var, typ), body) ->
132         let cic_type = aux_option ~localize loc context `Type typ in
133         let cic_name = cic_name_of_name var  in
134         let cic_body = aux ~localize loc (cic_name :: context) body in
135         (match binder_kind with
136         | `Lambda -> NCic.Lambda (cic_name, cic_type, cic_body)
137         | `Pi
138         | `Forall -> NCic.Prod (cic_name, cic_type, cic_body)
139         | `Exists ->
140             Disambiguate.resolve ~env ~mk_choice (Symbol ("exists", 0))
141               (`Args [ cic_type; NCic.Lambda (cic_name, cic_type, cic_body) ]))
142     | NotationPt.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 `Term outtype in
145         let do_branch ((_, _, args), term) =
146          let rec do_branch' context = function
147            | [] -> aux ~localize loc context term
148            | (name, typ) :: tl ->
149                let cic_name = 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 -> NCic.Implicit `Type
154                  | Some typ -> aux ~localize loc context typ
155                in
156                NCic.Lambda (cic_name, typ, cic_body)
157          in
158           do_branch' context args
159         in
160         if create_dummy_ids then
161          let branches =
162           List.map
163            (function
164                Ast.Wildcard,term -> ("wildcard",None,[]), term
165              | Ast.Pattern _,_ ->
166                 raise (DisambiguateTypes.Invalid_choice 
167                  (lazy (loc, "Syntax error: the left hand side of a "^
168                    "branch pattern must be \"_\"")))
169            ) branches
170          in
171          (*
172           NCic.MutCase (ref, cic_outtype, cic_term,
173             (List.map do_branch branches))
174           *) ignore branches; assert false (* patterns not implemented yet *)
175         else
176          let indtype_ref =
177           match indty_ident with
178           | Some (indty_ident, _) ->
179              (match Disambiguate.resolve ~env ~mk_choice 
180                 (Id indty_ident) (`Args []) with
181               | NCic.Const (NReference.Ref (_,NReference.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: " ^ NCicPp.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 (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                 (Id (fst_constructor branches)) (`Args []) with
209               | NCic.Const (NReference.Ref (_,NReference.Con _) as r) -> 
210                    let b,_,_,_,_ = NCicEnvironment.get_checked_indtys r in
211                    NReference.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                   ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))))
220          in
221          let _,leftsno,itl,_,indtyp_no =
222           NCicEnvironment.get_checked_indtys 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 ~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.LetRec (_kind, _defs, _body) -> NCic.Implicit `Term
313     | NotationPt.Ident _
314     | NotationPt.Uri _
315     | NotationPt.NRef _ when is_path -> raise Disambiguate.PathNotWellFormed
316     | NotationPt.Ident (name, subst) ->
317        assert (subst = None);
318        (try
319              NCic.Rel (find_in_context name context)
320        with Not_found -> 
321          try NCic.Const (List.assoc name obj_context)
322          with Not_found ->
323             Disambiguate.resolve ~env ~mk_choice (Id name) (`Args []))
324     | NotationPt.Uri (uri, subst) ->
325        assert (subst = None);
326        (try
327          NCic.Const (NReference.reference_of_string uri)
328         with NRef.IllFormedReference _ ->
329          NotationPt.fail loc "Ill formed reference")
330     | NotationPt.NRef nref -> NCic.Const nref
331     | NotationPt.NCic t ->  t
332     | NotationPt.Implicit `Vector -> NCic.Implicit `Vector
333     | NotationPt.Implicit `JustOne -> NCic.Implicit `Term
334     | NotationPt.Implicit (`Tagged s) -> NCic.Implicit (`Tagged s)
335     | NotationPt.UserInput -> NCic.Implicit `Hole
336     | NotationPt.Num (num, i) -> 
337         Disambiguate.resolve ~env ~mk_choice (Num i) (`Num_arg num)
338     | NotationPt.Meta (index, subst) ->
339         let cic_subst =
340          List.map
341           (function None -> assert false| Some t -> aux ~localize loc context t)
342           subst
343         in
344          NCic.Meta (index, (0, NCic.Ctx cic_subst))
345     | NotationPt.Sort `Prop -> NCic.Sort NCic.Prop
346     | NotationPt.Sort `Set -> NCic.Sort (NCic.Type
347        [`Type,NUri.uri_of_string "cic:/matita/pts/Type.univ"])
348     | NotationPt.Sort (`NType s) -> NCic.Sort (NCic.Type
349        [`Type,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
350     | NotationPt.Sort (`NCProp s) -> NCic.Sort (NCic.Type
351        [`CProp,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
352     | NotationPt.Symbol (symbol, instance) ->
353         Disambiguate.resolve ~env ~mk_choice 
354          (Symbol (symbol, instance)) (`Args [])
355     | NotationPt.Variable _
356     | NotationPt.Magic _
357     | NotationPt.Layout _
358     | NotationPt.Literal _ -> assert false (* god bless Bologna *)
359   and aux_option ~localize loc context annotation = function
360     | None -> NCic.Implicit annotation
361     | Some (NotationPt.AttributedTerm (`Loc loc, term)) ->
362         let res = aux_option ~localize loc context annotation (Some term) in
363         if localize then 
364           NCicUntrusted.NCicHash.add localization_tbl res loc;
365         res
366     | Some (NotationPt.AttributedTerm (_, term)) ->
367         aux_option ~localize loc context annotation (Some term)
368     | Some NotationPt.Implicit `JustOne -> NCic.Implicit annotation
369     | Some NotationPt.Implicit `Vector -> NCic.Implicit `Vector
370     | Some term -> aux ~localize loc context term
371   in
372    (fun ~context -> aux ~localize:true HExtlib.dummy_floc context),
373    (fun ~context -> aux_option ~localize:true HExtlib.dummy_floc context)
374 ;;
375
376 let interpretate_term ?(create_dummy_ids=false) ~context ~env ~uri ~is_path ast
377      ~obj_context ~localization_tbl ~mk_choice
378 =
379   let context = List.map fst context in
380   fst 
381     (interpretate_term_and_interpretate_term_option 
382       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path ~localization_tbl)
383     ~context ast
384 ;;
385
386 let interpretate_term_option 
387   ?(create_dummy_ids=false) ~context ~env ~uri ~is_path 
388   ~localization_tbl ~mk_choice ~obj_context
389 =
390   let context = List.map fst context in
391   snd 
392     (interpretate_term_and_interpretate_term_option 
393       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path ~localization_tbl)
394     ~context 
395 ;;
396
397 let disambiguate_path path =
398   let localization_tbl = NCicUntrusted.NCicHash.create 23 in
399   fst
400     (interpretate_term_and_interpretate_term_option 
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 
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 (flavour, name, ty, bo, pragma) ->
424      let ty' = 
425        interpretate_term 
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           let attrs = `Provided, flavour, pragma in
433           NCic.Constant ([],name,None,ty',attrs)
434       | Some _,`Axiom -> assert false
435       | None,_ ->
436           let attrs = `Provided, flavour, pragma in
437           NCic.Constant ([],name,Some (NCic.Implicit `Term),ty',attrs)
438       | Some bo,_ ->
439         (match bo with
440          | NotationPt.LetRec (kind, defs, _) ->
441              let inductive = kind = `Inductive in
442              let _,obj_context =
443                List.fold_left
444                  (fun (i,acc) (_,(name,_),_,k) -> 
445                   (i+1, 
446                     (ncic_name_of_ident name, NReference.reference_of_spec uri 
447                      (if inductive then NReference.Fix (i,k,0)
448                       else NReference.CoFix i)) :: acc))
449                  (0,[]) defs
450              in
451              let inductiveFuns =
452                List.map
453                  (fun (params, (name, typ), body, decr_idx) ->
454                    let add_binders kind t =
455                     List.fold_right
456                      (fun var t -> 
457                         NotationPt.Binder (kind, var, t)) params t
458                    in
459                    let cic_body =
460                      interpretate_term 
461                        ~obj_context ~context ~env ~uri:None ~is_path:false
462                        (add_binders `Lambda body) 
463                    in
464                    let cic_type =
465                      interpretate_term_option 
466                        ~obj_context:[]
467                        ~context ~env ~uri:None ~is_path:false `Type
468                        (HExtlib.map_option (add_binders `Pi) typ)
469                    in
470                    ([],ncic_name_of_ident name, decr_idx, cic_type, cic_body))
471                  defs
472              in
473              let attrs = `Provided, flavour, pragma in
474              NCic.Fixpoint (inductive,inductiveFuns,attrs)
475          | bo -> 
476              let bo = 
477                interpretate_term 
478                 ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false bo
479              in
480              let attrs = `Provided, flavour, pragma in
481              NCic.Constant ([],name,Some bo,ty',attrs)))
482  | NotationPt.Inductive (params,tyl) ->
483     let context,params =
484      let context,res =
485       List.fold_left
486        (fun (context,res) (name,t) ->
487          let t =
488           match t with
489              None -> NotationPt.Implicit `JustOne
490            | Some t -> t in
491          let name = cic_name_of_name name in
492          let t =
493           interpretate_term ~obj_context:[] ~context ~env ~uri:None
494            ~is_path:false t
495          in
496           (name,NCic.Decl t)::context,(name,t)::res
497        ) ([],[]) params
498      in
499       context,List.rev res in
500     let add_params =
501      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
502     let leftno = List.length params in
503     let _,inductive,_,_ = try List.hd tyl with Failure _ -> assert false in
504     let obj_context =
505      snd (
506       List.fold_left
507        (fun (i,res) (name,_,_,_) ->
508          let nref =
509           NReference.reference_of_spec uri (NReference.Ind (inductive,i,leftno))
510          in
511           i+1,(name,nref)::res)
512        (0,[]) tyl) in
513     let tyl =
514      List.map
515       (fun (name,_,ty,cl) ->
516         let ty' =
517          add_params
518          (interpretate_term ~obj_context:[] ~context ~env ~uri:None
519            ~is_path:false ty) in
520         let cl' =
521          List.map
522           (fun (name,ty) ->
523             let ty' =
524              add_params
525               (interpretate_term ~obj_context ~context ~env ~uri:None
526                 ~is_path:false ty) in
527             let relevance = [] in
528              relevance,name,ty'
529           ) cl in
530         let relevance = [] in
531          relevance,name,ty',cl'
532       ) tyl
533     in
534      let height = (* XXX calculate *) 0 in
535      let attrs = `Provided, `Regular in
536      uri, height, [], [], 
537      NCic.Inductive (inductive,leftno,tyl,attrs)
538  | NotationPt.Record (params,name,ty,fields) ->
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 -> NotationPt.Implicit `JustOne
546            | Some t -> t in
547          let name = cic_name_of_name name in
548          let t =
549           interpretate_term ~obj_context:[] ~context ~env ~uri:None
550            ~is_path:false t
551          in
552           (name,NCic.Decl t)::context,(name,t)::res
553        ) ([],[]) params
554      in
555       context,List.rev res in
556     let add_params =
557      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
558     let leftno = List.length params in
559     let ty' =
560      add_params
561       (interpretate_term ~obj_context:[] ~context ~env ~uri:None
562         ~is_path:false ty) in
563     let nref =
564      NReference.reference_of_spec uri (NReference.Ind (true,0,leftno)) in
565     let obj_context = [name,nref] in
566     let fields' =
567      snd (
568       List.fold_left
569        (fun (context,res) (name,ty,_coercion,_arity) ->
570          let ty =
571           interpretate_term ~obj_context ~context ~env ~uri:None
572            ~is_path:false ty in
573          let context' = (name,NCic.Decl ty)::context in
574           context',(name,ty)::res
575        ) (context,[]) fields) in
576     let concl =
577      let mutind = NCic.Const nref in
578      if params = [] then mutind
579      else
580       NCic.Appl
581        (mutind::mk_rels (List.length params) (List.length fields)) in
582     let con =
583      List.fold_left (fun t (name,ty) -> NCic.Prod (name,ty,t)) concl fields' in
584     let con' = add_params con in
585     let relevance = [] in
586     let tyl = [relevance,name,ty',[relevance,"mk_" ^ name,con']] in
587     let field_names = List.map (fun (x,_,y,z) -> x,y,z) fields in
588      let height = (* XXX calculate *) 0 in
589      let attrs = `Provided, `Record field_names in
590      uri, height, [], [], 
591      NCic.Inductive (true,leftno,tyl,attrs)
592 ;;
593
594 let disambiguate_term ~context ~metasenv ~subst ~expty
595    ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
596    ~aliases ~universe ~rdb ~lookup_in_library 
597    (text,prefix_len,term) 
598  =
599   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
600    let res,b =
601     MultiPassDisambiguator.disambiguate_thing
602      ~freshen_thing:NotationUtil.freshen_term
603      ~context ~metasenv ~initial_ugraph:() ~aliases
604      ~mk_implicit ~description_of_alias ~fix_instance
605      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
606      ~universe ~uri:None ~pp_thing:NotationPp.pp_term
607      ~passes:(MultiPassDisambiguator.passes ())
608      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_term
609      ~interpretate_thing:(interpretate_term ~obj_context:[] ~mk_choice (?create_dummy_ids:None))
610      ~refine_thing:(refine_term ~rdb) (text,prefix_len,term)
611      ~mk_localization_tbl ~expty ~subst
612    in
613     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
614 ;;
615
616 let disambiguate_obj 
617    ~mk_implicit ~description_of_alias ~fix_instance ~mk_choice
618    ~aliases ~universe ~rdb ~lookup_in_library ~uri
619    (text,prefix_len,obj) 
620  =
621   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
622    let res,b =
623     MultiPassDisambiguator.disambiguate_thing
624      ~freshen_thing:NotationUtil.freshen_obj
625      ~context:[] ~metasenv:[] ~subst:[] ~initial_ugraph:() ~aliases
626      ~mk_implicit ~description_of_alias ~fix_instance
627      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
628      ~universe 
629      ~uri:(Some uri)
630      ~pp_thing:(NotationPp.pp_obj NotationPp.pp_term)
631      ~passes:(MultiPassDisambiguator.passes ())
632      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_obj
633      ~interpretate_thing:(interpretate_obj ~mk_choice)
634      ~refine_thing:(refine_obj ~rdb) 
635      (text,prefix_len,obj)
636      ~mk_localization_tbl ~expty:None
637    in
638     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
639 ;;
640 (*
641 let _ = 
642 let mk_type n = 
643   if n = 0 then
644      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
645   else
646      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
647 in
648 let mk_cprop n = 
649   if n = 0 then 
650     [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
651   else
652     [false, NUri.uri_of_string ("cic:/matita/pts/CProp"^string_of_int n^".univ")]
653 in
654          NCicEnvironment.add_constraint true (mk_type 0) (mk_type 1);
655          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_cprop 1);
656          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_type 1);
657          NCicEnvironment.add_constraint true (mk_type 0) (mk_cprop 1);
658          NCicEnvironment.add_constraint false (mk_cprop 0) (mk_type 0);
659          NCicEnvironment.add_constraint false (mk_type 0) (mk_cprop 0);
660
661          NCicEnvironment.add_constraint true (mk_type 1) (mk_type 2);
662          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_cprop 2);
663          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_type 2);
664          NCicEnvironment.add_constraint true (mk_type 1) (mk_cprop 2);
665          NCicEnvironment.add_constraint false (mk_cprop 1) (mk_type 1);
666          NCicEnvironment.add_constraint false (mk_type 1) (mk_cprop 1);
667
668          NCicEnvironment.add_constraint true (mk_type 2) (mk_type 3);
669          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_cprop 3);
670          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_type 3);
671          NCicEnvironment.add_constraint true (mk_type 2) (mk_cprop 3);
672          NCicEnvironment.add_constraint false (mk_cprop 2) (mk_type 2);
673          NCicEnvironment.add_constraint false (mk_type 2) (mk_cprop 2);
674
675          NCicEnvironment.add_constraint false (mk_cprop 3) (mk_type 3);
676          NCicEnvironment.add_constraint false (mk_type 3) (mk_cprop 3);
677
678 ;;
679 *)
680