[Template-haskell] Re: Template Haskell - read function

Simon Peyton-Jones simonpj@microsoft.com
Thu, 3 Jul 2003 11:13:58 +0100


What about passing the type as the driving argument:

f :: [String] -> (Int, Bool)
f =3D $(mkReader [t| (Int,Bool) |])

mkReader :: Type -> ExpQ
mkReader ty =3D ...

| -----Original Message-----
| From: template-haskell-admin@haskell.org
[mailto:template-haskell-admin@haskell.org] On Behalf Of
| Thomas L. Bevan
| Sent: 02 July 2003 16:01
| To: template-haskell@haskell.org
| Cc: haskell@haskell.org
| Subject: [Template-haskell] Re: Template Haskell - read function
|=20
| -----BEGIN PGP SIGNED MESSAGE-----
| Hash: SHA1
|=20
| I have got a little further by defining the following functions and I
now have a better defined question.
|=20
| - --Takes a list and converts it to a n-ary tuple
| - --i.e. Generates functions like [String] -> (a,a,a)
| listToTuple i =3D lam [Pvar "xs"] (tup ([ infixE (Just $ var "xs") =
(var
"!!") (Just $ lit (Integer n)) | n <-
| [0..(i-1)] ]))
|=20
| - --Casts an n-ary tuple of type (String,String,..)
| - --i.e. Generates functions like (a,a,a) -> (Int,Bool,Int)
| cast i =3D lam [Ptup pats] (tup [ app (var "read") v | v <- vars])
|  where
|  pats =3D [ Pvar $ show n | n <- [1..i] ]
|  vars =3D [ var $ show n | n <- [1..i] ]
|=20
| So, I can now define a function f,
|=20
| f :: [String] -> (Int, Bool)
| f =3D $(cast 2) . $(listToTuple 2)
|=20
| but I can still see no way to infer the size of the tuple purely from
the declared type signature.
|=20
| Tom
|=20
|=20
|=20
| On Wed, 2 Jul 2003 11:56 am, Thomas L. Bevan wrote:
| > Hi,
| >
| > I'm struggling to write the following function in Template Haskell.
| >
| > I have a phantom type,
| >
| > >data HTMLPage a =3D HTMLPage FilePath
| >
| > This type points to an HTML file, where 'a' refers to the return
type of
| > the posted form.
| > So, mostly, I would expect types like
| >  HTMLPage (String, String, Date, Int)
| >
| > Where this represented a form the returned someone's first name,
last name,
| > dob and age.
| >
| > So, I want a function that will take the returned name/value pairs
and cast
| > them into the matching type.
| >
| > eg.
| >
| > >readForm :: Read a =3D> [(String,String)] -> HTMLPage a -> a
| >
| > Can anyone give me any pointers or am I better off just abandoning
this
| > approach?
| >
| > Tom
| >
| > _______________________________________________
| > Haskell mailing list
| > Haskell@haskell.org
| > http://www.haskell.org/mailman/listinfo/haskell
| -----BEGIN PGP SIGNATURE-----
| Version: GnuPG v1.2.2 (GNU/Linux)
|=20
| iD8DBQE/AvOlYha8TWXIQwoRAju7AKCxXxFQ1gMLTIUt93UpXyIQRVdqMQCg0Ryo
| FQK/bFrjQaV9fOyi6naeYEo=3D
| =3Dfuyq
| -----END PGP SIGNATURE-----
|=20
| _______________________________________________
| template-haskell mailing list
| template-haskell@haskell.org
| http://www.haskell.org/mailman/listinfo/template-haskell