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. Re: Phantom types and export lists (Karl Voelker)
2. no instance for monad (Emmanuel Touzery)
3. Re: no instance for monad (Emmanuel Touzery)
4. Re: no instance for monad (Daniel Trstenjak)
5. Re: Phantom types and export lists (Emmanuel Surleau)
----------------------------------------------------------------------
Message: 1
Date: Sat, 20 Oct 2012 09:52:39 -0700
From: Karl Voelker <[email protected]>
Subject: Re: [Haskell-beginners] Phantom types and export lists
To: Emmanuel Surleau <[email protected]>
Cc: [email protected]
Message-ID:
<caffow0yk_hfho1dyzagde_jgowatng0xeuf7tz0ej3gz-bo...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Sat, Oct 20, 2012 at 12:41 AM, Emmanuel Surleau
<[email protected]> wrote:
> Is this the recommended way of doing something like this?
It looks to me like using a "smart constructor" would be sufficient in
this case. That is, you have a function:
makeTask :: String -> Either String Task
This function performs your validation. Then you make sure not to
export the data constructor for Task - that way, the only way to make
a Task outside of this module is to call the validating constructor
function.
> When I'm exporting "Task", I seem
> to be exporting the data type, but is there a way to export the Task
> constructor itself (not that I would want that)?
You can say Foo(..) to export all the data constructors of type Foo.
Or, you can list out the data constructors you want to export inside
the parens. If you want to be explicit about the fact that you are not
exporting any data constructors, you should probably write Foo().
Note that the same syntax applies to classes and their methods.
> Of course, it's quite possible that I'm reinventing the wheel and that
> I've missed a perfectly useable data validation module on hackage.
I don't know of any. But I'm also not convinced that such a thing is
necessary - smart constructors are usually all you need. Perhaps in a
context with more assumptions (i.e., the web), there would be some
useful work that a validation library could do.
-Karl
------------------------------
Message: 2
Date: Sat, 20 Oct 2012 20:24:29 +0200
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] no instance for monad
To: [email protected]
Message-ID:
<CAC42Ren4ycopNwaHCWegz1ZmoKeLx_B1=3gk2z-f2-a9xje...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
in my program i have a function which can be simplified like this:
writeProgramInfo :: Handle -> ProgramInfo -> IO ()
writeProgramInfo handle program = do
hPrintf handle "hello"
This is using the hPrintf from Text.Printf.
Unfortunately that hPrintf works on String parameters which is not good
enough because I need unicode character support (if I use unpack it works
on linux but complains about invalid characters on windows, I guess because
my locale on linux is utf-8 but on windows it's a local codepage). So I
found the Text.Format library:
http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html
Which appears to solve exactly that problem (out of the box Text support).
However I can't just install the library, add the import statements, and
replace hPrintf by TF.hprint and change the format string, that doesn't
work; if I change this function to:
import qualified Data.Text.Format as TF
writeProgramInfo :: Handle -> ProgramInfo -> IO ()
writeProgramInfo handle program = do
TF.hprint handle "hello"
I get this error message at build time:
Couldn't match expected type `IO ()'
with actual type `ps0 -> m0 ()'
In the return type of a call of `TF.hprint'
In the expression: TF.hprint handle "hello"
In the expression: do { TF.hprint handle "hello" }
Now this clearly results from the type of hprint, which is:
hprint ::
(MonadIO<http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Control-Monad-IO-Class.html#t:MonadIO>m,
Params<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format-Params.html#t:Params>ps)
=>
Handle<http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/GHC-IO-Handle.html#t:Handle>->
Format<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html#t:Format>->
ps -> m
()<http://hackage.haskell.org/packages/archive/ghc-prim/0.2.0.0/doc/html/GHC-Unit.html#t:-40--41->
while the type of hPrintf is:
hPrintf<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#v%3AhPrintf>::
HPrintfType<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#t%3AHPrintfType>r
=>
Handle<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-Handle.html#t%3AHandle>->
String<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Data-Char.html#t%3AString>->
r
with the precision: The return type is restricted to
(IO<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO.html#t%3AIO>a)
.
Well I didn't completely 'get' the monad thing yet. I think I'm pretty
close.. but not there yet. I'm pretty sure understanding this specific
example will take me one step closer... What am I missing here?
Thank you!
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121020/d2617405/attachment-0001.htm>
------------------------------
Message: 3
Date: Sat, 20 Oct 2012 20:26:44 +0200
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] no instance for monad
To: [email protected]
Message-ID:
<CAC42RemRFKPdA2hLzNZX3Kvg2Hg-Y9cj9VsGYPGBD=hyowd...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
and the subject of the mail is because if actually give parameters to the
print call I get a different error message, which is the first one I saw:
No instance for (MonadIO ((->) t0))
arising from a use of `TF.hprint'
Possible fix: add an instance declaration for (MonadIO ((->) t0))
but here I might be misusing the library in yet another way. I would first
focus on the "no parameters" case.
Emmanuel
On Sat, Oct 20, 2012 at 8:24 PM, Emmanuel Touzery <[email protected]>wrote:
> Hello,
>
> in my program i have a function which can be simplified like this:
>
> writeProgramInfo :: Handle -> ProgramInfo -> IO ()
> writeProgramInfo handle program = do
> hPrintf handle "hello"
>
> This is using the hPrintf from Text.Printf.
>
> Unfortunately that hPrintf works on String parameters which is not good
> enough because I need unicode character support (if I use unpack it works
> on linux but complains about invalid characters on windows, I guess because
> my locale on linux is utf-8 but on windows it's a local codepage). So I
> found the Text.Format library:
>
> http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html
>
> Which appears to solve exactly that problem (out of the box Text
> support). However I can't just install the library, add the import
> statements, and replace hPrintf by TF.hprint and change the format string,
> that doesn't work; if I change this function to:
>
> import qualified Data.Text.Format as TF
>
> writeProgramInfo :: Handle -> ProgramInfo -> IO ()
> writeProgramInfo handle program = do
> TF.hprint handle "hello"
>
> I get this error message at build time:
>
> Couldn't match expected type `IO ()'
> with actual type `ps0 -> m0 ()'
> In the return type of a call of `TF.hprint'
> In the expression: TF.hprint handle "hello"
> In the expression: do { TF.hprint handle "hello" }
>
> Now this clearly results from the type of hprint, which is:
> hprint ::
> (MonadIO<http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Control-Monad-IO-Class.html#t:MonadIO>m,
> Params<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format-Params.html#t:Params>ps)
> =>
> Handle<http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/GHC-IO-Handle.html#t:Handle>->
> Format<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html#t:Format>->
> ps -> m
> ()<http://hackage.haskell.org/packages/archive/ghc-prim/0.2.0.0/doc/html/GHC-Unit.html#t:-40--41->
>
> while the type of hPrintf is:
> hPrintf<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#v%3AhPrintf>::
> HPrintfType<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#t%3AHPrintfType>r
> =>
> Handle<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-Handle.html#t%3AHandle>->
> String<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Data-Char.html#t%3AString>->
> r
>
> with the precision: The return type is restricted to
> (IO<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO.html#t%3AIO>a)
> .
>
> Well I didn't completely 'get' the monad thing yet. I think I'm pretty
> close.. but not there yet. I'm pretty sure understanding this specific
> example will take me one step closer... What am I missing here?
>
> Thank you!
>
> Emmanuel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121020/f2bbaedc/attachment-0001.htm>
------------------------------
Message: 4
Date: Sat, 20 Oct 2012 21:46:21 +0200
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] no instance for monad
To: [email protected]
Message-ID: <20121020194621.GA22084@machine>
Content-Type: text/plain; charset=us-ascii
Hi Emmanuel,
> writeProgramInfo :: Handle -> ProgramInfo -> IO ()
> writeProgramInfo handle program = do
> TF.hprint handle "hello"
>
> I get this error message at build time:
>
> Couldn't match expected type `IO ()'
> with actual type `ps0 -> m0 ()'
> In the return type of a call of `TF.hprint'
> In the expression: TF.hprint handle "hello"
> In the expression: do { TF.hprint handle "hello" }
GHC expects an expression of 'IO ()' - the return type of writeProgramInfo -
but the expression 'TF.hprint handle "hello"' has the type 'ps0 -> m0 ()'
Look at the type of 'hprint':
hprint :: (MonadIO m, Params ps) => Handle -> Format -> ps -> m ()
Your call of 'hprint' is missing the last argument, the 'ps'. So
instead of 'm ()' you're returing the function 'ps -> m()', that's
what GHC tries to tell you.
You might be confused, because 'hprintf' uses some magic to allow
multiple parameters and can even be called without any parameter.
'hprint' uses are more explict - and imho a nicer and simpler approach -
for the parameters, but the parameter can't be completly omitted,
it has to be at least '()'.
So in your case: TF.hprint handle "hello" ()
Greetings,
Daniel
------------------------------
Message: 5
Date: Sat, 20 Oct 2012 21:52:29 +0200
From: Emmanuel Surleau <[email protected]>
Subject: Re: [Haskell-beginners] Phantom types and export lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Karl Voelker <[email protected]> wrote:
>On Sat, Oct 20, 2012 at 12:41 AM, Emmanuel Surleau
><[email protected]> wrote:
>> Is this the recommended way of doing something like this?
>
>It looks to me like using a "smart constructor" would be sufficient in
>this case. That is, you have a function:
>
>makeTask :: String -> Either String Task
>
>This function performs your validation. Then you make sure not to
>export the data constructor for Task - that way, the only way to make
>a Task outside of this module is to call the validating constructor
>function.
l see. However, with phantom types, you get more flexibility. For instance, you
can generate a blank Task as a template. I think I want to retain the ability
to work with non-well-formed data. But I'll keep smart constructors in mind.
>> When I'm exporting "Task", I seem
>> to be exporting the data type, but is there a way to export the Task
>> constructor itself (not that I would want that)?
>
>You can say Foo(..) to export all the data constructors of type Foo.
>Or, you can list out the data constructors you want to export inside
>the parens. If you want to be explicit about the fact that you are not
>exporting any data constructors, you should probably write Foo().
>
>Note that the same syntax applies to classes and their methods.
Thanks. This means I can export Sanitise (..) instead of Sanitise and sanitise.
>> Of course, it's quite possible that I'm reinventing the wheel and
>that
>> I've missed a perfectly useable data validation module on hackage.
>
>I don't know of any. But I'm also not convinced that such a thing is
>necessary - smart constructors are usually all you need. Perhaps in a
>context with more assumptions (i.e., the web), there would be some
>useful work that a validation library could do.
I guess you are right. Not many use cases for a completely generic validation
library.
Thanks,
Emm
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121020/ea750c97/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 52, Issue 25
*****************************************