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