I've managed to crack something that always annoyed me when I used to do network programming. However, Hugs and GHC behave differently. I'd be interested in views on this approach and also which implementation behaves correctly. Suppose I want to send an ICMP packet. The first byte is the type and the second byte is the code. Furthermore, the code depends on the type. Now you know at compile time that you can't use codes for one type with a different type. However, in Pascal (which is what I used to use) you only seemed to be able to carry out run time checks. Here's a way I came up with for checking at compile time in Haskell. module Main(main) where -- ICMP has many more values for the type byte but we only need two for the example. data Redirect = Redirect data TimeExceeded = TimeExceeded data ICMPType = MkRedirect Redirect | MkTimeExceeded TimeExceeded -- 5 and 11 are the values that get sent. instance Enum ICMPType where fromEnum (MkRedirect Redirect) = 5 fromEnum (MkTimeExceeded TimeExceeded) = 11 data ICMPCodeRedirect = RedirNet | RedirHost | RedirNetToS | RedirHostToS deriving Enum data ICMPCodeTimeExceeded = ExcTTL | ExcFragTime deriving Enum class Encode a b | a -> b where encode :: a -> b instance Encode ICMPType String where encode = show . fromEnum instance Encode Redirect (ICMPCodeRedirect -> String) where encode a y = encode (MkRedirect a) ++ (show (fromEnum y)) instance Encode TimeExceeded (ICMPCodeTimeExceeded -> String) where encode b z = encode (MkTimeExceeded b) ++ (show (fromEnum z)) Now I can say things like encode Redirect Redir and get the value "50". But if I say encode Redirect ExcTTL then I get a type error. GHC gives Couldn't match `ICMPCodeRedirect' against `ICMPCodeTimeExceeded' Expected type: ICMPCodeRedirect -> String Inferred type: ICMPCodeTimeExceeded -> t and Hugs gives ERROR: Constraints are not consistent with functional dependency *** Constraint : Encode Redirect (ICMPCodeTimeExceeded -> [Char]) *** And constraint : Encode Redirect (ICMPCodeRedirect -> String) *** For class : Encode a b *** Break dependency : a -> b This is just what you want as it picks up errors at compile time not at run time. However, if I now comment out the functional dependency class Encode a b {- | a -> b -} where encode :: a -> b and include the expressions x = encode TimeExceeded ExcTTL main = putStrLn x then Hugs complains ERROR "codes.hs" (line 37): Unresolved top-level overloading *** Binding : x *** Outstanding context : Encode TimeExceeded (ICMPCodeTimeExceeded -> b) whereas GHC doesn't complain. Which is right? Dominic.
"Dominic Steinitz" <dominic.j.steinitz@britishairways.com> wrote:
I've managed to crack something that always annoyed me when I used to do network programming. [. . .]
Suppose I want to send an ICMP packet. The first byte is the type and the second byte is the code. Furthermore, the code depends on the type. Now you know at compile time that you can't use codes for one type with a different type. However, in Pascal (which is what I used to use) you only seemed to be able to carry out run time checks.
I'm not sure I understand your problem. I don't see what's wrong with the following approach, which is Haskell 98. The type byte is coded as the type of the packet. Excuse the perhaps ideosyncratic style ... (in particular, I'm expecting people to use import qualified with this). module ICMP where data Type = Redirect RedirectData | TimeExceeded TimeData {- so you get an alternative for each of the packet types -} instance Enum Type where fromEnum (Redirect _) = 5 fromEnum (TimeExceeded _) = 11 {- we can't derive Enum for ICMP.Type, because it has non-nullary constructors. That just makes it a bit more tedious One could provide a class "code" with code:: t -> Int instead of fromEnum -} {- now we define individual record types for each of the different ICMP types -} data RedirectData = RedirectData {redirectCode:: RedirectCode, ip_addr:: Int, -- whatever redirectData:: [Int]} -- or whatever data RedirectCode = RedirNet | RedirHost | RedirNetToS | RedirHostToS deriving Enum data TimeData = TimeData {timeCode:: TimeExceededCode, timeData:: [Int]} -- or whatever data TimeExceededCode = ExcTTL | ExcFragTime deriving Enum {- Since Haskell 98 doesn't have MPTCs, if we want to encode packets as anything other than [Int] we'd have to define more classes. Encode serves as an example. -} class Encode t where encode:: t -> [Int] instance Encode Type where encode p@(Redirect d) = fromEnum p: encode d encode p@(TimeExceeded d) = fromEnum p: encode d instance Encode RedirectData where encode d = fromEnum (redirectCode d): ip_addr d: redirectData d instance Encode TimeData where encode d = fromEnum (timeCode d): 0: timeData d so one can go encode (Redirect (RedirectData RedirNet 0 [0])) and get [5,0,0,0], but encode (TimeExceeded (TimeData RedirNet 0 [0])) gives an error, as one would hope. What am I missing? Cheers, Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk 31 Chalmers Road jf@cl.cam.ac.uk Cambridge CB1 3SZ +44 1223 570179 (after 14:00 only, please!)
participants (2)
-
Dominic Steinitz -
Jon Fairbairn