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