Re: [Haskell-cafe] Casting newtype to base type?
Hi Vlatko. On 2 July 2013 16:03, Vlatko Basic vlatko.ba...@gmail.com wrote: Is there a nicer way to extract the 'IO String' from 'IOS', without 'case' or without pattern matching the whole 'P'? You might enjoy the newtype package. http://hackage.haskell.org/package/newtype Hope this helps, Ozgur. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
Is there a nicer way to extract the 'IO String' from 'IOS', without 'case' or without pattern matching the whole 'P'? newtype IOS = IOS (IO String) data P = P { getA :: String, getB :: String, getC :: IOS } deriving (Show, Eq) getC_IO :: P - IO String getC_IO p = case getC p of IOS a - a getC_IO (P _ _ (IOS a)) = a Original Message Subject: Re: [Haskell-cafe] Casting newtype to base type? From: Malcolm Wallace malcolm.wall...@me.com To: vlatko.ba...@gmail.com Cc: Haskell-Cafe haskell-cafe@haskell.org Date: 01.07.2013 17:24 On 1 Jul 2013, at 16:07, Vlatko Basic wrote: I had a (simplified) record data P = P { a :: String, b :: String, c :: IO String } deriving (Show, Eq) but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String Not quite! That is a newtype'd String, not a newtype's (IO String). Try this: newtype IOS = IOS (IO String) but now when I try to set 'c' field in return $ p {c = readFile path} I get error Couldn't match expected type `IOS' with actual type `IO String' Use the newtype constructor to convert an IO String - IOS. return $ p {c = IOS $ readFile path} Regards, Malcolm ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
On Tue, Jul 02, 2013 at 03:03:08PM +0200, Vlatko Basic wrote: Is there a nicer way to extract the 'IO String' from 'IOS', without 'case' or without pattern matching the whole 'P'? newtype IOS = IOS (IO String) data P = P { getA :: String, getB :: String, getC :: IOS } deriving (Show, Eq) getC_IO :: P - IO String getC_IO p = case getC p of IOS a - a getC_IO (P _ _ (IOS a)) = a How about unIOS :: IOS - IO String unIOS (IOS a) = a getC_IO :: P - IO String getC_IO = unIOS . getC Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
Original Message Subject: Re: [Haskell-cafe] Casting newtype to base type? From: Tom Ellis tom-lists-haskell-cafe-2...@jaguarpaw.co.uk To: haskell-cafe@haskell.org Date: 02.07.2013 15:25 On Tue, Jul 02, 2013 at 03:03:08PM +0200, Vlatko Basic wrote: Is there a nicer way to extract the 'IO String' from 'IOS', without 'case' or without pattern matching the whole 'P'? newtype IOS = IOS (IO String) data P = P { getA :: String, getB :: String, getC :: IOS } deriving (Show, Eq) getC_IO :: P - IO String getC_IO p = case getC p of IOS a - a getC_IO (P _ _ (IOS a)) = a How about unIOS :: IOS - IO String unIOS (IOS a) = a getC_IO :: P - IO String getC_IO = unIOS . getC Thanks for your answer. I had those two funcs, but thought there might be a shorter/prettier one-func one-liner. :-) Tom ___ 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
Re: [Haskell-cafe] Casting newtype to base type?
You could always just put it into your newtype: newtype IOS = IOS { unIOS :: IO String } On Tue, Jul 2, 2013 at 9:31 AM, Vlatko Basic vlatko.ba...@gmail.com wrote: Original Message Subject: Re: [Haskell-cafe] Casting newtype to base type? From: Tom Ellis tom-lists-haskell-cafe-2...@jaguarpaw.co.uk To: haskell-cafe@haskell.org Date: 02.07.2013 15:25 On Tue, Jul 02, 2013 at 03:03:08PM +0200, Vlatko Basic wrote: Is there a nicer way to extract the 'IO String' from 'IOS', without 'case' or without pattern matching the whole 'P'? newtype IOS = IOS (IO String) data P = P { getA :: String, getB :: String, getC :: IOS } deriving (Show, Eq) getC_IO :: P - IO String getC_IO p = case getC p of IOS a - a getC_IO (P _ _ (IOS a)) = a How about unIOS :: IOS - IO String unIOS (IOS a) = a getC_IO :: P - IO String getC_IO = unIOS . getC Thanks for your answer. I had those two funcs, but thought there might be a shorter/prettier one-func one-liner. :-) Tom ___ 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
Am 7/1/2013 5:07 PM, schrieb Vlatko Basic: to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String What you really want is newtype IOS = IOS (IO String) I.e. a IOS value wraps an IO String. data P = P { a :: String, b :: String, c :: IOS } deriving (Show, Eq) but now when I try to set 'c' field in fun :: FilePath - P - IO P fun path p = do b - doesFileExist path ... return $ p {c = readFile path} I get error Couldn't match expected type `IOS' with actual type `IO String' which is correct. So, the question is: Is it possible to somehow cast 'IO String' to 'IOS' With the change to your 'newtype', you could use return $ p {c = IOS (readFile path)} instead. -- Frerich Raabe - ra...@froglogic.com www.froglogic.com - Multi-Platform GUI Testing ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
On Mon, Jul 01, 2013 at 05:07:00PM +0200, Vlatko Basic wrote: Hello Cafe! I had a (simplified) record data P = P { a :: String, b :: String, c :: IO String } deriving (Show, Eq) but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String instance Show (IOS) where show _ = (IO String) function instance Eq (IOS) where _ == _ = True An Eq instance for something containing IO is bound to lead to puzzlement somewhere down the line. I think you're better off defining something like data P_lesser = P_lesser { a_lesser :: String, b_lesser :: String } deriving (Show, Eq) to_lesser p = P_lesser (a p) (b p) and just factoring everything through to_lesser when you want to compare or show. Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
On 1 Jul 2013, at 16:07, Vlatko Basic wrote: I had a (simplified) record data P = P { a :: String, b :: String, c :: IO String } deriving (Show, Eq) but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String Not quite! That is a newtype'd String, not a newtype's (IO String). Try this: newtype IOS = IOS (IO String) but now when I try to set 'c' field in return $ p {c = readFile path} I get error Couldn't match expected type `IOS' with actual type `IO String' Use the newtype constructor to convert an IO String - IOS. return $ p {c = IOS $ readFile path} Regards, Malcolm ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
Hi Wallace, yes, indeed. Now I see I mixed newtype with type in declaration, and forgot the first one is a constructor. Original Message Subject: Re: [Haskell-cafe] Casting newtype to base type? From: Malcolm Wallace malcolm.wall...@me.com To: vlatko.ba...@gmail.com Cc: Haskell-Cafe haskell-cafe@haskell.org Date: 01.07.2013 17:24 On 1 Jul 2013, at 16:07, Vlatko Basic wrote: I had a (simplified) record data P = P { a :: String, b :: String, c :: IO String } deriving (Show, Eq) but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String Not quite! That is a newtype'd String, not a newtype's (IO String). Try this: newtype IOS = IOS (IO String) but now when I try to set 'c' field in return $ p {c = readFile path} I get error Couldn't match expected type `IOS' with actual type `IO String' Use the newtype constructor to convert an IO String - IOS. return $ p {c = IOS $ readFile path} Regards, Malcolm ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Casting newtype to base type?
I'm experimenting. The IO field is just a helper field, so it shouldn't have any consequences. Original Message Subject: Re: [Haskell-cafe] Casting newtype to base type? From: Tom Ellis tom-lists-haskell-cafe-2...@jaguarpaw.co.uk To: haskell-cafe@haskell.org Date: 01.07.2013 17:24 On Mon, Jul 01, 2013 at 05:07:00PM +0200, Vlatko Basic wrote: Hello Cafe! I had a (simplified) record data P = P { a :: String, b :: String, c :: IO String } deriving (Show, Eq) but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have created 'newtype IOS' and its 'Show' and 'Eq' instances newtype IOS = IO String instance Show (IOS) where show _ = (IO String) function instance Eq (IOS) where _ == _ = True An Eq instance for something containing IO is bound to lead to puzzlement somewhere down the line. I think you're better off defining something like data P_lesser = P_lesser { a_lesser :: String, b_lesser :: String } deriving (Show, Eq) to_lesser p = P_lesser (a p) (b p) and just factoring everything through to_lesser when you want to compare or show. Tom ___ 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