Skip to content

Commit

Permalink
Merge PR coq#18262: Restore tailrecness of CList.filter
Browse files Browse the repository at this point in the history
Reviewed-by: ppedrot
Co-authored-by: ppedrot <[email protected]>
  • Loading branch information
coqbot-app[bot] and ppedrot authored Nov 13, 2023
2 parents fb6a0cf + 55505ed commit 9206669
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions clib/cList.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,31 @@ let assign l n e =

(** {6 Filtering} *)

let rec filter_loop f p = function
| [] -> ()
| x :: l' as l ->
let b = f x in
filter_loop f p l';
if b then if p.tail == l' then p.tail <- l else p.tail <- x :: p.tail
(* [filter_loop f (Some (c0,l0)) c l] will do c0.tail <- l0 if [for_all f l] *)
let rec filter_loop f reset p = function
| [] -> begin match reset with
| None -> ()
| Some (c,orig) -> c.tail <- orig
end
| x :: l as orig ->
if f x then
let c = { head = x; tail = [] } in
let () = p.tail <- cast c in
let reset = match reset with
| Some _ -> reset
| None -> Some (p,orig)
in
filter_loop f reset c l
else
filter_loop f None p l

let rec filter f = function
| [] -> []
| x :: l' as l ->
| x :: l' as orig ->
if f x then
let c = { head = x; tail = [] } in
filter_loop f c l';
if c.tail == l' then l else cast c
filter_loop f None c l';
if c.tail == l' then orig else cast c
else
filter f l'

Expand Down

0 comments on commit 9206669

Please sign in to comment.