You need some parentheses.
let rec sum m n f=
if m>n then 0
else if m=n then f n
else f m + sum n (m+1) f
(Although for readability, I would usually parenthesize the last line as else (f m) + (sum n (m+1) f)
. )
What's happening without the parentheses is that it's treating it as (f m) + (sum n m) + (1 f)
which is producing the error that sum n m
doesn't have type int, as it's a partial function application with a more complex type.
As a general rule, when an expression is being passed as an argument to a function, it always needs to be parenthesized. On a related note, if you ever actually wanted to pass the plus function as an argument, you would put it in parentheses (for example: sum m n (+)
(although that wouldn't type check in this case, since + expects two numbers)).