two things I wanted from TH today
Greetings. FYI, I ran into two problems while trying to write something in TH today that I thought I'd mention. I'm not saying that these are vital features, but that this is perhaps a vote for these :) I wanted to write code to generate a function like this:
foo :: Dynamic -> String foo val | Just (a::String) <- fromDynamic val = a | Just (a::Int) <- fromDynamic val = show a | Just (a::Float) <- fromDynamic val = show a
But it doesn't look like Pattern Guards are available in THSyntax yet. I bravely started writing something to do this, but gave up, as did someone else on IRC, since what I really want is a lot simpler. So then I decided to settle for code like this:
foo'' :: Dynamic -> String foo'' val = case fromDynamic val of Just (a::String) -> a _ -> (case fromDynamic val of Just (a::Int) -> show a _ -> (case fromDynamic val of Just (a::Float) -> show a _ -> ""))
But then I found that type signatures on patterns also aren't available. In the end, I expect to be able to generate code like this:
foo' :: Dynamic -> String foo' val = case fromDynamic val of Just a -> (a::String) _ -> (case fromDynamic val of Just a -> show (a::Int) _ -> (case fromDynamic val of Just a -> show (a::Float) _ -> ""))
peace, isaac p.s. As an aside, pattern guards are cool! I've been avoiding almost all extensions in my work projects, and am temporarily lifting that self-imposed restriction. I've learned some neat stuff in the past week :)
On Tue, Apr 13, 2004 at 05:10:04PM -0400, Isaac Jones wrote:
FYI, I ran into two problems while trying to write something in TH today that I thought I'd mention. I'm not saying that these are vital features, but that this is perhaps a vote for these :)
I wanted to write code to generate a function like this:
foo :: Dynamic -> String foo val | Just (a::String) <- fromDynamic val = a | Just (a::Int) <- fromDynamic val = show a | Just (a::Float) <- fromDynamic val = show a
But it doesn't look like Pattern Guards are available in THSyntax yet.
Hmm, we currently have data Body = GuardedB [(Exp,Exp)] | NormalB Exp It looks like we would want either ----- 1 data Body = GuardedB [(Guard,Exp)] | NormalB Exp data Guard = NormalG Exp | PatG [Stmt] ----- or ----- 2 data Body = GuardedB [(Exp,Exp)] | PatGuardedB [(Guard,Exp)] | NormalB Exp data Guard = NormalG Exp | PatG [Stmt] ----- or ----- 3 data Body = GuardedB [(Exp,Exp)] | PatGuardedB [([Stmt],Exp)] | NormalB Exp ----- 1 is the simplest overall, but I think it would set a bad precedent as we wouldn't want future extensions to cause TH programs to break. 2 and 3 have the advantage that Haskell98+TH users can pretend nothing happened. I think I prefer 2 over 3 as it's simpler to see what happens when a GuardedB and PatGuardedB are concatenated. So I propose option 2. Does anyone disagree?
I bravely started writing something to do this, but gave up, as did someone else on IRC, since what I really want is a lot simpler.
So then I decided to settle for code like this:
foo'' :: Dynamic -> String foo'' val = case fromDynamic val of Just (a::String) -> a _ -> (case fromDynamic val of Just (a::Int) -> show a _ -> (case fromDynamic val of Just (a::Float) -> show a _ -> ""))
But then I found that type signatures on patterns also aren't available.
This one should be just be a matter of adding the alternative. Thanks Ian
participants (2)
-
Ian Lynagh -
Isaac Jones