Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Understanding the type signature of flip$id (Jeff Lasslett)
2. Re: Understanding the type signature of flip$id (Daniel Fischer)
3. Re: Understanding the type signature of flip$id (Jeff Lasslett)
4. Re: Understanding the type signature of flip$id
(Christian Maeder)
----------------------------------------------------------------------
Message: 1
Date: Wed, 2 Mar 2011 10:43:23 +1100
From: Jeff Lasslett <[email protected]>
Subject: [Haskell-beginners] Understanding the type signature of
flip$id
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Greetings,
I have been reading about how to use system.console.getOpt,
specifically "Interpreting flags as transformations of an options
record" which can be found at
http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-Console-GetOpt.html#4
Line 3 of
compilerOpts argv =
case getOpt Permute options argv of
(o,n,[] ) -> return (foldl (flip id) defaultOptions o, n)
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo
header options))
where header = "Usage: ic [OPTION...] files..."
uses (flip id) to resolve a list of functions down to a single record.
Looking at the type of flip$id I can see why it works in this context.
I just don't understand how passing id to flip results in the
following type signature:
Prelude> :t flip$id
flip$id :: b -> (b -> c) -> c
I do understand what flip has done to map here:-
Prelude> :t flip$map
flip$map :: [a] -> (a -> b) -> [b]
map take a function and a list and produces a new list. If map is
passed to flip the result is a function that takes a list, then a
function and results in a new list.
How do we go from flip having this signature:
Prelude> :t flip
flip :: (a -> b -> c) -> b -> a -> c
Prelude>
and id having
Prelude> :t id
id :: a -> a
Prelude>
to flip$id looking like flip$id :: b -> (b -> c) -> c ???
Thanks,
Jeff
------------------------------
Message: 2
Date: Wed, 2 Mar 2011 01:23:40 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Understanding the type signature of
flip$id
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Wednesday 02 March 2011 00:43:23, Jeff Lasslett wrote:
> I just don't understand how passing id to flip results in the
> following type signature:
>
> Prelude> :t flip$id
> flip$id :: b -> (b -> c) -> c
>
> I do understand what flip has done to map here:-
>
> Prelude> :t flip$map
> flip$map :: [a] -> (a -> b) -> [b]
>
> map take a function and a list and produces a new list. If map is
> passed to flip the result is a function that takes a list, then a
> function and results in a new list.
>
> How do we go from flip having this signature:
>
> Prelude> :t flip
> flip :: (a -> b -> c) -> b -> a -> c
> Prelude>
>
> and id having
>
> Prelude> :t id
> id :: a -> a
> Prelude>
>
> to flip$id looking like flip$id :: b -> (b -> c) -> c ???
>
> Thanks,
> Jeff
The point is that the type of id has to be unified with the type of flip's
(first) argument.
flip :: (a -> b -> c) -> (b -> a -> c)
id :: t -> t
So we have to unify (a -> b -> c) and (t -> t). Fully parenthesized,
a -> b -> c is a -> (b -> c). Now unification yields
t = a
-- id's arg must have the same type as the flip's argument's arg
and
t = (b -> c)
-- id's result must have the same result as flip's argument's result
>From that follows a = (b -> c) and *id can be passed to flip only at a more
restricted type than id's most general type, namely at the type
id :: (b -> c) -> (b -> c)*
So,
flip (id :: (b -> c) -> b -> c) :: b -> (b -> c) -> c
------------------------------
Message: 3
Date: Wed, 2 Mar 2011 16:33:17 +1100
From: Jeff Lasslett <[email protected]>
Subject: Re: [Haskell-beginners] Understanding the type signature of
flip$id
To: [email protected]
Cc: Daniel Fischer <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks Daniel,
I think I follow what you've written.
(b -> c) -> (b -> c) is the same as (b -> c) -> b > c
and when that is flipped: b -> (b- > c) -> c
Is that right?
Thanks,
Jeff
On 2 March 2011 11:23, Daniel Fischer <[email protected]> wrote:
> On Wednesday 02 March 2011 00:43:23, Jeff Lasslett wrote:
>> ?I just don't understand how passing id to flip results in the
>> following type signature:
>>
>> Prelude> :t flip$id
>> flip$id :: b -> (b -> c) -> c
>>
>> I do understand what flip has done to map here:-
>>
>> Prelude> :t flip$map
>> flip$map :: [a] -> (a -> b) -> [b]
>>
>> map take a function and a list and produces a new list. ?If map is
>> passed to flip the result is a function that takes a list, then a
>> function and results in a new list.
>>
>> How do we go from flip having this signature:
>>
>> Prelude> :t flip
>> flip :: (a -> b -> c) -> b -> a -> c
>> Prelude>
>>
>> and id having
>>
>> Prelude> :t id
>> id :: a -> a
>> Prelude>
>>
>> to flip$id looking like flip$id :: b -> (b -> c) -> c ? ???
>>
>> Thanks,
>> Jeff
>
> The point is that the type of id has to be unified with the type of flip's
> (first) argument.
>
> flip :: (a -> b -> c) -> (b -> a -> c)
> id :: t -> t
>
> So we have to unify (a -> b -> c) and (t -> t). Fully parenthesized,
> a -> b -> c is a -> (b -> c). Now unification yields
>
> t = a
> -- id's arg must have the same type as the flip's argument's arg
>
> and
>
> t = (b -> c)
> -- id's result must have the same result as flip's argument's result
>
> From that follows a = (b -> c) and *id can be passed to flip only at a more
> restricted type than id's most general type, namely at the type
> id :: (b -> c) -> (b -> c)*
>
> So,
>
> flip (id :: (b -> c) -> b -> c) :: b -> (b -> c) -> c
>
------------------------------
Message: 4
Date: Wed, 02 Mar 2011 08:51:43 +0100
From: Christian Maeder <[email protected]>
Subject: Re: [Haskell-beginners] Understanding the type signature of
flip$id
To: Jeff Lasslett <[email protected]>
Cc: [email protected], Daniel Fischer
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Maybe "flip ($)" is clearer? ($) is identity over function types.
If you have the (first) argument you could use a section like "($ a)"
and avoid flip.
C.
Am 02.03.2011 06:33, schrieb Jeff Lasslett:
> Thanks Daniel,
>
> I think I follow what you've written.
>
> (b -> c) -> (b -> c) is the same as (b -> c) -> b > c
>
> and when that is flipped: b -> (b- > c) -> c
>
> Is that right?
>
> Thanks,
> Jeff
>
>
>
> On 2 March 2011 11:23, Daniel Fischer <[email protected]>
> wrote:
>> On Wednesday 02 March 2011 00:43:23, Jeff Lasslett wrote:
>>> I just don't understand how passing id to flip results in the
>>> following type signature:
>>>
>>> Prelude> :t flip$id
>>> flip$id :: b -> (b -> c) -> c
>>>
>>> I do understand what flip has done to map here:-
>>>
>>> Prelude> :t flip$map
>>> flip$map :: [a] -> (a -> b) -> [b]
>>>
>>> map take a function and a list and produces a new list. If map is
>>> passed to flip the result is a function that takes a list, then a
>>> function and results in a new list.
>>>
>>> How do we go from flip having this signature:
>>>
>>> Prelude> :t flip
>>> flip :: (a -> b -> c) -> b -> a -> c
>>> Prelude>
>>>
>>> and id having
>>>
>>> Prelude> :t id
>>> id :: a -> a
>>> Prelude>
>>>
>>> to flip$id looking like flip$id :: b -> (b -> c) -> c ???
>>>
>>> Thanks,
>>> Jeff
>>
>> The point is that the type of id has to be unified with the type of flip's
>> (first) argument.
>>
>> flip :: (a -> b -> c) -> (b -> a -> c)
>> id :: t -> t
>>
>> So we have to unify (a -> b -> c) and (t -> t). Fully parenthesized,
>> a -> b -> c is a -> (b -> c). Now unification yields
>>
>> t = a
>> -- id's arg must have the same type as the flip's argument's arg
>>
>> and
>>
>> t = (b -> c)
>> -- id's result must have the same result as flip's argument's result
>>
>> From that follows a = (b -> c) and *id can be passed to flip only at a more
>> restricted type than id's most general type, namely at the type
>> id :: (b -> c) -> (b -> c)*
>>
>> So,
>>
>> flip (id :: (b -> c) -> b -> c) :: b -> (b -> c) -> c
>>
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 33, Issue 1
****************************************