]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_disambiguation/nCicDisambiguate.ml
5ded5b886370c7c9230e9bfd6323e7be8522db3c
[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 ~coercion_db ~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         (NCicUnifHint.db ())
51         ~look_for_coercion:(
52           if use_coercions then 
53            NCicCoercion.look_for_coercion coercion_db
54           else (fun _ _ _ _ _ -> []))
55         metasenv subst context term expty ~localise 
56     in
57      Disambiguate.Ok (term, metasenv, subst, ())
58   with
59   | NCicRefiner.Uncertain loc_msg ->
60       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
61         NCicPp.ppterm ~metasenv ~subst ~context term)) ;
62       Disambiguate.Uncertain loc_msg
63   | NCicRefiner.RefineFailure loc_msg ->
64       debug_print (lazy (sprintf "PRUNED:\nterm%s\nmessage:%s"
65         (NCicPp.ppterm ~metasenv ~subst ~context term) (snd(Lazy.force loc_msg))));
66       Disambiguate.Ko loc_msg
67 ;;
68
69 let refine_obj 
70   ~coercion_db metasenv subst context _uri 
71   ~use_coercions obj _ _ugraph ~localization_tbl 
72 =
73   assert (metasenv=[]);
74   assert (subst=[]);
75   let localise t = 
76     try NCicUntrusted.NCicHash.find localization_tbl t
77     with Not_found -> 
78       prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t);
79       (*assert false*)HExtlib.dummy_floc
80   in
81   try
82     let obj =
83       NCicRefiner.typeof_obj
84         (NCicUnifHint.db ())
85         ~look_for_coercion:(
86           if use_coercions then 
87            NCicCoercion.look_for_coercion coercion_db
88           else (fun _ _ _ _ _ -> []))
89         obj ~localise 
90     in
91       Disambiguate.Ok (obj, [], [], ())
92   with
93   | NCicRefiner.Uncertain loc_msg ->
94       debug_print (lazy ("UNCERTAIN: [" ^ snd (Lazy.force loc_msg) ^ "] " ^ 
95         NCicPp.ppobj obj)) ;
96       Disambiguate.Uncertain loc_msg
97   | NCicRefiner.RefineFailure loc_msg ->
98       debug_print (lazy (sprintf "PRUNED:\nobj: %s\nmessage: %s"
99         (NCicPp.ppobj obj) (snd(Lazy.force loc_msg))));
100       Disambiguate.Ko loc_msg
101 ;;
102   
103
104   (* TODO move it to Cic *)
105 let find_in_context name context =
106   let rec aux acc = function
107     | [] -> raise Not_found
108     | hd :: _ when hd = name -> acc
109     | _ :: tl ->  aux (acc + 1) tl
110   in
111   aux 1 context
112
113 let interpretate_term_and_interpretate_term_option 
114   ?(create_dummy_ids=false) 
115     ~obj_context ~mk_choice ~env ~uri ~is_path ~localization_tbl 
116 =
117   (* create_dummy_ids shouldbe used only for interpretating patterns *)
118   assert (uri = None);
119
120   let rec aux ~localize loc context = function
121     | CicNotationPt.AttributedTerm (`Loc loc, term) ->
122         let res = aux ~localize loc context term in
123         if localize then 
124          NCicUntrusted.NCicHash.add localization_tbl res loc;
125        res
126     | CicNotationPt.AttributedTerm (_, term) -> aux ~localize loc context term
127     | CicNotationPt.Appl (CicNotationPt.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     | CicNotationPt.Appl terms ->
131        NCic.Appl (List.map (aux ~localize loc context) terms)
132     | CicNotationPt.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     | CicNotationPt.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
171          in
172          (*
173           NCic.MutCase (ref, cic_outtype, cic_term,
174             (List.map do_branch branches))
175           *) ignore branches; assert false (* patterns not implemented yet *)
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: " ^ NCicPp.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 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                   ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t))))
221          in
222          let _,leftsno,itl,_,indtyp_no =
223           NCicEnvironment.get_checked_indtys 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 ~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                         CicNotationPt.Binder
290                          (`Lambda, (CicNotationPt.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     | CicNotationPt.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     | CicNotationPt.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     | CicNotationPt.LetRec (_kind, _defs, _body) -> NCic.Implicit `Term
314     | CicNotationPt.Ident _
315     | CicNotationPt.Uri _ when is_path -> raise Disambiguate.PathNotWellFormed
316     | CicNotationPt.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     | CicNotationPt.Uri (name, subst) ->
325        assert (subst = None);
326        (try
327          NCic.Const (NRef.reference_of_string name)
328         with NRef.IllFormedReference _ ->
329          CicNotationPt.fail loc "Ill formed reference")
330     | CicNotationPt.Implicit -> NCic.Implicit `Term
331     | CicNotationPt.UserInput -> NCic.Implicit `Hole
332     | CicNotationPt.Num (num, i) -> 
333         Disambiguate.resolve ~env ~mk_choice (Num i) (`Num_arg num)
334     | CicNotationPt.Meta (index, subst) ->
335         let cic_subst =
336          List.map
337           (function None -> assert false| Some t -> aux ~localize loc context t)
338           subst
339         in
340          NCic.Meta (index, (0, NCic.Ctx cic_subst))
341     | CicNotationPt.Sort `Prop -> NCic.Sort NCic.Prop
342     | CicNotationPt.Sort `Set -> NCic.Sort (NCic.Type
343        [false,NUri.uri_of_string "cic:/matita/pts/Type.univ"])
344     | CicNotationPt.Sort (`Type _u) -> NCic.Sort (NCic.Type
345        [false,NUri.uri_of_string "cic:/matita/pts/Type0.univ"])
346     | CicNotationPt.Sort (`NType s) -> NCic.Sort (NCic.Type
347        [false,NUri.uri_of_string ("cic:/matita/pts/Type" ^ s ^ ".univ")])
348     | CicNotationPt.Sort (`NCProp s) -> NCic.Sort (NCic.Type
349        [false,NUri.uri_of_string ("cic:/matita/pts/CProp" ^ s ^ ".univ")])
350     | CicNotationPt.Sort (`CProp _u) -> NCic.Sort (NCic.Type
351        [false,NUri.uri_of_string "cic:/matita/pts/CProp0.univ"])
352     | CicNotationPt.Symbol (symbol, instance) ->
353         Disambiguate.resolve ~env ~mk_choice 
354          (Symbol (symbol, instance)) (`Args [])
355     | CicNotationPt.Variable _
356     | CicNotationPt.Magic _
357     | CicNotationPt.Layout _
358     | CicNotationPt.Literal _ -> assert false (* god bless Bologna *)
359   and aux_option ~localize loc context annotation = function
360     | None -> NCic.Implicit annotation
361     | Some (CicNotationPt.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 (CicNotationPt.AttributedTerm (_, term)) ->
367         aux_option ~localize loc context annotation (Some term)
368     | Some CicNotationPt.Implicit -> NCic.Implicit annotation
369     | Some term -> aux ~localize loc context term
370   in
371    (fun ~context -> aux ~localize:true HExtlib.dummy_floc context),
372    (fun ~context -> aux_option ~localize:true HExtlib.dummy_floc context)
373 ;;
374
375 let interpretate_term ?(create_dummy_ids=false) ~context ~env ~uri ~is_path ast
376      ~obj_context ~localization_tbl ~mk_choice
377 =
378   let context = List.map fst context in
379   fst 
380     (interpretate_term_and_interpretate_term_option 
381       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path ~localization_tbl)
382     ~context ast
383 ;;
384
385 let interpretate_term_option 
386   ?(create_dummy_ids=false) ~context ~env ~uri ~is_path 
387   ~localization_tbl ~mk_choice ~obj_context
388 =
389   let context = List.map fst context in
390   snd 
391     (interpretate_term_and_interpretate_term_option 
392       ~obj_context ~mk_choice ~create_dummy_ids ~env ~uri ~is_path ~localization_tbl)
393     ~context 
394 ;;
395
396 let disambiguate_path path =
397   let localization_tbl = NCicUntrusted.NCicHash.create 23 in
398   fst
399     (interpretate_term_and_interpretate_term_option 
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 new_flavour_of_flavour = function 
406   | `Definition -> `Definition
407   | `MutualDefinition -> `Definition 
408   | `Fact -> `Fact
409   | `Lemma -> `Lemma
410   | `Remark -> `Corollary
411   | `Theorem -> `Theorem
412   | `Variant -> `Corollary 
413   | `Axiom -> `Fact
414 ;;
415
416 let ncic_name_of_ident = function
417   | Ast.Ident (name, None) -> name
418   | _ -> assert false
419 ;;
420
421 let interpretate_obj 
422 (*      ?(create_dummy_ids=false)  *)
423      ~context ~env ~uri ~is_path obj ~localization_tbl ~mk_choice 
424 =
425  assert (context = []);
426  assert (is_path = false);
427  let interpretate_term ~obj_context =
428   interpretate_term ~mk_choice ~localization_tbl ~obj_context in
429  let interpretate_term_option ~obj_context =
430    interpretate_term_option ~mk_choice ~localization_tbl ~obj_context in
431  let uri = match uri with | None -> assert false | Some u -> u in
432  match obj with
433  | CicNotationPt.Theorem (flavour, name, ty, bo) ->
434      let ty' = 
435        interpretate_term 
436          ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false ty 
437      in
438      let height = (* XXX calculate *) 0 in
439      uri, height, [], [], 
440      (match bo,flavour with
441       | None,`Axiom -> 
442           let attrs = `Provided, new_flavour_of_flavour flavour, `Regular in
443           NCic.Constant ([],name,None,ty',attrs)
444       | Some _,`Axiom -> assert false
445       | None,_ ->
446           let attrs = `Provided, new_flavour_of_flavour flavour, `Regular in
447           NCic.Constant ([],name,Some (NCic.Implicit `Term),ty',attrs)
448       | Some bo,_ ->
449         (match bo with
450          | CicNotationPt.LetRec (kind, defs, _) ->
451              let inductive = kind = `Inductive in
452              let _,obj_context =
453                List.fold_left
454                  (fun (i,acc) (_,(name,_),_,k) -> 
455                   (i+1, 
456                     (ncic_name_of_ident name, NReference.reference_of_spec uri 
457                      (if inductive then NReference.Fix (i,k,0)
458                       else NReference.CoFix i)) :: acc))
459                  (0,[]) defs
460              in
461              let inductiveFuns =
462                List.map
463                  (fun (params, (name, typ), body, decr_idx) ->
464                    let add_binders kind t =
465                     List.fold_right
466                      (fun var t -> 
467                         CicNotationPt.Binder (kind, var, t)) params t
468                    in
469                    let cic_body =
470                      interpretate_term 
471                        ~obj_context ~context ~env ~uri:None ~is_path:false
472                        (add_binders `Lambda body) 
473                    in
474                    let cic_type =
475                      interpretate_term_option 
476                        ~obj_context:[]
477                        ~context ~env ~uri:None ~is_path:false `Type
478                        (HExtlib.map_option (add_binders `Pi) typ)
479                    in
480                    ([],ncic_name_of_ident name, decr_idx, cic_type, cic_body))
481                  defs
482              in
483              let attrs = `Provided, new_flavour_of_flavour flavour in
484              NCic.Fixpoint (inductive,inductiveFuns,attrs)
485          | bo -> 
486              let bo = 
487                interpretate_term 
488                 ~obj_context:[] ~context:[] ~env ~uri:None ~is_path:false bo
489              in
490              let attrs = `Provided, new_flavour_of_flavour flavour, `Regular in
491              NCic.Constant ([],name,Some bo,ty',attrs)))
492  | CicNotationPt.Inductive (params,tyl) ->
493     let context,params =
494      let context,res =
495       List.fold_left
496        (fun (context,res) (name,t) ->
497          let t =
498           match t with
499              None -> CicNotationPt.Implicit
500            | Some t -> t in
501          let name = cic_name_of_name name in
502          let t =
503           interpretate_term ~obj_context:[] ~context ~env ~uri:None
504            ~is_path:false t
505          in
506           (name,NCic.Decl t)::context,(name,t)::res
507        ) ([],[]) params
508      in
509       context,List.rev res in
510     let add_params =
511      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
512     let leftno = List.length params in
513     let obj_context =
514      snd (
515       List.fold_left
516        (fun (i,res) (name,_,_,_) ->
517          let _,inductive,_,_ =
518           (* ??? *)
519           try List.hd tyl with Failure _ -> assert false in
520          let nref =
521           NReference.reference_of_spec uri (NReference.Ind (inductive,i,leftno))
522          in
523           i+1,(name,nref)::res)
524        (0,[]) tyl) in
525     let tyl =
526      List.map
527       (fun (name,_,ty,cl) ->
528         let ty' =
529          add_params
530          (interpretate_term ~obj_context:[] ~context ~env ~uri:None
531            ~is_path:false ty) in
532         let cl' =
533          List.map
534           (fun (name,ty) ->
535             let ty' =
536              add_params
537               (interpretate_term ~obj_context ~context ~env ~uri:None
538                 ~is_path:false ty) in
539             let relevance = [] in
540              relevance,name,ty'
541           ) cl in
542         let relevance = [] in
543          relevance,name,ty',cl'
544       ) tyl
545     in
546      let height = (* XXX calculate *) 0 in
547      let attrs = `Provided, `Regular in
548      uri, height, [], [], 
549      NCic.Inductive (true,leftno,tyl,attrs)
550  | CicNotationPt.Record (params,name,ty,fields) ->
551     let context,params =
552      let context,res =
553       List.fold_left
554        (fun (context,res) (name,t) ->
555          let t =
556           match t with
557              None -> CicNotationPt.Implicit
558            | Some t -> t in
559          let name = cic_name_of_name name in
560          let t =
561           interpretate_term ~obj_context:[] ~context ~env ~uri:None
562            ~is_path:false t
563          in
564           (name,NCic.Decl t)::context,(name,t)::res
565        ) ([],[]) params
566      in
567       context,List.rev res in
568     let add_params =
569      List.fold_right (fun (name,ty) t -> NCic.Prod (name,ty,t)) params in
570     let leftno = List.length params in
571     let ty' =
572      add_params
573       (interpretate_term ~obj_context:[] ~context ~env ~uri:None
574         ~is_path:false ty) in
575     let nref =
576      NReference.reference_of_spec uri (NReference.Ind (true,0,leftno)) in
577     let obj_context = [name,nref] in
578     let fields' =
579      snd (
580       List.fold_left
581        (fun (context,res) (name,ty,_coercion,_arity) ->
582          let ty =
583           interpretate_term ~obj_context ~context ~env ~uri:None
584            ~is_path:false ty in
585          let context' = (name,NCic.Decl ty)::context in
586           context',(name,ty)::res
587        ) (context,[]) fields) in
588     let concl =
589      let mutind = NCic.Const nref in
590      if params = [] then mutind
591      else
592       NCic.Appl
593        (mutind::mk_rels (List.length params) (List.length fields)) in
594     let con =
595      List.fold_left (fun t (name,ty) -> NCic.Prod (name,ty,t)) concl fields' in
596     let con' = add_params con in
597     let relevance = [] in
598     let tyl = [relevance,name,ty',[relevance,"mk_" ^ name,con']] in
599     let field_names = List.map (fun (x,_,y,z) -> x,y,z) fields in
600      let height = (* XXX calculate *) 0 in
601      let attrs = `Provided, `Record field_names in
602      uri, height, [], [], 
603      NCic.Inductive (true,leftno,tyl,attrs)
604 ;;
605
606 let disambiguate_term ~context ~metasenv ~subst ~expty
607    ~mk_implicit ~description_of_alias ~mk_choice
608    ~aliases ~universe ~coercion_db ~lookup_in_library 
609    (text,prefix_len,term) 
610  =
611   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
612    let res,b =
613     MultiPassDisambiguator.disambiguate_thing
614      ~freshen_thing:CicNotationUtil.freshen_term
615      ~context ~metasenv ~initial_ugraph:() ~aliases
616      ~mk_implicit ~description_of_alias
617      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
618      ~universe ~uri:None ~pp_thing:CicNotationPp.pp_term
619      ~passes:(MultiPassDisambiguator.passes ())
620      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_term
621      ~interpretate_thing:(interpretate_term ~obj_context:[] ~mk_choice (?create_dummy_ids:None))
622      ~refine_thing:(refine_term ~coercion_db) (text,prefix_len,term)
623      ~mk_localization_tbl ~expty ~subst
624    in
625     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
626 ;;
627
628 let disambiguate_obj 
629    ~mk_implicit ~description_of_alias ~mk_choice
630    ~aliases ~universe ~coercion_db ~lookup_in_library ~uri
631    (text,prefix_len,obj) 
632  =
633   let mk_localization_tbl x = NCicUntrusted.NCicHash.create x in
634    let res,b =
635     MultiPassDisambiguator.disambiguate_thing
636      ~freshen_thing:CicNotationUtil.freshen_obj
637      ~context:[] ~metasenv:[] ~subst:[] ~initial_ugraph:() ~aliases
638      ~mk_implicit ~description_of_alias
639      ~string_context_of_context:(List.map (fun (x,_) -> Some x))
640      ~universe 
641      ~uri:(Some uri)
642      ~pp_thing:(CicNotationPp.pp_obj CicNotationPp.pp_term)
643      ~passes:(MultiPassDisambiguator.passes ())
644      ~lookup_in_library ~domain_of_thing:Disambiguate.domain_of_obj
645      ~interpretate_thing:(interpretate_obj ~mk_choice)
646      ~refine_thing:(refine_obj ~coercion_db) 
647      (text,prefix_len,obj)
648      ~mk_localization_tbl ~expty:None
649    in
650     List.map (function (a,b,c,d,_) -> a,b,c,d) res, b
651 ;;
652 (*
653 let _ = 
654 let mk_type n = 
655   if n = 0 then
656      [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
657   else
658      [false, NUri.uri_of_string ("cic:/matita/pts/Type"^string_of_int n^".univ")]
659 in
660 let mk_cprop n = 
661   if n = 0 then 
662     [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
663   else
664     [false, NUri.uri_of_string ("cic:/matita/pts/CProp"^string_of_int n^".univ")]
665 in
666          NCicEnvironment.add_constraint true (mk_type 0) (mk_type 1);
667          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_cprop 1);
668          NCicEnvironment.add_constraint true (mk_cprop 0) (mk_type 1);
669          NCicEnvironment.add_constraint true (mk_type 0) (mk_cprop 1);
670          NCicEnvironment.add_constraint false (mk_cprop 0) (mk_type 0);
671          NCicEnvironment.add_constraint false (mk_type 0) (mk_cprop 0);
672
673          NCicEnvironment.add_constraint true (mk_type 1) (mk_type 2);
674          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_cprop 2);
675          NCicEnvironment.add_constraint true (mk_cprop 1) (mk_type 2);
676          NCicEnvironment.add_constraint true (mk_type 1) (mk_cprop 2);
677          NCicEnvironment.add_constraint false (mk_cprop 1) (mk_type 1);
678          NCicEnvironment.add_constraint false (mk_type 1) (mk_cprop 1);
679
680          NCicEnvironment.add_constraint true (mk_type 2) (mk_type 3);
681          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_cprop 3);
682          NCicEnvironment.add_constraint true (mk_cprop 2) (mk_type 3);
683          NCicEnvironment.add_constraint true (mk_type 2) (mk_cprop 3);
684          NCicEnvironment.add_constraint false (mk_cprop 2) (mk_type 2);
685          NCicEnvironment.add_constraint false (mk_type 2) (mk_cprop 2);
686
687          NCicEnvironment.add_constraint false (mk_cprop 3) (mk_type 3);
688          NCicEnvironment.add_constraint false (mk_type 3) (mk_cprop 3);
689
690 ;;
691 *)
692