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. Phantom types and export lists (Emmanuel Surleau)
----------------------------------------------------------------------
Message: 1
Date: Sat, 20 Oct 2012 09:41:29 +0200
From: Emmanuel Surleau <[email protected]>
Subject: [Haskell-beginners] Phantom types and export lists
To: [email protected]
Message-ID:
<CADd2AG6+=5nxv+o8cqmpfqxi9ubcac+xyhpq_hps3t3eh8l...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
In order to get a feel for Haskell, I decided to write a simple CRUD
application. I stumbled upon phantom types while looking for ways to
validate data in Haskell.
Here is what I have done so far:
{-# OPTIONS_GHC -XTemplateHaskell #-}
module Foo.Entities(buildTask, Sanitise, sanitise, Task,
Validated, Unvalidated,
name) where
import Data.Lens.Lazy ( (~=), access, (%=), mapLens, (^=), (^.),
(^%=), (^%%=), (^$) )
import Data.Lens.Template ( makeLenses )
data Validated
data Unvalidated
data Task a = Task { _name :: String }
makeLenses [''Task ]
buildTask :: String -> Task Unvalidated
buildTask name = Task { _name = name }
class Sanitise a where
sanitise :: Task a -> Either (Task Validated) String
instance Sanitise Validated where
sanitise t = Left t
instance Sanitise Unvalidated where
sanitise t
| t ^. name == "" = Right "Error: the name field should
be not be empty"
| otherwise = Left (Task { _name = t ^. name } )
Is this the recommended way of doing something like this? Do I really
need to export each lens I'm using? 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)? Also, how would I go
about generalizing my Sanitise typeclass, so that it would work on
other data types than Task?
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.
Thanks,
Emm
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 52, Issue 24
*****************************************