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