Skip to content

Commit ea1b3f6

Browse files
committed
add remove clause + tests
1 parent c19dd60 commit ea1b3f6

File tree

17 files changed

+177
-50
lines changed

17 files changed

+177
-50
lines changed

src/API.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ module Utils = struct
13811381
| Some (`After,x) -> [After x]
13821382
| Some (`Before,x) -> [Before x]
13831383
| Some (`Replace,x) -> [Replace x]
1384+
| Some (`Remove,x) -> [Remove x]
13841385
| None -> []) in
13851386
[Program.Clause {
13861387
Clause.loc = loc;

src/API.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ module Utils : sig
12431243

12441244
(** Hackish, in particular the output should be a compiled program *)
12451245
val clause_of_term :
1246-
?name:string -> ?graft:([`After | `Before | `Replace] * string) ->
1246+
?name:string -> ?graft:([`After | `Before | `Replace | `Remove] * string) ->
12471247
depth:int -> Ast.Loc.t -> Data.term -> Ast.program
12481248

12491249
(** Lifting/restriction/beta (LOW LEVEL, don't use) *)

src/bl.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ module Array = struct
8888
in
8989
shift t (i+len-1)
9090

91+
let shift_left t i len =
92+
let rec shift t j =
93+
if j = len then t
94+
else shift (set t j (get t (j+1))) (j + 1)
95+
in
96+
shift t i
97+
9198
let rec length t = match !t with Diff(_,_,x) -> length x | Array a -> Array.length a
9299

93100
let of_list l = ref @@ Array (Array.of_list l)
@@ -147,6 +154,18 @@ let rec replace f x = function
147154
a (* bleah *)
148155
in
149156
aux 0
157+
let rec remove f = function
158+
| BCons (head,tail) when f head -> tail
159+
| BCons (head, tail) -> BCons (head, remove f tail)
160+
| BArray { len; data } as a ->
161+
let rec aux i =
162+
if i < len then
163+
if f data.(i) then BArray { len = len-1; data = Array.shift_left data i len }
164+
else aux (i+1)
165+
else
166+
a (* bleah *)
167+
in
168+
aux 0
150169

151170
let rec insert f x = function
152171
| BCons (head, tail) when f head <= 0 -> BCons (head, BCons(x,tail))

src/bl.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ val rcons : 'a -> 'a t -> 'a t
1919
(* O(n) space and time *)
2020
val copy : 'a t -> 'a t
2121

22-
(* These 2 are O(n) time, O(1) space. The test must succeed once *)
22+
(* These 3 are O(n) time, O(1) space. The test must succeed once *)
2323
val replace : ('a -> bool) -> 'a -> 'a t -> 'a t
24+
val remove : ('a -> bool) -> 'a t -> 'a t
2425
val insert : ('a -> int) -> 'a -> 'a t -> 'a t
2526

2627
type 'a scan

src/compiler.ml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ end = struct (* {{{ *)
668668
| Replace s :: rest ->
669669
if r.insertion <> None then duplicate_err "insertion";
670670
aux_attrs { r with insertion = Some (Replace s) } rest
671+
| Remove s :: rest ->
672+
if r.insertion <> None then duplicate_err "insertion";
673+
aux_attrs { r with insertion = Some (Remove s) } rest
671674
| If s :: rest ->
672675
if r.ifexpr <> None then duplicate_err "if";
673676
aux_attrs { r with ifexpr = Some s } rest
@@ -693,7 +696,7 @@ end = struct (* {{{ *)
693696
| If s :: rest ->
694697
if r.cifexpr <> None then duplicate_err "if";
695698
aux_chr { r with cifexpr = Some s } rest
696-
| (Before _ | After _ | Replace _ | External | Index _) as a :: _ -> illegal_err a
699+
| (Before _ | After _ | Replace _ | Remove _ | External | Index _) as a :: _ -> illegal_err a
697700
in
698701
let cid = Loc.show loc in
699702
{ c with Chr.attributes = aux_chr { cid; cifexpr = None } attributes }
@@ -724,7 +727,7 @@ end = struct (* {{{ *)
724727
| Some (Structured.Index _) -> duplicate_err "index"
725728
| Some _ -> error ~loc "external predicates cannot be indexed"
726729
end
727-
| (Before _ | After _ | Replace _ | Name _ | If _) as a :: _ -> illegal_err a
730+
| (Before _ | After _ | Replace _ | Remove _ | Name _ | If _) as a :: _ -> illegal_err a
728731
in
729732
let attributes = aux_tatt None attributes in
730733
let attributes =
@@ -2080,7 +2083,7 @@ module Assemble : sig
20802083
end = struct (* {{{ *)
20812084

20822085
let compile_clause_attributes ({ Ast.Clause.attributes = { Ast.Structured.id }} as c) =
2083-
{ c with Ast.Clause.attributes = { Assembled.id }}
2086+
{ c with attributes = { Assembled.id }}
20842087

20852088
let sort_insertion ~old_rev ~extra:l =
20862089
let add s { Ast.Clause.attributes = { Assembled.id }; loc } =
@@ -2096,18 +2099,19 @@ let compile_clause_attributes ({ Ast.Clause.attributes = { Ast.Structured.id }}
20962099
match l, loc_name with
20972100
| [],_ -> error ~loc:c.Ast.Clause.loc ("unable to graft this clause: no clause named " ^
20982101
match loc_name with
2099-
| Ast.Structured.Replace x -> x
2100-
| Ast.Structured.After x -> x
2101-
| Ast.Structured.Before x -> x)
2102-
| { Ast.Clause.attributes = { Assembled.id = Some n }} :: xs,
2103-
Ast.Structured.Replace name when n = name ->
2104-
c :: xs
2105-
| { Ast.Clause.attributes = { Assembled.id = Some n }} as x :: xs,
2102+
| Replace x | After x | Before x | Remove x -> x)
2103+
| { Ast.Clause.attributes = { Assembled.id = Some n }} as x :: xs, (* AFTER *)
21062104
Ast.Structured.After name when n = name ->
21072105
c :: x :: xs
2108-
| { Ast.Clause.attributes = { Assembled.id = Some n }} as x :: xs,
2109-
Ast.Structured.Before name when n = name ->
2106+
| { attributes = { Assembled.id = Some n }} as x :: xs, (* BEFORE *)
2107+
Before name when n = name ->
21102108
x :: c :: xs
2109+
| { attributes = { id = Some n }} :: xs, (* REPLACE *)
2110+
Replace name when n = name ->
2111+
c :: xs
2112+
| { attributes = { id = Some n }} :: xs, (* REMOVE *)
2113+
Remove name when n = name ->
2114+
c :: xs
21112115
| x :: xs, _ -> x :: insert loc_name c xs in
21122116
let rec aux_sort seen acc = function
21132117
| [] -> acc
@@ -2264,7 +2268,7 @@ let rec constants_of acc = function
22642268
let w_symbol_table s f x =
22652269
let table = Symbols.compile_table @@ State.get Symbols.table s in
22662270
let pp_ctx = { table; uv_names = ref (IntMap.empty,0) } in
2267-
Util.set_spaghetti_printer Util.pp_const (R.Pp.pp_constant ~pp_ctx);
2271+
Util.set_spaghetti_printer pp_const (R.Pp.pp_constant ~pp_ctx);
22682272
f x
22692273

22702274
(* Compiler passes *)

src/discrimination_tree.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ module Trie = struct
174174
map = map |> Ptmap.map (replace p x);
175175
}
176176

177+
let rec remove f = function
178+
| Node { data; other; listTailVariable; map } ->
179+
Node {
180+
data = data |> List.filter (fun x -> not (f x));
181+
other = other |> Option.map (remove f);
182+
listTailVariable = listTailVariable |> Option.map (remove f);
183+
map = map |> Ptmap.map (remove f);
184+
}
185+
177186
let add (a : Path.t) v t =
178187
let max = ref 0 in
179188
let rec ins ~pos = let x = Path.get a pos in function
@@ -392,6 +401,7 @@ let retrieve cmp_data path { t } =
392401
Bl.of_list @@ List.sort cmp_data r
393402

394403
let replace p x i = { i with t = Trie.replace p x i.t }
404+
let remove keep dt = { dt with t = Trie.remove keep dt.t}
395405

396406
module Internal = struct
397407
let kConstant = kConstant

src/discrimination_tree.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ val empty_dt : 'b list -> 'a t
5252
val retrieve : ('a -> 'a -> int) -> Path.t -> 'a t -> 'a Bl.scan
5353

5454
val replace : ('a -> bool) -> 'a -> 'a t -> 'a t
55+
val remove : ('a -> bool) -> 'a t -> 'a t
5556

5657
(***********************************************************)
5758
(* Printers *)

src/parser/ast.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type raw_attribute =
143143
| After of string
144144
| Before of string
145145
| Replace of string
146+
| Remove of string
146147
| External
147148
| Index of int list * string option
148149
[@@deriving show]
@@ -318,7 +319,7 @@ and attribute = {
318319
id : string option;
319320
ifexpr : string option;
320321
}
321-
and insertion = Before of string | After of string | Replace of string
322+
and insertion = Before of string | After of string | Replace of string | Remove of string
322323
and tattribute =
323324
| External
324325
| Index of int list * tindex option

src/parser/ast.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type raw_attribute =
7373
| After of string
7474
| Before of string
7575
| Replace of string
76+
| Remove of string
7677
| External
7778
| Index of int list * string option
7879
[@@ deriving show]
@@ -213,7 +214,7 @@ and attribute = {
213214
id : string option;
214215
ifexpr : string option;
215216
}
216-
and insertion = Before of string | After of string | Replace of string
217+
and insertion = Before of string | After of string | Replace of string | Remove of string
217218
and cattribute = {
218219
cid : string;
219220
cifexpr : string option

src/parser/grammar.mly

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ attribute:
300300
| AFTER; s = STRING { After s }
301301
| BEFORE; s = STRING { Before s }
302302
| REPLACE; s = STRING { Replace s }
303+
| REMOVE; s = STRING { Remove s }
303304
| EXTERNAL { External }
304305
| INDEX; LPAREN; l = nonempty_list(indexing) ; RPAREN; o = option(STRING) { Index (l,o) }
305306

@@ -400,6 +401,7 @@ constant:
400401
| BEFORE { Func.from_string "before" }
401402
| AFTER { Func.from_string "after" }
402403
| REPLACE { Func.from_string "replace" }
404+
| REMOVE { Func.from_string "remove" }
403405
| INDEX { Func.from_string "index" }
404406
| c = IO { Func.from_string @@ String.make 1 c }
405407
| CUT { Func.cutf }

0 commit comments

Comments
 (0)