[Haskell-beginners] How to understand the type "ShowS"?
Lyndon Maydwell
maydwell at gmail.com
Wed Sep 25 06:58:40 CEST 2013
Although the links (difflist, for-a-few-monads-more) that you posted are
very interesting in their own right, you only need to understand the
following in order to comprehend "shows" and "ShowS"...
* Type-Classes
* Type Synonyms
* Partial Application
* Function Composition
Type Classes
==========
There are many references for understanding how these work, but the "shows"
function simply depends on its first parameter being able to be shown
through the use of the "Show" type-class.
Type Synonyms
============
Any type can have an "alias" created in the form of a type-synonym.
Although these can be parameterised, in the case of "ShowS" it is not:
`type ShowS = String -> String`
This means that wherever you see a reference to "ShowS" in a type, you may
replace it with "String -> String".
Partial application
=============
Look at a reference implementation of "shows" that Shrivats has described:
`shows x s = show x ++ s`
The type signature of "shows" focuses on the partially applied viewpoint,
because (thanks to the type-synonym) it is written as if it only takes one
argument:
`shows :: Show a => a -> ShowS`
However, with the "ShowS" synonym resolved, you can see that it actually
takes two:
`shows :: Show a => a -> String -> String`
Keep in mind that although Shrivats implementation is semantically
equivalent to the one in GHC.List, the performance characteristics may be
different.
Function Composition
================
Although this isn't strictly required in order to understand "shows", it
provides a potential motivation for why the function exists.
If you wished to chain together a bunch of String representations of
various objects, leaving the possibility of adding more later, you would
have to use many lambdas if you wished to constrain yourself to using
"show" and "++". For example:
`showWithMore a = \x -> show a ++ x`
Applying these would become tedious:
`myBools = \x -> showWithMore True (showWithMore False x)`
Thankfully, this can be avoided through the use of function-composition:
``myBools = shows True . shows False`
Hopefully this goes some way to getting you to the core of the construction
and motivation of these functions and synonyms.
As always, for a true understanding of the motivations behind such a
function, nothing beats looking at the source code [1].
[1] -
http://hackage.haskell.org/packages/archive/base/3.0.3.2/doc/html/src/GHC-Show.html#ShowS
On Wed, Sep 25, 2013 at 10:15 AM, yi lu <zhiwudazhanjiangshi at gmail.com>wrote:
> to @Kim-Ee
>
> I find it here. Thanks again.
> http://learnyouahaskell.com/for-a-few-monads-more
>
>
>
> On Tue, Sep 24, 2013 at 8:36 PM, yi lu <zhiwudazhanjiangshi at gmail.com>wrote:
>
>>
>>
>>
>> On Tue, Sep 24, 2013 at 7:50 PM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:
>>
>>>
>>> On Tue, Sep 24, 2013 at 6:43 PM, yi lu <zhiwudazhanjiangshi at gmail.com>wrote:
>>>
>>>> I have just looked at the API of Prelude, and I remember similar
>>>> definition for parallel haskell.
>>>
>>>
>>> How far have you gotten with LYAH? Or Hutton's textbook?
>>>
>>> I don't know this problem is revealed in LYAH, and I will check it now.
>> Thanks.
>>
>>
>>> What does a search on "haskell intro type system" reveal?
>>>
>>> -- Kim-Ee
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130925/aef01171/attachment.htm>
More information about the Beginners
mailing list