2

It seems like flip is doing unexpected things to my functions

Example 1:

let m = flip max
:t max
max :: Ord a => a -> a ->
:t m
m :: () -> () -> ()

Example 2:

let f x y = x + y
:t f
f :: Num a => a -> a -> a
let g = flip f
:t g
g :: Integer -> Integer -> Integer

f can evaluate floating point numbers, but g throws an error when it sees floats. But when I run

(flip f) 1.5 1.7

This evaluates fine! What is the difference between these expressions?

Tim
  • 361
  • 1
  • 5
  • 14

1 Answers1

12

The dreaded monomorphism restriction strikes again!

This is due to the monomorphism restriction, which causes polymorphic functions to be restricted to a monomorphic type in GHCi. Just run :set -XNoMonomorphismRestriction or give m an explicit type signature to get around this (hint: :set +m enables multiline mode in GHCi).


See these questions for explanations:

Community
  • 1
  • 1
bheklilr
  • 53,530
  • 6
  • 107
  • 163