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: Class and Instance Question (Antoine Latter)
2. Re: Yesod question - ajax.lhs (Michael Snoyman)
3. Re: "pure" versus "impure" code (Sean Perry)
4. Re: Class and Instance Question (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Thu, 19 May 2011 23:08:30 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Class and Instance Question
To: "Adam C. Emerson" <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, May 19, 2011 at 8:05 PM, Adam C. Emerson <[email protected]> wrote:
> Good morning,
>
> As my first "real" toy project (something trivial but with some
> pretense of usefulness at least to me) in Haskell, I had been trying
> to write an XDR encoder/decoder based on top of Data.Binary. ?As such,
> I have a typeclass:
>
> class Encodable t where
> ? ?-- | Encode a value in the Put monad.
> ? ?put :: t -> Put
> ? ?-- | Decode a value in the Get monad
> ? ?get :: Get t
>
> And I have various instances, all of which seem to work, except for
> two. ?My thought was to treat a list of Chars as a string and a list
> of "encodable" types as a counted array. ?Thus, I tried:
>
> instance Encodable [Char] where
> ?put s = put $ runPut (putUTF8str s)
> ?get ? = do bs <- get
> ? ? ? ? ? ? return (runGet getUTF8str bs)
>
> and
>
> instance (Encodable e) => Encodable [e] where
> ?put l = if (length l) > xdrmaxlen
> ? ? ? ? ?then fail "Length of data exceeds XDR maximum for arrays."
> ? ? ? ? ?else (put (length l) >> putFixed (length l) l)
> ?get ? = do n <- get
> ? ? ? ? ? ? getFixed n
>
> This failed, and the compiler suggested I try adding:
>
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE OverlappingInstances #-}
>
> But even with these two statements, I get:
>
> *Data.XDR.Encodable> encode [1, 2, 3]
>
> <interactive>:1:0:
> ? ?Overlapping instances for Encodable [t]
> ? ? ?arising from a use of `encode' at <interactive>:1:0-15
> ? ?Matching instances:
> ? ? ?instance [overlap ok] (Encodable e) => Encodable [e]
> ? ? ? ?-- Defined at
> /home/azure/programming/xdr/src/Data/XDR/Encodable.hs:226:9-38
> ? ? ?instance [overlap ok] Encodable [Char]
> ? ? ? ?-- Defined at
> /home/azure/programming/xdr/src/Data/XDR/Encodable.hs:160:9-24
> ? ?(The choice depends on the instantiation of `t'
> ? ? To pick the first instance above, use -XIncoherentInstances
> ? ? when compiling the other instance declarations)
> ? ?In the expression: encode [1, 2, 3]
>
> So, the question I have, more than wondering how to get this to work
> (I suspect I shouldn't even be doing this, and instead I should
> newtype XDRInt and XDRString and so on), is why the two instances
> overlap. ?I have no instance for Char, so the first instance should
> apply to lists of Char (which isn't Encodable.) ?And the second should
> apply to lists of Encodable things (which Char isn't.)
>
A key point about instance resolution - when GHC tries to find an
instance to useit only looks at what is on the left-hand side of the
(=>) mark.
So an instance of the form:
> instance (Constraint a) => MyClass [a] where ...
can be read aloud as:
"[a] is an instance of MyClass. Also, it is an error if Constraint a
is not satisfied".
One way to get around this is to do what the 'Show' class does:
> class Show a where
> show :: a -> String
>
> showList :: [a] -> String
> showList [] = "[]"
> showList (x:xs) = "[" ++ ... ++ "]"
and then:
> instance Show a => Show [a] where
> show xs = showList xs
> instance Show Char where
> show x = ...
> showList xs = "\"" + ... + "\"
This way, the special handling that strings need is put in the Char instance.
The above instances are an approximation only, the Show class is a bit
more complex, but I just wanted to show the trick it uses for strings.
The other approach is to use the OverlappingInstances extension, which
I'm less able to explain as readily.
Antoine
> Is there a fairly comprehensible source I should read to understand
> typeclasses and instances better? ?(I've read my way through Real
> World Haskell, the Wikibook, and A Gentle Introduction, though it's
> possible they covered this and I just missed it.)
>
> Thank you.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Fri, 20 May 2011 07:59:13 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] Yesod question - ajax.lhs
To: Michael Litchard <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
My point is that it *is* working, exactly as intended. The compiler
warnings are deprecation messages, which mean that at some point in
the future the code may not work anymore. But by keeping it as-is, we
retain some backwards-compatibility with old code.
Michael
On Fri, May 20, 2011 at 6:47 AM, Michael Litchard <[email protected]> wrote:
> Michael,
> ? ? ? ? ? ?Your examples have a lot to do with why I picked Yesod in
> the first place. I've used a few of them to play around with and get a
> better grasp of your framework. I'm not knowledgeable enough at this
> point to extrapolate what's wrong with your code and then fix it.
> Could you provide a fixed version? I will then use it to play around
> with jQuery and see if I can't get a tabbed navbar going. The ultimate
> goal is to steal as much as I can from the haskellers design.
>
> Thanks :)
>
> Michael
>
> On Thu, May 19, 2011 at 8:38 PM, Michael Snoyman <[email protected]> wrote:
>> On Fri, May 20, 2011 at 3:38 AM, Michael Litchard <[email protected]>
>> wrote:
>>> I'm using yesod 0.8.1
>>>
>>> I'm playing around with yesod examples 0.8.0.1 and I noticed a few
>>> things that send up some red flags
>>>
>>> first thing, when I try to run ajax.lhs from the top level directory
>>>
>>>
>>>
>>>
>>> src/ajax.lhs:6:3:
>>> ? ?Warning: In the use of `string'
>>> ? ? ? ? ? ? (imported from Yesod, but defined in Text.Blaze.Internal):
>>> ? ? ? ? ? ? Deprecated: "Use Blaze.Html.toHtml"
>>
>> This is just using an older version of blaze-html. Replacing "string"
>> with "toHtml" should work just fine.
>>
>>> src/ajax.lhs:37:18:
>>> ? ?Warning: Deprecated syntax:
>>> ? ? ? ? ? ? ? quasiquotes no longer need a dollar sign: $parseRoutes
>>>
>>> src/ajax.lhs:52:21:
>>> ? ?Warning: Deprecated syntax:
>>> ? ? ? ? ? ? ? quasiquotes no longer need a dollar sign: $hamlet
>>>
>>> src/ajax.lhs:93:17:
>>> ? ?Warning: Deprecated syntax:
>>> ? ? ? ? ? ? ? quasiquotes no longer need a dollar sign: $hamlet
>>
>> This is intentional. GHC 7 changed the syntax for QQ. The examples
>> still mostly use the GHC 6.12 syntax for backwards compatibility.
>>
>>>
>>> This is suggesting to me the example code hasn't caught up to the
>>> changes in yesod, which can cause confusion for n00bs like me.
>>>
>>> I'm trying to learn how to use jQuery via yesod, and when I see the
>>> html generated from the current ajax.lhs this is what comes up
>>>
>>> <!DOCTYPE html><html><head><title>Page 1</title><link rel="stylesheet"
>>> href="/static/style%2Ecss?YaQxW21D"><script
>>> src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script
>>> src="/static/script%2Ejs?NWBIzSLG"></script></head><body><ul
>>> id="navbar"><li><a href="/page/page%2D1">Page 1</a></li><li><a
>>> href="/page/page%2D2">Page 2</a></li><li><a href="/page/page%2D3">Page
>>> 3</a></li></ul><div id="content"><h1>Page 1</h1><article>My first
>>> page</article></div></body></html>
>>>
>>> I want to draw attention to
>>> <script src="/static/script%2Ejs?NWBIzSLG"> and things like this. This
>>> is the other red flag that the example code hasn't caught up to the
>>> current state of Yesod. Am I interpreting the problem correctly?
>>
>> Yesod provides the query string as an optimization to allow you to set
>> cache headers. There's a good reason the code looks like that. (Though
>> I'm surprised the period is getting URL-encoded, looks like an issue
>> with http-types. And before anyone bugs the author of the package, it
>> looks like an issue with the code *I* wrote.)
>>
>>> Here is what the latest example of ajax.lhs looks like. Could someone
>>> tell me what needs to change to prevent the above problems from
>>> happening?
>>>
>>>> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell,
>>>> MultiParamTypeClasses, OverloadedStrings #-}
>>>> import Yesod
>>>> import Yesod.Helpers.Static
>>>> import Data.Monoid (mempty)
>>>
>>> Like the blog example, we'll define some data first.
>>>
>>>> data Page = Page
>>>> ? { pageName :: String
>>>> ? , pageSlug :: String
>>>> ? , pageContent :: String
>>>> ? }
>>>
>>>> loadPages :: IO [Page]
>>>> loadPages = return
>>>> ? [ Page "Page 1" "page-1" "My first page"
>>>> ? , Page "Page 2" "page-2" "My second page"
>>>> ? , Page "Page 3" "page-3" "My third page"
>>>> ? ]
>>>
>>>> data Ajax = Ajax
>>>> ? { ajaxPages :: [Page]
>>>> ? , ajaxStatic :: Static
>>>> ? }
>>>> type Handler = GHandler Ajax Ajax
>>>
>>>
>>>> staticFiles "static/yesod/ajax"
>>>
>>>> mkYesod "Ajax" [$parseRoutes|
>>>> / ? ? ? ? ? ? ? ? ?HomeR ? GET
>>>> /page/#String ? ? ?PageR ? GET
>>>> /static ? ? ? ? ? ?StaticR Static ajaxStatic
>>>> |]
>>>
>>>
>>>> instance Yesod Ajax where
>>>> ? approot _ = ""
>>>> ? defaultLayout widget = do
>>>> ? Ajax pages _ <- getYesod
>>>> ? content <- widgetToPageContent widget
>>>> ? hamletToRepHtml [$hamlet|
>>>> \<!DOCTYPE html>
>>>>
>>>> <html>
>>>> ? <head>
>>>> ? ? <title>#{pageTitle content}
>>>> ? ? <link rel="stylesheet" href="@{StaticR style_css}">
>>>> ? ? <script
>>>> src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
>>>> ? ? <script src="@{StaticR script_js}">
>>>> ? ? \^{pageHead content}
>>>> ? <body>
>>>> ? ? <ul id="navbar">
>>>> ? ? ? $forall page <- pages
>>>> ? ? ? ? <li>
>>>> ? ? ? ? ? <a href="@{PageR (pageSlug page)}">#{pageName page}
>>>> ? ? <div id="content">
>>>> ? ? ? \^{pageBody content}
>>>> |]
>>>
>>>
>>>> getHomeR :: Handler ()
>>>> getHomeR = do
>>>> ? Ajax pages _ <- getYesod
>>>> ? let first = head pages
>>>> ? redirect RedirectTemporary $ PageR $ pageSlug first
>>>
>>>> getPageR :: String -> Handler RepHtmlJson
>>>> getPageR slug = do
>>>> ? Ajax pages _ <- getYesod
>>>> ? case filter (\e -> pageSlug e == slug) pages of
>>>> ? ? ? [] -> notFound
>>>> ? ? ? page:_ -> defaultLayoutJson (do
>>>> ? ? ? ? ? setTitle $ string $ pageName page
>>>> ? ? ? ? ? addHamlet $ html page
>>>> ? ? ? ? ? ) (json page)
>>>> ?where
>>>> ? html page = [$hamlet|
>>>> <h1>#{pageName page}
>>>> <article>#{pageContent page}
>>>> |]
>>>> ? json page = jsonMap
>>>> ? ? ? [ ("name", jsonScalar $ pageName page)
>>>> ? ? ? , ("content", jsonScalar $ pageContent page)
>>>> ? ? ? ]
>>>
>>>> main :: IO ()
>>>> main = do
>>>> ? pages <- loadPages
>>>> ? let s = static "static/yesod/ajax"
>>>> ? warpDebug 3000 $ Ajax pages s
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>
------------------------------
Message: 3
Date: Thu, 19 May 2011 22:27:39 -0700
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] "pure" versus "impure" code
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On May 19, 2011, at 1:12 PM, Costello, Roger L. wrote:
>
> Also, it is my understanding that good software design is to isolate/separate
> the impure code from the pure code. Is that correct? Does that principle
> apply to all programming languages, or just Haskell?
>
(Since you asked about other languages the code below is pseudo Python, but
Haskell, C, whatever would still work)
The question is why you would want to make this separation. At first blush it
sounds arbitrary. In fact for writing code quickly in a "throw away" mode that
kind of separation is over kill in other languages. But consider code you might
want to keep for a few years.
Let's consider a typical hacking problem. You need to read from a file and do
something. The something is not all that important.
open file
for line in file:
# parse / edit / print / something
close file
Now let's say you have this chunk of code in the middle of some function calls.
Now to test this code you have to have access to a filesystem with the file
ready to go. What if you just checked out the code from revision control? Guess
you need to checkout the test data too. Bummer.
If we abstract out the concept of reading from a file we could provide a fake
file interface and still test our code. You can imagine how this would help
test networking code and other types of I/O.
In haskell this can be accomplished by hiding a Handle in your own type and for
testing providing a different Handle which is not bound to I/O. But you can
also separate the code which deals with the result of the input from the act of
doing the I/O.
open file
input = []
for line in file:
input.append(line)
close file
# do something with input over here
Now we can test all of the code which simply acts on the input data. We can
provide our own canned data, etc. Due to the IO Monad this is a bigger concern
for Haskell but it is good programming practice in any language.
------------------------------
Message: 4
Date: Fri, 20 May 2011 10:02:58 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Class and Instance Question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Friday 20 May 2011 06:08:30, Antoine Latter wrote:
>
> A key point about instance resolution - when GHC tries to find an
> instance to useit only looks at what is on the left-hand side of the
Antoine meant right-hand side here, of course.
> (=>) mark.
>
> So an instance of the form:
> > instance (Constraint a) => MyClass [a] where ...
>
> can be read aloud as:
>
> "[a] is an instance of MyClass. Also, it is an error if Constraint a
> is not satisfied".
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 41
*****************************************