
Is it possible to access type information and perform either of the following in a Haskell program? 1) Find the type/class of a variable e.g. a type predicate: is y of-type A, or is y of-Class A 2) Assert the type of a variable e.g. if y.length > 100 then y is of type big. Regards, Pat This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

On 22/07/2011 10:18, Patrick Browne should have wrote:
2) Assert the class of a variable e.g. if y.length > 100 then y is of class big.
Regards, Pat
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

You can do that using the Typeable class and possibly the Dynamic type.
(Data.Typeable and Data.Dynamic in base)
When you have:
foo :: (Typeable a, Num a) => a -> a
foo x = case (cast x :: Maybe Int) of
Just x' -> x + 12
_ -> x
foo will accept every instance of Num but will add 12 to it only if it is an
Int.
Dynamic wraps an instance of Typeable.
2011/7/22 Patrick Browne
Is it possible to access type information and perform either of the following in a Haskell program?
1) Find the type/class of a variable e.g. a type predicate: is y of-type A, or is y of-Class A
2) Assert the type of a variable e.g. if y.length > 100 then y is of type big.
Regards, Pat
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

But here we need to get the grammar from users. How can I do that? Or how can input the grammar in my code??
Sent from my iPhone
On Jul 22, 2011, at 5:04 AM, Yves Parès
You can do that using the Typeable class and possibly the Dynamic type. (Data.Typeable and Data.Dynamic in base)
When you have:
foo :: (Typeable a, Num a) => a -> a foo x = case (cast x :: Maybe Int) of Just x' -> x + 12 _ -> x
foo will accept every instance of Num but will add 12 to it only if it is an Int.
Dynamic wraps an instance of Typeable.
2011/7/22 Patrick Browne
Is it possible to access type information and perform either of the following in a Haskell program? 1) Find the type/class of a variable e.g. a type predicate: is y of-type A, or is y of-Class A
2) Assert the type of a variable e.g. if y.length > 100 then y is of type big.
Regards, Pat
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, Jul 22, 2011 at 10:31 AM, Stephen Tetley
As Haskell is statically typed, if y.length < 100, y is still of type Big...
You could achieve this kind of thing with smart constructors and GADTs. I don't know how useful it is, though =). data Sized t a where Small :: a -> Sized Small a Big :: a -> Sized Big a data Small -- empty data Big -- empty retrieve :: Sized t a -> a retrieve (Small a) = a retrieve (Big a) = a toSmall :: [a] -> Maybe (Sized Small [a]) toSmall xs = if length xs < 100 then Just (Small xs) else Nothing toBig :: ... -- almost the same as toSmall toUnknown :: [a] -> (forall t. Sized t [a] -> r) -> r toUnknown xs f = if length xs < 100 then f (Small xs) else f (Big xs) -- Felipe.

Thanks. Let me try this. I appreciate all the help. And how do I compute a LR parser graph using haskell?
Sent from my iPhone
On Jul 22, 2011, at 6:41 AM, Felipe Almeida Lessa
On Fri, Jul 22, 2011 at 10:31 AM, Stephen Tetley
wrote: As Haskell is statically typed, if y.length < 100, y is still of type Big...
You could achieve this kind of thing with smart constructors and GADTs. I don't know how useful it is, though =).
data Sized t a where Small :: a -> Sized Small a Big :: a -> Sized Big a
data Small -- empty data Big -- empty
retrieve :: Sized t a -> a retrieve (Small a) = a retrieve (Big a) = a
toSmall :: [a] -> Maybe (Sized Small [a]) toSmall xs = if length xs < 100 then Just (Small xs) else Nothing
toBig :: ... -- almost the same as toSmall
toUnknown :: [a] -> (forall t. Sized t [a] -> r) -> r toUnknown xs f = if length xs < 100 then f (Small xs) else f (Big xs)
-- Felipe.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Felipe Almeida Lessa
-
Patrick Browne
-
Sharadha Raghunathan
-
Stephen Tetley
-
Yves Parès