1
(* -*- compile-command: "make -C .. bin/coqtop.byte" -*- *)
2
(************************************************************************)
3
(* v * The Coq Proof Assistant / The Coq Development Team *)
4
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
5
(* \VV/ **************************************************************)
6
(* // * This file is distributed under the terms of the *)
7
(* * GNU Lesser General Public License Version 2.1 *)
8
(************************************************************************)
10
(* $Id: dn.ml 11282 2008-07-28 11:51:53Z msozeau $ *)
12
(* This file implements the basic structure of what Chet called
13
``discrimination nets''. If my understanding is right, it serves
14
to associate actions (for example, tactics) with a priority to term
15
patterns, so that if a hypothesis matches a pattern in the net,
16
then the associated tactic is applied. Discrimination nets are used
17
(only) to implement the tactics Auto, DHyp and Point.
19
A discrimination net is a tries structure, that is, a tree structure
20
specially conceived for searching patterns, like for example strings
21
--see the file Tlm.ml in the directory lib/util--. Here the tries
22
structure are used for looking for term patterns.
24
This module is then used in :
25
- termdn.ml (discrimination nets of terms);
26
- btermdn.ml (discrimination nets of terms with bounded depth,
27
used in the tactic auto);
28
- nbtermdn.ml (named discrimination nets with bounded depth, used
29
in the tactics Dhyp and Point).
32
(* Definition of the basic structure *)
34
type ('lbl,'pat) decompose_fun = 'pat -> ('lbl * 'pat list) option
36
type 'res lookup_res = Label of 'res | Nothing | Everything
38
type ('lbl,'tree) lookup_fun = 'tree -> ('lbl * 'tree list) lookup_res
40
type ('lbl,'pat,'inf) t = (('lbl * int) option,'pat * 'inf) Tlm.t
42
let create () = Tlm.empty
44
(* [path_of dna pat] returns the list of nodes of the pattern [pat] read in
45
prefix ordering, [dna] is the function returning the main node of a pattern *)
48
let rec path_of_deferred = function
50
| h::tl -> pathrec tl h
52
and pathrec deferred t =
55
None :: (path_of_deferred deferred)
57
(Some (lbl,0))::(path_of_deferred deferred)
58
| Some (lbl,(h::def_subl as v)) ->
59
(Some (lbl,List.length v))::(pathrec (def_subl@deferred) h)
64
try [Tlm.map tm lbl, true] with Not_found -> []
66
let rec skip_arg n tm =
67
if n = 0 then [tm,true]
71
(fun a -> match a with
72
| None -> skip_arg (pred n) (Tlm.map tm a)
74
skip_arg (pred n + m) (Tlm.map tm a))
78
let rec lookrec t tm =
80
| Nothing -> tm_of tm None
85
List.flatten(List.map (fun (tm, b) ->
86
if b then lookrec c tm
88
(tm_of tm (Some(lbl,List.length v))) v)
89
| Everything -> skip_arg 1 tm
91
List.flatten (List.map (fun (tm,b) -> Tlm.xtract tm) (lookrec t tm))
93
let add tm dna (pat,inf) =
94
let p = path_of dna pat in Tlm.add tm (p,(pat,inf))
96
let rmv tm dna (pat,inf) =
97
let p = path_of dna pat in Tlm.rmv tm (p,(pat,inf))
99
let app f tm = Tlm.app (fun (_,p) -> f p) tm