How to understand the type "ShowS"?

Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' It is a type of a function? I cannot understand this type, and don't know how to create functions of this type. And this function "shows" Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' I don't know how this function works. Yi

Looks like it's a convenience for building up a compositions of "Show"s. ShowS is indeed a synonym for a function. The type of shows alone isn't enough to figure out how it behaves exactly, but testing it out in GHCi is telling:
[Prelude] λ :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' [Prelude] λ :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' [Prelude] λ shows "asdf" "qwer" "\"asdf\"qwer"
On Tue, Sep 24, 2013 at 8:15 PM, yi lu
Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Sep 24, 2013 at 6:54 PM, Lyndon Maydwell
Looks like it's a convenience for building up a compositions of "Show"s.
ShowS is indeed a synonym for a function. The type of shows alone isn't enough to figure out how it behaves exactly, but testing it out in GHCi is telling:
[Prelude] λ :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' [Prelude] λ :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' [Prelude] λ shows "asdf" "qwer" "\"asdf\"qwer"
I don't know the meaning of this result.
On Tue, Sep 24, 2013 at 8:15 PM, yi lu
wrote: Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

What does `show "asdf"` give you in ghci? What Lyndon showed you was that
this function is equivalent to `shows x s = show x ++ s`.
Have fun,
Shrivats
On Sep 24, 2013 5:09 PM, "yi lu"
On Tue, Sep 24, 2013 at 6:54 PM, Lyndon Maydwell
wrote: Looks like it's a convenience for building up a compositions of "Show"s.
ShowS is indeed a synonym for a function. The type of shows alone isn't enough to figure out how it behaves exactly, but testing it out in GHCi is telling:
[Prelude] λ :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' [Prelude] λ :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' [Prelude] λ shows "asdf" "qwer" "\"asdf\"qwer"
I don't know the meaning of this result.
On Tue, Sep 24, 2013 at 8:15 PM, yi lu
wrote: Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Sep 24, 2013 at 7:47 PM, Shrivats
What does `show "asdf"` give you in ghci? What Lyndon showed you was that this function is equivalent to `shows x s = show x ++ s`.
Ah, it works a bit like "++" ?
Have fun,
Shrivats On Sep 24, 2013 5:09 PM, "yi lu"
wrote: On Tue, Sep 24, 2013 at 6:54 PM, Lyndon Maydwell
wrote: Looks like it's a convenience for building up a compositions of "Show"s.
ShowS is indeed a synonym for a function. The type of shows alone isn't enough to figure out how it behaves exactly, but testing it out in GHCi is telling:
[Prelude] λ :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' [Prelude] λ :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' [Prelude] λ shows "asdf" "qwer" "\"asdf\"qwer"
I don't know the meaning of this result.
On Tue, Sep 24, 2013 at 8:15 PM, yi lu
wrote: Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I have found the link of difference list. Maybe this helps.
http://www.haskell.org/haskellwiki/Difference_list
On Tue, Sep 24, 2013 at 7:54 PM, yi lu
On Tue, Sep 24, 2013 at 7:47 PM, Shrivats
wrote: What does `show "asdf"` give you in ghci? What Lyndon showed you was that this function is equivalent to `shows x s = show x ++ s`.
Ah, it works a bit like "++" ?
Have fun,
Shrivats On Sep 24, 2013 5:09 PM, "yi lu"
wrote: On Tue, Sep 24, 2013 at 6:54 PM, Lyndon Maydwell
wrote: Looks like it's a convenience for building up a compositions of "Show"s.
ShowS is indeed a synonym for a function. The type of shows alone isn't enough to figure out how it behaves exactly, but testing it out in GHCi is telling:
[Prelude] λ :i ShowS type ShowS = String -> String -- Defined in `GHC.Show' [Prelude] λ :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show' [Prelude] λ shows "asdf" "qwer" "\"asdf\"qwer"
I don't know the meaning of this result.
On Tue, Sep 24, 2013 at 8:15 PM, yi lu
wrote: Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Sep 24, 2013 at 5:15 PM, yi lu
Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
When you say "cannot" you probably mean "do not". Among the reasons are this lack of ability is highly transient. And this transience is highly dependent on the steps you take to change this. There are heaps of freely available intro material on this. What have you looked at? -- Kim-Ee

I have just looked at the API of Prelude, and I remember similar definition
for parallel haskell.
On Tue, Sep 24, 2013 at 7:17 PM, Kim-Ee Yeoh
On Tue, Sep 24, 2013 at 5:15 PM, yi lu
wrote: Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
When you say "cannot" you probably mean "do not". Among the reasons are this lack of ability is highly transient. And this transience is highly dependent on the steps you take to change this.
There are heaps of freely available intro material on this. What have you looked at?
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Sep 24, 2013 at 7:50 PM, Kim-Ee Yeoh
On Tue, Sep 24, 2013 at 6:43 PM, yi lu
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
On Tue, Sep 24, 2013 at 7:50 PM, Kim-Ee Yeoh
wrote: On Tue, Sep 24, 2013 at 6:43 PM, yi lu
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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-Sh...
On Wed, Sep 25, 2013 at 10:15 AM, yi lu
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
wrote: On Tue, Sep 24, 2013 at 7:50 PM, Kim-Ee Yeoh
wrote: On Tue, Sep 24, 2013 at 6:43 PM, yi lu
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

ShowS is just a type alias for String -> String. Anywhere where you could put a function of type String -> String you could replace that with ShowS. Examples blah :: String -> String blah "abc" = "def" is no different than blah :: Shows blah "abc" = "def"
From the GHC.Show import
GHC.Show.showList__ :: (a -> ShowS) -> [a] -> ShowS
is equivalent to
GHC.Show.showList__ :: (a -> (String -> String)) -> [a] -> (String ->
String)
It can be a little confusing but a lot of times you use the same function
prototype and it is useful to just turn it into its own little type to
shorten the types in your code.
On Tue, Sep 24, 2013 at 6:15 AM, yi lu
Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Yi
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Sep 24, 2013 at 3:45 PM, yi lu
Prelude> :i ShowS type ShowS = String -> String -- Defined in `GHC.Show'
It is a type of a function? I cannot understand this type, and don't know how to create functions of this type.
And this function "shows"
Prelude> :i shows shows :: Show a => a -> ShowS -- Defined in `GHC.Show'
I don't know how this function works.
Just attempting to give an imperative programmer's motivation for shows. Look at the picture at start of http://en.wikipedia.org/wiki/Linked_list which corresponds to the Haskell list a = [12,19,37] and 'a' would be a pointer pointing to the 12-node. Now if we wanted to add something -- say 8 -- before 12, its a simple operation: Make a new node containing 8, point it to the 12-node Assign a to this new node [Remember we are in imperative-land!!] This is a simple or constant-time operation However adding something to the end is harder: we have to start from a and walk down the list till the end and then mutate it to the new 8-node -- an O(n) operation where n is the length of the list. How can we have an O(1) add_to_end operation? Well in imperative land there are many approaches, one of which is that for every list like a, we also store an a_end pointing to the last element of a and then mutate that. This converts an O(n) operation into an O(1) operation at the cost of a mere extra pointer. shows is the same idea in FP-land! The haskell list a = [12,19,37] is as you surely know a = 12 : (19 : (37: [])) Replace the [] all the way in with an x and to make it valid lambda-bind it; ie a = \ x -> 12 : (19 : (37 : x)) a :: [Int] -> [Int] If the Int were Char that would be the Shows type And that lambda-exp basically constitutes your 'pointer-to-the-end' In particular with normal lists [12,19,37] ++ some_list needs 3 steps to walk down the first list However instead of [12,19,37] if we have the lambda-exp 'a' above we just need to do a some_list and we have append being done by a single beta reduction step! Voila!
participants (6)
-
David McBride
-
Kim-Ee Yeoh
-
Lyndon Maydwell
-
Rustom Mody
-
Shrivats
-
yi lu