Re: Overloading and Literal Numerics
Hi, I am trying to create an overloaded function à la Java to be able to call it either with a string or a number. Ex : definePort http definePort 80 but I have problem with restrictions in Haskell's type system Is there a better solution ? If we knew /why/ you wanted to do this we might be able to help. I can't see why you want to allow Strings, which have far too wide a range of values, as arguments to something that takes a port designator as an argument. data Port = Tcpmux | Nbp | Echo_ddp | Rje | Zip | Echo_tcp | ... deriving Enum, ... instance Num Port where ... would seem like a better way to me. Jón -- Jón Fairbairn [EMAIL PROTECTED] 31 Chalmers Road [EMAIL PROTECTED] Cambridge CB1 3SZ+44 1223 570179 (after 14:00 only, please!) ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
Re: Overloading and Literal Numerics
Jon Fairbairn [EMAIL PROTECTED] writes: data Port = Tcpmux | Nbp | Echo_ddp | Rje | Zip | Echo_tcp | ... deriving Enum, ... instance Num Port where ... Or, alternatively, just use Strings, and have a portFromString first check /etc/services for a match, then try to parse the string as a positive integer, and finally generate an error if no valid port can be determined? -kzm -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
Re: Overloading and Literal Numerics
Jon Fairbairn wrote : Hi, I am trying to create an overloaded function à la Java to be able to call it either with a string or a number. Ex : definePort http definePort 80 but I have problem with restrictions in Haskell's type system Is there a better solution ? If we knew /why/ you wanted to do this we might be able to help. I can't see why you want to allow Strings, which have far too wide a range of values, as arguments to something that takes a port designator as an argument. data Port = Tcpmux | Nbp | Echo_ddp | Rje | Zip | Echo_tcp | ... deriving Enum, ... instance Num Port where ... would seem like a better way to me. Jón I am trying to build a functional firewall generator. The first part describes the available protections (kernel, anti-address spoofing, etc.). The second desribes every protocol, and the necessary rules if the corresponding service is enabled (e.g. open the http port...). In the third one, the user will choose the services he wants to use/open and the static parameters (for instance the squid port number). I wanted the user part to be user-friendly, even if it is an Haskell program. So the commands definePort squidPort 3128 Seemed more logical than definePort squidPort 3128 The problem is that the numeric literal 3128 is considered as being a member of Num class, and not as beeing an Int. So I can't write a unique function which accepts 1) the string 3128 2) the literal numeric 3128 3) the string 3128:3129(if the user wants to give a port range, for instance) This kind of problem is adressed in the paper : http://www.cse.ogi.edu/~mbs/pub/overloading/ where the closed class extension seems to solve this kind of ambiguous type difficulty. But it does not seem to have been implemented yet Alain ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
Re: Overloading and Literal Numerics
Alain Cremieux wrote: I am trying to build a functional firewall generator. The first part describes the available protections (kernel, anti-address spoofing, etc.). The second desribes every protocol, and the necessary rules if the corresponding service is enabled (e.g. open the http port...). In the third one, the user will choose the services he wants to use/open and the static parameters (for instance the squid port number). I wanted the user part to be user-friendly, even if it is an Haskell program. So the commands definePort squidPort 3128 Seemed more logical than definePort squidPort 3128 The problem is that the numeric literal 3128 is considered as being a member of Num class, and not as beeing an Int. So I can't write a unique function which accepts 1) the string 3128 2) the literal numeric 3128 3) the string 3128:3129(if the user wants to give a port range, for instance) I understand the problem, but I still don't see why you want strings here. [Int] would do. They'd just have to type [3218..3130] for a range of port numbers, and you can define ordinary variables: type Port = [Int] http:: Port http = [80] You'd have to have them type definePort squidPort [3128] and that allows giving a range of ports where only one port is required, but at least they are going to be constrained to be numbers. With this, portRange [3128.3129] will give a compile time error, where portRange 3128.3129 would have to be a run-time error. Jón -- Jón Fairbairn [EMAIL PROTECTED] 31 Chalmers Road [EMAIL PROTECTED] Cambridge CB1 3SZ+44 1223 570179 (after 14:00 only, please!) ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
Re: Overloading and Literal Numerics
The problem is that you might have: instance Poly Double where ... and then when you say: po 5 it doesn't know whether this is an Int or a Double. writing po (5::Int) should be sufficient. -- Hal Daume III Computer science is no more about computers| [EMAIL PROTECTED] than astronomy is about telescopes. -Dijkstra | www.isi.edu/~hdaume On Wed, 26 Jun 2002, Alain Cremieux wrote: Hi, I am trying to create an overloaded function à la Java to be able to call it either with a string or a number. Ex : definePort http definePort 80 but I have problem with restrictions in Haskell's type system (or with my lack of experience with it). The program : data PolyType = MkPolyLeft String | MkPolyRight (String, String) | MkPolyNum Int deriving Show class Poly a where poly :: a - PolyType instance Poly String where poly s = MkPolyLeft s instance Poly (String, String) where poly p = MkPolyRight p instance Poly Int where poly i = MkPolyNum i po :: (Poly a) = a - PolyType po = poly tpo1, tpo2, tpo3 :: PolyType tpo1 = po 35 tpo2 = po (36, 37) tpo3 = po 39 gives the following result with ghc (5.03 -fglasgow-exts) : cl.hs:21: Ambiguous type variable(s) `a' in the constraint `Poly a' arising from use of `po' at cl.hs:21 In the definition of `tpo3': po 39 cl.hs:21: Ambiguous type variable(s) `a' in the constraint `Num a' arising from the literal `39' at cl.hs:21 In the first argument of `po', namely `39' In the definition of `tpo3': po 39 I think I need the closed extension of the 'class' clause to do that (but it does not seem to be implemented yet). Is there a better solution ? Thank you, Alain ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell