yanaijepeux/lib/StackAction.ml
Alain Emilia Anna Zscheile 16ea1b7d57
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
StackAction: +map
2024-02-24 13:46:34 +01:00

19 lines
549 B
OCaml

(** A stack action (pop is run first, then push sequentially) *)
type 'a t =
{ pop : int
; push : 'a list
}
let merge lhs rhs =
(* merge lhs.push and rhs.pop first *)
let rec compensate push pop = match (push, pop) with
| (_, 0) -> (push, pop)
| ([], _) -> (push, pop)
| (_::xs, _) -> compensate xs (pop - 1)
in let (push, pop) = compensate lhs.push rhs.pop in
{ pop = lhs.pop + pop; push = List.append push rhs.push; }
let empty = { pop = 0; push = []; }
let map f sta = { pop = sta.pop; push = List.map f sta.push; }