Debian Codegraph @

tactics/dn.ml

raw symbols SCIP
1 open Util
2
3 type 'res lookup_res = Label of 'res | Nothing | Everything
4
5 module Make =
6 functor (Y : Map.OrderedType) ->
7 functor (Z : Map.OrderedType) ->
8 struct
9
10 module Y_tries = struct
11 type t = (Y.t * int) option
12 let compare x y =
13 match x,y with
14 None,None -> 0
15 | Some (l,n),Some (l',n') ->
16 let m = Y.compare l l' in
17 if Int.equal m 0 then
18 n-n'
19 else m
20 | Some(l,n),None -> 1
21 | None, Some(l,n) -> -1
22 end
23 module ZSet = Set.Make(Z)
24 module X_tries =
25 struct
26 type t = ZSet.t
27 let nil = ZSet.empty
28 let is_nil = ZSet.is_empty
29 let add = ZSet.union
30 let sub = ZSet.diff
31 end
32
33 module Trie = Trie.Make(Y_tries)(X_tries)
34
35 type 'a decompose_fun = 'a -> (Y.t * 'a list) option
36
37 type 'tree lookup_fun = 'tree -> (Y.t * 'tree list) lookup_res
38
39 type t = Trie.t
40
41 type pattern = (Y.t * int) option list
42
43 let empty = Trie.empty
44
45 (* [path_of dna pat] returns the list of nodes of the pattern [pat] read in
46 prefix ordering, [dna] is the function returning the main node of a pattern *)
47
48 let path_of dna =
49 let rec path_of_deferred = function
50 | [] -> []
51 | h::tl -> pathrec tl h
52
53 and pathrec deferred t =
54 match dna t with
55 | None ->
56 None :: (path_of_deferred deferred)
57 | Some (lbl,[]) ->
58 (Some (lbl,0))::(path_of_deferred deferred)
59 | Some (lbl,(h::def_subl as v)) ->
60 (Some (lbl,List.length v))::(pathrec (def_subl@deferred) h)
61 in
62 pathrec []
63
64 let tm_of tm lbl =
65 try [Trie.next tm lbl] with Not_found -> []
66
67 let rec skip_arg n tm =
68 if Int.equal n 0 then [tm]
69 else
70 let labels = Trie.labels tm in
71 let map lbl = match lbl with
72 | None -> skip_arg (pred n) (Trie.next tm lbl)
73 | Some (_, m) ->
74 skip_arg (pred n + m) (Trie.next tm lbl)
75 in
76 List.map_append map labels
77
78 let lookup tm dna t =
79 let rec lookrec t tm =
80 match dna t with
81 | Nothing -> tm_of tm None
82 | Label(lbl,v) ->
83 let fold accu c = List.map_append (fun tm -> lookrec c tm) accu in
84 tm_of tm None @
85 (List.fold_left fold (tm_of tm (Some (lbl, List.length v))) v)
86 | Everything -> skip_arg 1 tm
87 in
88 List.map_append (fun tm -> ZSet.elements (Trie.get tm)) (lookrec t tm)
89
90 let pattern dna pat = path_of dna pat
91
92 let add tm p inf =
93 Trie.add p (ZSet.singleton inf) tm
94
95 let rmv tm p inf =
96 Trie.remove p (ZSet.singleton inf) tm
97
98 end
99