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