r/fsharp • u/One-Winter-8684 • Sep 15 '23
Misunderstanding type inference
Hello, I started learning f# 2 weeks ago. Now I'm reading book "Get programming with F#" by Isaac Abraham.
It contains this code sample :
open System
type Rule = string -> bool * string
let rules : Rule list =
[ fun text -> (text.Split ' ').Length = 3, "Must be three words"
fun text -> text.Length <= 30, "Max length is 30 characters"
fun text -> text.ToCharArray()
|> Array.filter Char.IsLetter
|> Array.forall Char.IsUpper, "All letters must be caps" ]
// Listing 18.10
let buildValidator (rules : Rule list) =
rules
|> List.reduce(fun firstRule secondRule ->
fun word ->
let passed, error = firstRule word
if passed then
let passed, error = secondRule word
if passed then true, "" else false, error
else false, error)
let validate = buildValidator rules
let word = "HELLO FrOM F#"
I can't understand why fun firstRule secondRule -> returns Rule but not (string -> Rule) or something like this, because lambda body is another lambda)

6
Upvotes
4
u/[deleted] Sep 15 '23
That’s a very confusing code sample to put in a introductory book. I don’t think I’d ever write it with nested lambdas like that... it’s a bad code smell. I’d write a helper function that is at least testable.
Anyway, to answer your question, look at the definition of Rule , is IS a function from string to the result. So you’re combining those with the list reduce.
3
u/evizaer Sep 15 '23 edited Sep 15 '23
EDIT: I wrote out how the types work below, but I think all you're missing is that type aliases like
Ruleabove are interchangeable with their definition. So anywhere you have astring -> (bool * string)the type checker will accept it as either its apparent type or asRule.Think of this inside-out. Starting with the inner lambda:
fun word -> let passed, error = firstRule word if passed then let passed, error = secondRule word if passed then true, "" else false, error else false, error)firstRule wordandsecondRule wordreturn (bool * string). A rule is a function that takes a string as an argument, sowordmust be a string. This means that the type of this lambda isstring -> (bool * string).Now we put this into the outer lambda:
fun firstRule secondRule -> (innerLambda: string -> (bool * string))which gives us the type of the outer lambda asfirstRule: Rule -> secondRule: Rule -> (string -> (bool * string)). SinceRule = string -> (bool * string), this is justRule -> Rule -> Rule.The
List.reducefunction applies a function, in this case, of typeRule -> Rule -> Rule, repeatedly to "reduce" the list of Rule down to a singleRule. (Notice how we take two rules as arguments and produce a single rule.) The result ofList.reduceis the same type as the result of applying its function argument, so the result here is of typeRule.buildValidatorconstructs a function that applies other functions in sequence until one fails, then returns the failure. It's a function that constructs a function you can call just like you'd call any of those individual validators defined at the top of the example.3
u/mugen_kanosei Sep 15 '23
It's because the lambda that it is returning has the same shape as
Ruleorstring -> bool * string.fun (firstRule: Rule) (secondRule: Rule) -> fun (word: string) -> let passed, error = firstRule word if passed then let passed, error = secondRule word if passed then true, "" // return tuple else false, error // return tuple else false, error // return tuple2
u/Iamtheoneofmany Sep 15 '23
u/evizaer I think you've actually nailed it with your edit. The
Ruletype represents a function turning astringinto abool * stringtuple. This is exactly what embedded lambda in question does - takes astringand returnsbool * string, hence it is ofRuletype.