Skip to content

Commit 919c130

Browse files
committed
Merge branch 'compile-clause-early' of github.com:LPCIC/elpi into compile-clause-early
# Conflicts: # src/bl.ml # src/bl.mli # src/compiler.ml # src/discrimination_tree.ml # src/discrimination_tree.mli # src/parser/ast.ml # src/parser/ast.mli # src/runtime.ml
2 parents dd9235e + ea1b3f6 commit 919c130

File tree

17 files changed

+145
-21
lines changed

17 files changed

+145
-21
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: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ module Array = struct
8888
in
8989
shift t (i+len-1)
9090

91-
92-
let shift_left1 t i len =
91+
let shift_left t i len =
9392
let rec shift t j =
94-
if i = len-1 then t
95-
else shift (set t j (get t (j+1))) (i+1)
93+
if j = len-1 then t
94+
else shift (set t j (get t (j+1))) (j + 1)
9695
in
9796
shift t i
9897

@@ -140,14 +139,26 @@ let rec rcons elt l =
140139
| BArray { len; data } when len < Array.length data -> BArray { len = len + 1; data = Array.set data len elt }
141140
| BArray { len; data } -> extend len data elt
142141

142+
let rec replace f x = function
143+
| BCons (head,tail) when f head -> BCons(x,tail)
144+
| BCons (head, tail) -> BCons (head, replace f x tail)
145+
| BArray { len; data } as a ->
146+
let rec aux i =
147+
if i < len then
148+
if f data.(i) then BArray { len; data = Array.set data i x }
149+
else aux (i+1)
150+
else
151+
a
152+
in
153+
aux 0
154+
143155
let rec remove f = function
144156
| BCons (head,tail) when f head -> tail
145157
| BCons (head, tail) -> BCons (head, remove f tail)
146158
| BArray { len; data } as a ->
147159
let rec aux i =
148160
if i < len then
149-
if f data.(i) then
150-
BArray { len = len - 1; data = Array.shift_left1 data i len }
161+
if f data.(i) then BArray { len = len-1; data = Array.shift_left data i len }
151162
else aux (i+1)
152163
else
153164
a

src/bl.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ val empty : unit -> 'a t
1515
val cons : 'a -> 'a t -> 'a t
1616
val rcons : 'a -> 'a t -> 'a t
1717

18+
val replace : ('a -> bool) -> 'a -> 'a t -> 'a t
19+
val remove : ('a -> bool) -> 'a t -> 'a t
1820
val insert : ('a -> int) -> 'a -> 'a t -> 'a t
1921
val remove : ('a -> bool) -> 'a t -> 'a t
2022

src/compiler.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ end = struct (* {{{ *)
655655
error ~loc ("illegal attribute " ^ show_raw_attribute a) in
656656
let illegal_replace s =
657657
error ~loc ("replacing clause for "^ s ^" cannot have a name attribute") in
658+
let illegal_remove_id s =
659+
error ~loc ("remove clause for "^ s ^" cannot have a name attribute") in
658660
let rec aux_attrs r = function
659661
| [] -> r
660662
| Name s :: rest ->
@@ -669,6 +671,9 @@ end = struct (* {{{ *)
669671
| Replace s :: rest ->
670672
if r.insertion <> None then duplicate_err "insertion";
671673
aux_attrs { r with insertion = Some (Replace s) } rest
674+
| Remove s :: rest ->
675+
if r.insertion <> None then duplicate_err "insertion";
676+
aux_attrs { r with insertion = Some (Remove s) } rest
672677
| If s :: rest ->
673678
if r.ifexpr <> None then duplicate_err "if";
674679
aux_attrs { r with ifexpr = Some s } rest
@@ -678,6 +683,7 @@ end = struct (* {{{ *)
678683
begin
679684
match attributes.insertion, attributes.id with
680685
| Some (Replace x), Some _ -> illegal_replace x
686+
| Some (Remove x), Some _ -> illegal_remove_id x
681687
| _ -> ()
682688
end;
683689
{ c with Clause.attributes }
@@ -694,7 +700,7 @@ end = struct (* {{{ *)
694700
| If s :: rest ->
695701
if r.cifexpr <> None then duplicate_err "if";
696702
aux_chr { r with cifexpr = Some s } rest
697-
| (Before _ | After _ | Replace _ | External | Index _) as a :: _ -> illegal_err a
703+
| (Before _ | After _ | Replace _ | Remove _ | External | Index _) as a :: _ -> illegal_err a
698704
in
699705
let cid = Loc.show loc in
700706
{ c with Chr.attributes = aux_chr { cid; cifexpr = None } attributes }
@@ -725,7 +731,7 @@ end = struct (* {{{ *)
725731
| Some (Structured.Index _) -> duplicate_err "index"
726732
| Some _ -> error ~loc "external predicates cannot be indexed"
727733
end
728-
| (Before _ | After _ | Replace _ | Name _ | If _) as a :: _ -> illegal_err a
734+
| (Before _ | After _ | Replace _ | Remove _ | Name _ | If _) as a :: _ -> illegal_err a
729735
in
730736
let attributes = aux_tatt None attributes in
731737
let attributes =
@@ -2225,7 +2231,7 @@ let rec constants_of acc = function
22252231
let w_symbol_table s f x =
22262232
let table = Symbols.compile_table @@ State.get Symbols.table s in
22272233
let pp_ctx = { table; uv_names = ref (IntMap.empty,0) } in
2228-
Util.set_spaghetti_printer Data.pp_const (R.Pp.pp_constant ~pp_ctx);
2234+
Util.set_spaghetti_printer pp_const (R.Pp.pp_constant ~pp_ctx);
22292235
f x
22302236

22312237
(* Compiler passes *)

src/discrimination_tree.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,22 @@ module Trie = struct
165165

166166
let is_empty x = x == empty
167167

168-
let rec remove p = function
168+
let rec replace p x = function
169169
| Node { data; other; listTailVariable; map } ->
170170
Node {
171-
data = data |> List.filter (fun y -> not(p y));
172-
other = other |> Option.map (remove p);
173-
listTailVariable = listTailVariable |> Option.map (remove p);
174-
map = map |> Ptmap.map (remove p);
171+
data = data |> List.map (fun y -> if p y then x else y);
172+
other = other |> Option.map (replace p x);
173+
listTailVariable = listTailVariable |> Option.map (replace p x);
174+
map = map |> Ptmap.map (replace p x);
175+
}
176+
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);
175184
}
176185

177186
let add (a : Path.t) v t =
@@ -391,7 +400,8 @@ let retrieve cmp_data path { t } =
391400
let r = call (retrieve ~pos:0 path t) in
392401
Bl.of_list @@ List.sort cmp_data r
393402

394-
let remove p i = { i with t = Trie.remove p i.t }
403+
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ val empty_dt : 'b list -> 'a t
5151
*)
5252
val retrieve : ('a -> 'a -> int) -> Path.t -> 'a t -> 'a Bl.scan
5353

54-
val remove : ('a -> bool) -> 'a t -> 'a t
54+
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 = Insert of insertion_place | Replace of string
322+
and insertion = Insert of insertion_place | Replace of string | Remove of string
322323
and insertion_place = Before of string | After of string
323324
and tattribute =
324325
| External

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 = Insert of insertion_place | Replace of string
217+
and insertion = Insert of insertion_place | Replace of string | Remove of string
217218
and insertion_place = Before of string | After of string
218219
and cattribute = {
219220
cid : string;

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)