]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_paramodulation/nCicBlob.ml
b1f2cfa6f2d9cb4774a56c3e89862209759b8983
[helm.git] / matita / components / ng_paramodulation / nCicBlob.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: terms.mli 9822 2009-06-03 15:37:06Z tassi $ *)
13
14 let eqPref = ref (fun _ -> assert false);;
15 let set_eqP t = eqPref := fun _ -> t;;
16
17 let default_eqP() =
18   let uri = NUri.uri_of_string "cic:/matita/basics/logic/eq.ind" in
19   let ref = NReference.reference_of_spec uri (NReference.Ind(true,0,2)) in
20     NCic.Const ref
21 ;;
22
23 let equivalence_relation =
24   let uri = NUri.uri_of_string "cic:/matita/ng/properties/relations/eq_rel.con"
25   in
26   let ref = NReference.reference_of_spec uri (NReference.Fix(0,1,2)) 
27   in NCic.Const ref
28
29 let setoid_eq =
30   let uri = NUri.uri_of_string "cic:/matita/ng/sets/setoids/eq.con" in
31   let ref = NReference.reference_of_spec uri (NReference.Fix(0,0,2)) 
32   in NCic.Const ref
33
34 let set_default_eqP() = eqPref := default_eqP
35
36 module type NCicContext =
37   sig
38     val metasenv : NCic.metasenv
39     val subst : NCic.substitution
40     val context : NCic.context
41   end
42
43 module NCicBlob(C : NCicContext) : Terms.Blob 
44 with type t = NCic.term and type input = NCic.term = struct
45
46   type t = NCic.term
47
48   let eq x y =
49    (* CSC: NCicPp.status is the best I can put here *)
50    x = y ||
51    NCicReduction.alpha_eq (new NCicPp.status) C.metasenv C.subst C.context x y;;
52
53   let height_of_ref = function
54     | NReference.Def h -> h
55     | NReference.Fix(_,_,h) -> h
56     | _ -> 0
57
58   external old_hash_param :
59     int -> int -> 'a -> int = "caml_hash_univ_param" (*[@@noalloc]*);;
60
61   let old_hash = old_hash_param 10 100;;
62
63   let compare_refs (NReference.Ref (u1,r1)) (NReference.Ref (u2,r2)) =
64     let x = height_of_ref r2 - height_of_ref r1 in
65       if x = 0 then 
66         old_hash (NUri.string_of_uri u1,r1) - 
67           old_hash (NUri.string_of_uri u2,r2)
68       else x 
69
70   let rec compare x y = 
71     match x,y with
72     | NCic.Rel i, NCic.Rel j -> j-i
73     | NCic.Meta (i,_), NCic.Meta (j,_) -> i-j
74     | NCic.Const r1, NCic.Const r2 -> compare_refs r1 r2
75     (*NReference.compare r1 r2*)
76     | NCic.Appl l1, NCic.Appl l2 -> FoUtils.lexicograph compare l1 l2
77     | NCic.Rel _, ( NCic.Meta _ | NCic.Const _ | NCic.Appl _ ) -> ~-1
78     | ( NCic.Meta _ | NCic.Const _ | NCic.Appl _ ), NCic.Rel _ -> 1
79     | NCic.Const _, ( NCic.Meta _ | NCic.Appl _ ) -> ~-1
80     | ( NCic.Meta _ | NCic.Appl _ ), NCic.Const _ -> 1
81     | NCic.Appl _, NCic.Meta _ -> ~-1
82     | NCic.Meta _, NCic.Appl _ -> 1
83     | _ -> Pervasives.compare x y
84         (* was assert false, but why? *)
85         
86   ;;
87   
88   let compare x y = 
89     if eq x y then 0
90     else compare x y
91   ;;
92
93   let eqP = (!eqPref)()
94   ;;
95
96   let is_eq = function
97     | Terms.Node [ Terms.Leaf eqt ; ty; l; r ] when eq eqP eqt ->
98         Some (ty,l,r) 
99 (*
100     | Terms.Node [ Terms.Leaf eqt ; _; Terms.Node [Terms.Leaf eqt2 ; ty]; l; r]
101         when eq equivalence_relation eqt && eq setoid_eq eqt2 ->
102         Some (ty,l,r) *)
103     | _ -> None
104
105   let pp t = 
106     (* CSC: NCicPp.status is the best I can put here *)
107     (new NCicPp.status)#ppterm ~context:C.context
108       ~metasenv:C.metasenv ~subst:C.subst t;;
109
110   type input = NCic.term
111
112   let rec embed = function
113     | NCic.Meta (i,_) -> Terms.Var i, [i]
114     | NCic.Appl l ->
115         let rec aux acc l = function
116           |[] -> acc@l
117           |hd::tl -> if List.mem hd l then aux acc l tl else aux (hd::acc) l tl
118         in
119         let res,vars = List.fold_left
120           (fun (r,v) t -> let r1,v1 = embed t in (r1::r),aux [] v v1) ([],[]) l
121         in (Terms.Node (List.rev res)), vars
122     | t -> Terms.Leaf t, []
123   ;;
124
125   let embed t = fst (embed t) ;;
126
127   let saturate t ty = 
128     let sty, _, args = 
129       (* CSC: NCicPp.status is the best I can put here *)
130       NCicMetaSubst.saturate (new NCicPp.status) ~delta:0 C.metasenv C.subst
131        C.context ty 0
132     in
133     let proof = 
134       if args = [] then Terms.Leaf t 
135       else Terms.Node (Terms.Leaf t :: List.map embed args)
136     in
137     let sty = embed sty in
138     proof, sty
139   ;;
140   
141  end