Template Haskell - read function

Thomas L. Bevan thomas_bevan@toll.com.au
Wed, 2 Jul 2003 15:00:53 +0000


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have got a little further by defining the following functions and I now have a better defined question.

- --Takes a list and converts it to a n-ary tuple
- --i.e. Generates functions like [String] -> (a,a,a)
listToTuple i = lam [Pvar "xs"] (tup ([ infixE (Just $ var "xs") (var "!!") (Just $ lit (Integer n)) | n <- [0..(i-1)] ]))

- --Casts an n-ary tuple of type (String,String,..)
- --i.e. Generates functions like (a,a,a) -> (Int,Bool,Int)
cast i = lam [Ptup pats] (tup [ app (var "read") v | v <- vars])
 where
 pats = [ Pvar $ show n | n <- [1..i] ]
 vars = [ var $ show n | n <- [1..i] ]

So, I can now define a function f,

f :: [String] -> (Int, Bool)
f = $(cast 2) . $(listToTuple 2)

but I can still see no way to infer the size of the tuple purely from the declared type signature.

Tom



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 = 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 => [(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)

iD8DBQE/AvOlYha8TWXIQwoRAju7AKCxXxFQ1gMLTIUt93UpXyIQRVdqMQCg0Ryo
FQK/bFrjQaV9fOyi6naeYEo=
=fuyq
-----END PGP SIGNATURE-----