-(*
- ||M|| This file is part of HELM, an Hypertextual, Electronic
- ||A|| Library of Mathematics, developed at the Computer Science
- ||T|| Department, University of Bologna, Italy.
- ||I||
- ||T|| HELM is free software; you can redistribute it and/or
- ||A|| modify it under the terms of the GNU General Public License
- \ / version 2 or (at your option) any later version.
- \ / This software is distributed as is, NO WARRANTY.
- V_______________________________________________________________ *)
+(* Copyright (C) 2005, HELM Team.
+ *
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ *
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ *
+ * For details, see the HELM World-Wide-Web page,
+ * http://cs.unibo.it/helm/.
+ *)
(* $Id$ *)
type path_string_elem =
- | Constant of NUri.uri
- | Bound of int
- | Variable
- | Proposition
- | Datatype
- | Dead of NCic.term
+ | Constant of NUri.uri * int (* name, arity *)
+ | Bound of int * int (* rel, arity *)
+ | Variable (* arity is 0 *)
+ | Proposition (* arity is 0 *)
+ | Datatype (* arity is 0 *)
+ | Dead (* arity is 0 *)
+;;
+
+let arity_of = function
+ | Constant (_,a)
+ | Bound (_,a) -> a
+ | _ -> 0
;;
-type path_string = path_string_elem list;;
+type path = path_string_elem list;;
let ppelem = function
- | Constant uri -> NUri.name_of_uri uri
- | Bound i -> string_of_int i
+ | Constant (uri,arity) ->
+ "("^NUri.name_of_uri uri ^ "," ^ string_of_int arity^")"
+ | Bound (i,arity) ->
+ "("^string_of_int i ^ "," ^ string_of_int arity^")"
| Variable -> "?"
| Proposition -> "Prop"
| Datatype -> "Type"
- | Dead t ->
- "DEAD("^NCicPp.ppterm ~context:[] ~subst:[] ~metasenv:[] t^")"
+ | Dead -> "Dead"
;;
-let pp_path_string l = String.concat "::" (List.map ppelem l) ;;
-
-let elem_of_cic = function
- | NCic.Meta _ | NCic.Implicit _ -> Variable
- | NCic.Rel i -> Bound i
- | NCic.Sort (NCic.Prop) -> Proposition
- | NCic.Sort _ -> Datatype
- | NCic.Const (NReference.Ref (u,_)) -> Constant u
- | NCic.Appl _ -> assert false (* should not happen *)
- | NCic.LetIn _ | NCic.Lambda _ | NCic.Prod _ | NCic.Match _ as t ->
- prerr_endline
- "FIXME: the discrimination tree receives an invalid term";
- Dead t
-;;
-
-let path_string_of_term arities =
- let set_arity arities k n =
- (assert (k<>Variable || n=0);
- match k with
- | Dead _ -> arities
- | _ ->
- (* here we override, but partial instantiation may trick us *)
- (k,n)::(List.remove_assoc k arities))
- in
- let rec aux arities = function
- | NCic.Appl ((hd::tl) as l) ->
- let arities =
- set_arity arities (elem_of_cic hd) (List.length tl)
- in
- List.fold_left
- (fun (arities,path) t ->
- let arities,tpath = aux arities t in
- arities,path@tpath)
- (arities,[]) l
- | t -> arities, [elem_of_cic t]
+let path_string_of_term_with_jl =
+ let rec aux arity = function
+ | NCic.Appl ((NCic.Meta _|NCic.Implicit _)::_) -> [Variable]
+ | NCic.Appl (NCic.Lambda _ :: _) -> [Variable] (* maybe we should b-reduce *)
+ | NCic.Appl [] -> assert false
+ | NCic.Appl (hd::tl) ->
+ aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl)
+ | NCic.Lambda _ | NCic.Prod _ -> [Variable]
+ (* I think we should CicSubstitution.subst Implicit t *)
+ | NCic.LetIn _ -> [Variable] (* z-reduce? *)
+ | NCic.Meta _ | NCic.Implicit _ -> assert (arity = 0); [Variable]
+ | NCic.Rel i -> [Bound (i, arity)]
+ | NCic.Sort (NCic.Prop) -> assert (arity=0); [Proposition]
+ | NCic.Sort _ -> assert (arity=0); [Datatype]
+ | NCic.Const (NReference.Ref (u,_)) -> [Constant (u, arity)]
+ | NCic.Match _ -> [Dead]
in
- aux arities
+ aux 0
;;
let compare_elem e1 e2 =
match e1,e2 with
- | Constant u1,Constant u2 -> NUri.compare u1 u2
+ | Constant (u1,a1),Constant (u2,a2) ->
+ let x = NUri.compare u1 u2 in
+ if x = 0 then Pervasives.compare a1 a2 else x
| e1,e2 -> Pervasives.compare e1 e2
;;
-let head_of_term = function
- | NCic.Appl (hd::tl) -> hd
- | term -> term
-;;
-
-let rec skip_prods = function
- | NCic.Prod (_,_,t) -> skip_prods t
- | term -> term
-;;
-
+let string_of_path l = String.concat "." (List.map ppelem l) ;;
module DiscriminationTreeIndexing =
functor (A:Set.S) ->
module DiscriminationTree = Trie.Make(PSMap);;
- type t = A.t DiscriminationTree.t * (path_string_elem*int) list
+ type t = A.t DiscriminationTree.t
- let empty = DiscriminationTree.empty, [] ;;
+ let empty = DiscriminationTree.empty;;
- let iter (dt, _ ) f =
- DiscriminationTree.iter (fun y x -> f y x) dt
- ;;
+ let iter dt f = DiscriminationTree.iter (fun p x -> f p x) dt;;
- let index (tree,arity) term info =
- let arity,ps = path_string_of_term arity term in
+ let index tree term info =
+ let ps = path_string_of_term_with_jl term in
let ps_set =
- try DiscriminationTree.find ps tree
- with Not_found -> A.empty in
- let tree = DiscriminationTree.add ps (A.add info ps_set) tree in
- tree,arity
+ try DiscriminationTree.find ps tree with Not_found -> A.empty
+ in
+ DiscriminationTree.add ps (A.add info ps_set) tree
;;
- let remove_index (tree,arity) term info =
- let arity,ps = path_string_of_term arity term in
+ let remove_index tree term info =
+ let ps = path_string_of_term_with_jl term in
try
let ps_set = A.remove info (DiscriminationTree.find ps tree) in
- if A.is_empty ps_set then
- DiscriminationTree.remove ps tree,arity
- else
- DiscriminationTree.add ps ps_set tree,arity
- with Not_found ->
- tree,arity
+ if A.is_empty ps_set then DiscriminationTree.remove ps tree
+ else DiscriminationTree.add ps ps_set tree
+ with Not_found -> tree
;;
- let in_index (tree,arity) term test =
- let arity,ps = path_string_of_term arity term in
+ let in_index tree term test =
+ let ps = path_string_of_term_with_jl term in
try
let ps_set = DiscriminationTree.find ps tree in
A.exists test ps_set
- with Not_found ->
- false
+ with Not_found -> false
;;
- let rec subterm_at_pos pos term =
- match pos with
- | [] -> term
- | index::pos ->
- match term with
- | NCic.Appl l ->
- (try subterm_at_pos pos (List.nth l index)
- with Failure _ -> raise Not_found)
- | _ -> raise Not_found
+ (* You have h(f(x,g(y,z)),t) whose path_string_of_term_with_jl is
+ (h,2).(f,2).(x,0).(g,2).(y,0).(z,0).(t,0) and you are at f and want to
+ skip all its progeny, thus you want to reach t.
+
+ You need to skip as many elements as the sum of all arieties contained
+ in the progeny of f.
+
+ The input ariety is the one of f while the path is x.g....t
+ Should be the equivalent of after_t in the literature (handbook A.R.)
+ *)
+ let rec skip arity path =
+ if arity = 0 then path else match path with
+ | [] -> assert false
+ | m::tl -> skip (arity-1+arity_of m) tl
;;
-
- let rec after_t pos term =
- let pos' =
- match pos with
- | [] -> raise Not_found
- | pos ->
- List.fold_right
- (fun i r -> if r = [] then [i+1] else i::r) pos []
+ (* the equivalent of skip, but on the index, thus the list of trees
+ that are rooted just after the term represented by the tree root
+ are returned (we are skipping the root) *)
+ let skip_root = function DiscriminationTree.Node (_, map) ->
+ let rec get n = function DiscriminationTree.Node (_, m) as tree ->
+ if n = 0 then [tree] else
+ PSMap.fold (fun k v res -> (get (n-1 + arity_of k) v) @ res) m []
in
- try
- ignore(subterm_at_pos pos' term ); pos'
- with Not_found ->
- let pos, _ =
- List.fold_right
- (fun i (r, b) -> if b then (i::r, true) else (r, true))
- pos ([], false)
- in
- after_t pos term
+ PSMap.fold (fun k v res -> (get (arity_of k) v) @ res) map []
;;
-
- let next_t pos term =
- let t = subterm_at_pos pos term in
- try
- let _ = subterm_at_pos [1] t in
- pos @ [1]
- with Not_found ->
- match pos with
- | [] -> [1]
- | pos -> after_t pos term
- ;;
-
- let retrieve_generalizations (tree,arity) term =
- let term = skip_prods term in
- let rec retrieve tree term pos =
- match tree with
- | DiscriminationTree.Node (Some s, _) when pos = [] -> s
- | DiscriminationTree.Node (_, map) ->
- let res =
- let hd_term =
- elem_of_cic (head_of_term (subterm_at_pos pos term))
- in
- if hd_term = Variable then A.empty else
- try
- let n = PSMap.find hd_term map in
- match n with
- | DiscriminationTree.Node (Some s, _) -> s
- | DiscriminationTree.Node (None, _) ->
- let newpos =
- try next_t pos term
- with Not_found -> []
- in
- retrieve n term newpos
- with Not_found ->
- A.empty
- in
- try
- let n = PSMap.find Variable map in
- let newpos = try after_t pos term with Not_found -> [-1] in
- if newpos = [-1] then
- match n with
- | DiscriminationTree.Node (Some s, _) -> A.union s res
- | _ -> res
- else
- A.union res (retrieve n term newpos)
- with Not_found ->
- res
- in
- retrieve tree term []
+ let retrieve unif tree term =
+ let path = path_string_of_term_with_jl term in
+ let rec retrieve path tree =
+ match tree, path with
+ | DiscriminationTree.Node (Some s, _), [] -> s
+ | DiscriminationTree.Node (None, _), [] -> A.empty
+ | DiscriminationTree.Node _, Variable::path when unif ->
+ List.fold_left A.union A.empty
+ (List.map (retrieve path) (skip_root tree))
+ | DiscriminationTree.Node (_, map), node::path ->
+ A.union
+ (if not unif && node = Variable then A.empty else
+ try retrieve path (PSMap.find node map)
+ with Not_found -> A.empty)
+ (try
+ match PSMap.find Variable map,skip (arity_of node) path with
+ | DiscriminationTree.Node (Some s, _), [] -> s
+ | n, path -> retrieve path n
+ with Not_found -> A.empty)
+ in
+ retrieve path tree
;;
-
- let jump_list arities = function
- | DiscriminationTree.Node (value, map) ->
- let rec get n tree =
- match tree with
- | DiscriminationTree.Node (v, m) ->
- if n = 0 then
- [tree]
- else
- PSMap.fold
- (fun k v res ->
- let a =
- try List.assoc k arities
- with Not_found -> 0
- in
- (get (n-1 + a) v) @ res) m []
- in
- PSMap.fold
- (fun k v res ->
- let arity =
- try
- List.assoc k arities
- with Not_found -> 0 in
- (get arity v) @ res)
- map []
- ;;
-
-
- let retrieve_unifiables (tree,arities) term =
- let term = skip_prods term in
- let rec retrieve tree term pos =
- match tree with
- | DiscriminationTree.Node (Some s, _) when pos = [] -> s
- | DiscriminationTree.Node (_, map) ->
- let subterm =
- try Some (subterm_at_pos pos term) with Not_found -> None
- in
- match subterm with
- | None -> A.empty
- | Some (NCic.Meta _) ->
- let newpos = try next_t pos term with Not_found -> [] in
- let jl = jump_list arities tree in
- List.fold_left
- (fun r s -> A.union r s)
- A.empty
- (List.map (fun t -> retrieve t term newpos) jl)
- | Some subterm ->
- let res =
- let hd_term = elem_of_cic (head_of_term subterm) in
- if hd_term = Variable then
- A.empty else
- try
- let n = PSMap.find hd_term map in
- match n with
- | DiscriminationTree.Node (Some s, _) -> s
- | DiscriminationTree.Node (None, _) ->
- retrieve n term (next_t pos term)
- with Not_found ->
- A.empty
- in
- try
- let n = PSMap.find Variable map in
- let newpos =
- try after_t pos term
- with Not_found -> [-1]
- in
- if newpos = [-1] then
- match n with
- | DiscriminationTree.Node (Some s, _) ->
- A.union s res
- | _ -> res
- else
- A.union res (retrieve n term newpos)
- with Not_found ->
- res
- in
- retrieve tree term []
+ let retrieve_generalizations tree term = retrieve false tree term;;
+ let retrieve_unifiables tree term = retrieve true tree term;;
end
;;