]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicUntrusted.ml
the iterator was wrongly processing the application
[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 a,l1 = 
26       (* sharing fold? *)
27       List.fold_right 
28         (fun t (a,l) -> let a,t = f k a t in a, t :: l) 
29         l (a,[])
30     in
31     a, if l1 == l then orig else (match l1 with
32        | C.Appl l :: tl -> C.Appl (l@tl)
33        | _ -> C.Appl l1)
34  | C.Prod (n,s,t) as orig ->
35      let a,s1 = f k a s in let a,t1 = f (g (n,C.Decl s) k) a t in
36      a, if t1 == t && s1 == s then orig else C.Prod (n,s1,t1)
37  | C.Lambda (n,s,t) as orig -> 
38      let a,s1 = f k a s in let a,t1 = f (g (n,C.Decl s) k) a t in
39      a, if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1)
40  | C.LetIn (n,ty,t,b) as orig -> 
41      let a,ty1 = f k a ty in let a,t1 = f k a t in
42      let a,b1 = f (g (n,C.Def (t,ty)) k) a b in
43      a, if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1)
44  | C.Match (r,oty,t,pl) as orig -> 
45      let a,oty1 = f k a oty in let a,t1 = f k a t in 
46      let a,pl1 = 
47        (* sharing fold? *)
48        List.fold_right (fun t (a,l) -> let a,t = f k a t in a,t::l) pl (a,[])
49      in
50      a, if oty1 == oty && t1 == t && pl1 == pl then orig 
51         else C.Match(r,oty1,t1,pl1)
52 ;;
53
54 let metas_of_term subst context term =
55   let rec aux ctx acc = function
56     | NCic.Rel i -> 
57          (match HExtlib.list_skip (i-1) ctx with
58          | (_,NCic.Def (bo,_)) :: ctx -> aux ctx acc bo
59          | _ -> acc)
60     | NCic.Meta(i,l) -> 
61          (try
62            let _,_,bo,_ = NCicUtils.lookup_subst i subst in
63            let bo = NCicSubstitution.subst_meta l bo in
64            aux ctx acc bo
65          with NCicUtils.Subst_not_found _ -> 
66             let shift, lc = l in
67             let lc = NCicUtils.expand_local_context lc in
68             let l = List.map (NCicSubstitution.lift shift) lc in
69             List.fold_left (aux ctx) (i::acc) l)
70     | t -> NCicUtils.fold (fun e c -> e::c) ctx aux acc t
71   in
72     HExtlib.list_uniq (List.sort Pervasives.compare (aux context [] term))
73 ;;
74