]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_refiner/nDiscriminationTree.ml
Porting to ocaml 5
[helm.git] / matita / components / ng_refiner / nDiscriminationTree.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id$ *)
27
28 open Discrimination_tree
29
30 module TermOT : Set.OrderedType with type t = NCic.term = struct 
31   type t = NCic.term 
32   let compare = Stdlib.compare 
33 end
34
35 module TermSet = Set.Make(TermOT)
36
37 module TermListOT : Set.OrderedType with type t = NCic.term list =
38  struct
39    type t = NCic.term list
40    let compare = Stdlib.compare
41  end
42
43 module TermListSet : Set.S with type elt = NCic.term list =
44  Set.Make(TermListOT)
45
46
47 module NCicIndexable : Indexable
48 with type input = NCic.term 
49 and type constant_name = NReference.reference = struct
50
51 type input = NCic.term
52 type constant_name = NReference.reference
53
54 let ppelem = function
55   | Constant (ref,arity) -> 
56       "("^NReference.string_of_reference ref ^ "," ^ string_of_int arity^")"
57   | Bound (i,arity) -> 
58       "("^string_of_int i ^ "," ^ string_of_int arity^")"
59   | Variable -> "?"
60   | Proposition -> "Prop"
61   | Datatype -> "Type"
62   | Dead -> "Dead"
63 ;;
64
65 let path_string_of t =
66   let rec aux arity depth = function
67     | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
68     | NCic.Appl (NCic.Match _::_) -> [Dead]
69     | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
70     | NCic.Appl [] -> assert false
71     | NCic.Appl l when depth > 10 || List.length l > 50 -> [Variable]
72     | NCic.Appl (hd::tl) ->
73        aux (List.length tl) depth hd @ 
74        List.flatten (List.map (aux 0 (depth+1)) tl)
75     | NCic.Lambda _ -> [Variable]
76         (* I think we should CicSubstitution.subst Implicit t *)
77     | NCic.LetIn _ -> [Variable] (* z-reduce? *)
78     | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
79     | NCic.Rel i -> [Bound (i, arity)]
80     | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
81     | NCic.Sort _ -> assert (arity=0); [Datatype]
82     | NCic.Const (u) -> [Constant (u, arity)]
83     (* Prod is used for coercions to funclass, ?->?       *)
84     (*  so it should not be unifiable with any other term *)
85     | NCic.Match _ | NCic.Prod _ -> [Dead] 
86   in 
87   aux 0 0 t
88 ;;
89
90 let compare e1 e2 =
91   match e1,e2 with
92   | Constant (u1,a1),Constant (u2,a2) -> 
93        let x = NReference.compare u1 u2 in
94        if x = 0 then Stdlib.compare a1 a2 else x
95   | e1,e2 -> Stdlib.compare e1 e2
96 ;;
97
98 let string_of_path l = String.concat "." (List.map ppelem l) ;;
99
100 end
101
102 module DiscriminationTree = Make(NCicIndexable)(TermSet)