Commit f9d3b859 authored by POTTIER Francois's avatar POTTIER Francois
Browse files

Introduce [Misc.sum] and remove [Misc.tabulateb].

parent a89234c5
......@@ -413,8 +413,8 @@ let runpushes s =
difference, for instance, if production [prod] is never reduced, or
if the top stack cell is in fact nonexistent. *)
let (shiftreduce : Production.index -> bool), shiftreducecount =
Production.tabulateb (fun prod ->
let shiftreduce : Production.index -> bool =
Production.tabulate (fun prod ->
(* Check that this production pops at least one stack cell. *)
......@@ -430,6 +430,9 @@ let (shiftreduce : Production.index -> bool), shiftreducecount =
)
let shiftreducecount =
Production.sum (fun prod -> if shiftreduce prod then 1 else 0)
let () =
Error.logC 1 (fun f ->
Printf.fprintf f
......
......@@ -822,13 +822,13 @@ module Production = struct
let nt, rhs = table.(prod) in
Printf.sprintf "%s -> %s" (Nonterminal.print false nt) (Symbol.printao 0 rhs)
(* Tabulation. *)
(* Tabulation and sum. *)
let tabulate f =
Misc.tabulate n f
let tabulateb f =
Misc.tabulateb n f
let sum f =
Misc.sum n f
(* This array allows recording, for each %prec declaration, whether it is
ever useful. This allows us to emit a warning about useless %prec
......
......@@ -465,12 +465,13 @@ module Production : sig
val print: index -> string
(* Tabulation of a Boolean function over productions. [tabulateb f]
returns a tabulated version of [f] as well as the number of
productions where [f] is true. *)
(* Tabulation of a function over productions. *)
val tabulate: (index -> 'a) -> (index -> 'a)
val tabulateb: (index -> bool) -> (index -> bool) * int
(* Sum of an integer function over productions. *)
val sum: (index -> int) -> int
end
......
......@@ -39,12 +39,12 @@ let tabulate n f =
let a = Array.init n f in
Array.get a
let tabulateb n f =
let a = Array.init n f in
Array.get a,
Array.fold_left (fun count element ->
if element then count + 1 else count
) 0 a
let sum n (f : int -> int) : int =
let sum = ref 0 in
for x = 0 to n - 1 do
sum := !sum + f x
done;
!sum
(* [tabulatef number fold n dummy f] returns a function that is extensionally
equal to [f], but relies on an internal array. Arguments to [f] are of type
......
......@@ -36,14 +36,9 @@ val mapd: ('a -> 'b * 'b) -> 'a list -> 'b list
val tabulate: int -> (int -> 'a) -> (int -> 'a)
(* Tabulating a function using an internal array. [tabulateb n f]
returns a function that is extensionally equal to [f], but relies
on an internal array. Arguments to [f] are of type [int] and are
supposed to lie in the range [0..n). The result type of [f] is
assumed to be of type [bool]. [tabulateb] also returns the number
of points where [f] is [true]. *)
(* [sum n f] computes the sum [f 0 + f 1 + ... + f (n-1)]. *)
val tabulateb: int -> (int -> bool) -> (int -> bool) * int
val sum: int -> (int -> int) -> int
(* [tabulateo number fold n f] returns a function that is
extensionally equal to [f], but relies on an internal
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment