]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicUntrusted.ml
- pretty printer made robust in face of list_nth
[helm.git] / helm / software / components / ng_kernel / nCicUntrusted.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$ *)
13
14 module C = NCic
15 module Ref = NReference
16
17 let map_term_fold_a g k f a = function
18  | C.Meta _ -> assert false
19  | C.Implicit _
20  | C.Sort _
21  | C.Const _
22  | C.Rel _ as t -> a,t
23  | C.Appl [] | C.Appl [_] -> assert false
24  | C.Appl l as orig ->
25     let fire_beta, upto = 
26       match l with C.Meta _ :: _ -> true, List.length l - 1 | _ -> false, 0 
27     in
28     let a,l1 = 
29       (* sharing fold? *)
30       List.fold_right 
31         (fun t (a,l) -> let a,t = f k a t in a, t :: l) 
32         l (a,[])
33     in
34     a, if l1 == l then orig else 
35        let t =
36         match l1 with
37          | C.Appl l :: tl -> C.Appl (l@tl)
38          | _ -> C.Appl l1
39        in
40         if fire_beta then NCicReduction.head_beta_reduce ~upto t
41         else t
42  | C.Prod (n,s,t) as orig ->
43      let a,s1 = f k a s in let a,t1 = f (g (n,C.Decl s) k) a t in
44      a, if t1 == t && s1 == s then orig else C.Prod (n,s1,t1)
45  | C.Lambda (n,s,t) as orig -> 
46      let a,s1 = f k a s in let a,t1 = f (g (n,C.Decl s) k) a t in
47      a, if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1)
48  | C.LetIn (n,ty,t,b) as orig -> 
49      let a,ty1 = f k a ty in let a,t1 = f k a t in
50      let a,b1 = f (g (n,C.Def (t,ty)) k) a b in
51      a, if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1)
52  | C.Match (r,oty,t,pl) as orig -> 
53      let a,oty1 = f k a oty in let a,t1 = f k a t in 
54      let a,pl1 = 
55        (* sharing fold? *)
56        List.fold_right (fun t (a,l) -> let a,t = f k a t in a,t::l) pl (a,[])
57      in
58      a, if oty1 == oty && t1 == t && pl1 == pl then orig 
59         else C.Match(r,oty1,t1,pl1)
60 ;;
61
62 let metas_of_term subst context term =
63   let rec aux ctx acc = function
64     | NCic.Rel i -> 
65          (match HExtlib.list_skip (i-1) ctx with
66          | (_,NCic.Def (bo,_)) :: ctx -> aux ctx acc bo
67          | _ -> acc)
68     | NCic.Meta(i,l) -> 
69          (try
70            let _,_,bo,_ = NCicUtils.lookup_subst i subst in
71            let bo = NCicSubstitution.subst_meta l bo in
72            aux ctx acc bo
73          with NCicUtils.Subst_not_found _ -> 
74             let shift, lc = l in
75             let lc = NCicUtils.expand_local_context lc in
76             let l = List.map (NCicSubstitution.lift shift) lc in
77             List.fold_left (aux ctx) (i::acc) l)
78     | t -> NCicUtils.fold (fun e c -> e::c) ctx aux acc t
79   in
80     HExtlib.list_uniq (List.sort Pervasives.compare (aux context [] term))
81 ;;
82
83 module NCicHash =
84  Hashtbl.Make
85   (struct
86     type t = C.term
87     let equal = (==)
88     let hash = Hashtbl.hash_param 100 1000 
89    end)
90 ;;
91
92 let mk_appl he args = 
93   if args = [] then he else
94   match he with
95   | NCic.Appl l -> NCic.Appl (l@args)
96   | _ -> NCic.Appl (he::args)
97 ;;
98
99 let map_obj_kind f =
100  function
101     NCic.Constant (relev,name,bo,ty,attrs) ->
102      NCic.Constant (relev,name,HExtlib.map_option f bo, f ty,attrs)
103   | NCic.Fixpoint (ind,fl,attrs) ->
104      let fl =
105       List.map
106        (function (relevance,name,recno,ty,bo) -> relevance,name,recno,f ty,f bo)
107        fl
108      in
109       NCic.Fixpoint (ind,fl,attrs)
110   | NCic.Inductive (is_ind,lno,itl,attrs) ->
111       let itl = 
112         List.map
113          (fun (relevance,name,ty,cl) ->
114            let cl = 
115              List.map (fun (relevance, name, ty) ->
116                 relevance, name, f ty)
117              cl
118            in
119            relevance, name, f ty, cl)
120          itl
121       in
122       NCic.Inductive (is_ind,lno,itl,attrs)
123 ;;
124
125 let apply_subst subst t = 
126  let rec apply_subst subst () =
127  function
128     NCic.Meta (i,lc) ->
129      (try
130        let _,_,t,_ = NCicUtils.lookup_subst i subst in
131        let t = NCicSubstitution.subst_meta lc t in
132         apply_subst subst () t
133       with
134        Not_found ->
135         match lc with
136            _,NCic.Irl _ -> NCic.Meta (i,lc)
137          | n,NCic.Ctx l ->
138             NCic.Meta
139              (i,(0,NCic.Ctx
140                  (List.map (fun t ->
141                    apply_subst subst () (NCicSubstitution.lift n t)) l))))
142   | t -> NCicUtils.map (fun _ () -> ()) () (apply_subst subst) t
143  in
144   apply_subst subst () t
145 ;;
146