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