
Are the following two functions equivalent? (i.e. do they describe the same computation) let add1 a = a + 2 let add2 = \ a -> a + 2 :t add1 add1 :: forall a. (Num a) => a -> a :t add2 add2 :: forall a. (Num a) => a -> a Does Haskell interpreter convert all functions in the form of add1 to the lambda form of add2? Does this lambda form represent the operational semantics of Haskell? How should I translate the type information into English? Thanks Pat This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

Well, yes add1 and add2 are equivalent. You can use either one or the other.
The type annotation can be read:
for all type 'a' such as 'a' is an instance of class Num, there exists a
function add1 that takes an 'a' and returns an 'a'.
2011/6/5 Patrick Browne
Are the following two functions equivalent? (i.e. do they describe the same computation)
let add1 a = a + 2 let add2 = \ a -> a + 2
:t add1 add1 :: forall a. (Num a) => a -> a
:t add2 add2 :: forall a. (Num a) => a -> a
Does Haskell interpreter convert all functions in the form of add1 to the lambda form of add2?
Does this lambda form represent the operational semantics of Haskell?
How should I translate the type information into English?
Thanks Pat
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, Jun 5, 2011 at 07:45, Patrick Browne
Are the following two functions equivalent? (i.e. do they describe the same computation)
let add1 a = a + 2 let add2 = \ a -> a + 2
Mostly. The monomorphism restriction can cause Haskell to restrict the type of the second to "Integer -> Integer". Otherwise, conceptually the first is turned into the second; this is the basis of partial application (think
let addN = \a -> \b -> a + b
which makes it clear that supplying "a" results in "\b -> supplied'a + b" being returned).

On 6/5/11 1:33 PM, Brandon Allbery wrote:
On Sun, Jun 5, 2011 at 07:45, Patrick Browne
wrote: Are the following two functions equivalent? (i.e. do they describe the same computation)
let add1 a = a + 2 let add2 = \ a -> a + 2
Mostly. The monomorphism restriction can cause Haskell to restrict the type of the second to "Integer -> Integer".
Also, in GHC 7 they may have different behavior with regards to inlining. That is, GHC 7 changed its rules to only inline when the arguments to the left of "=" have been saturated (I'm not sure how this interacts with the INLINE or INLINEABLE pragma). Whether this operational difference would count as "different" depends on how fine-grained you want your operational semantics to be. It wouldn't show up in the denotational semantics side of things. -- Live well, ~wren
participants (4)
-
Brandon Allbery
-
Patrick Browne
-
wren ng thornton
-
Yves Parès