[Haskell-cafe] Re: New to Haskell
Cristian Baboi [EMAIL PROTECTED] writes: What I should have been told about upfront: - the syntax for an expression Since there are only declarations and expressions, the syntax of an expression involves pretty much all of the language, so it would be difficult to tell it upfront. - the syntax for a block Not sure what you mean by block. do a - [1..10] b - [3,4] return (a,b) is an expression... you can write that same expression as do {a - [1..10]; b - [3,4]; return (a,b)} too. - the adhoc syntax rules (how to distinguish among a tuple and a pharanthesized expression a tuple has commas in it. I'll grant that (x) not being a 1-tuple is a little ad-hoc, but there really is very little ad-hockery in Haskell (and a 1-tuple behaves very much like a plain value after all). and how to find the start and end of a block for example ) again, I don't know what you mean by block, but if you write the above expression with the braces ({}), it's obvious, I think, and the layout rule just inserts braces as necessary when the indentation changes. do a b c -- this is less indented, so will cause the end of the do. - the fact that lambda expressions are not the same thing as algebraic data values It might help to know why you think they might be the same; the syntax is different and the name is different... - what guarantees are made by the LANGUAGE that an IO action (such as do putStrLn Hello world ) is not performed twice As has been pointed out, «do putStrLn Hello world» is an expression that you can bind to a variable and use as many times as you like. Incidentally, it means the same as «putStrLn Hello World»; do connects a sequence of bindings and expressions, so you don't need it if there's nothing to be connected to. - the lambda expressions can be written (input) but cannot be printed (output) This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). The biggest problem for me, so far, is the last one. I can't see how your example illustrates that, I'm afraid. Here is some strange example: What I don't understand is why I'm forced to use guards like x==aa in cc, when aa is clearly bounded (is 7) and why in function h, the bounded u and v become free variables in the case expression. I would have liked the language design to have permitted case to pattern match against variables too, but the question is, what would the syntax be? There was a fair bit of discussion about this when the language was designed (and since), but no-one could come up with a good way of doing it. One aspect of it is this: we want f 0 = 42 f x = 3*x to work, and we want all function definitions to be translated into the core language in the same way, so you get f = \a - case a of 0 - 42 x - 3*x and given that, you can't have a variable on the LHS of - do anything other than get bound to the value of the expression in the case (a in the example). It's not just a the top level, either: f Nothing = 0 f (Just n) = n+1 just means f = \v - case v of Nothing - 0 Just n - n+1 so you can't have variables inside constructors do anything but get bound at that point. -- Jón Fairbairn [EMAIL PROTECTED] http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2007-05-07) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
On Tue, 18 Dec 2007 11:56:36 +0200, Jon Fairbairn [EMAIL PROTECTED] wrote: Cristian Baboi [EMAIL PROTECTED] writes: - the syntax for a block Not sure what you mean by block. do a - [1..10] b - [3,4] return (a,b) is an expression... you can write that same expression as do {a - [1..10]; b - [3,4]; return (a,b)} too. I mean anything that you can put between { }, and between ; - the adhoc syntax rules (how to distinguish among a tuple and a pharanthesized expression a tuple has commas in it. I'll grant that (x) not being a 1-tuple is a little ad-hoc, but there really is very little ad-hockery in Haskell (and a 1-tuple behaves very much like a plain value after all). Is this ([1 ,2 ,3 ,4]) a tuple or what ? It has commas in it! - the fact that lambda expressions are not the same thing as algebraic data values It might help to know why you think they might be the same; the syntax is different and the name is different... Ah, just a thought, nothing more. Lambda expressions are values, which is just data, after all. Even C can apply a function variable to an argument (function pointers). - what guarantees are made by the LANGUAGE that an IO action (such as do putStrLn Hello world ) is not performed twice As has been pointed out, «do putStrLn Hello world» is an expression that you can bind to a variable and use as many times as you like. Incidentally, it means the same as «putStrLn Hello World»; do connects a sequence of bindings and expressions, so you don't need it if there's nothing to be connected to. Yes, but that was not the question. What make you so sure it will be printed the exact number of times you intended ? - the lambda expressions can be written (input) but cannot be printed (output) This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). Even C can apply a function variable to an argument (function pointers). What make Haskell different beside the lazy evaluation and mutable variables things ? The biggest problem for me, so far, is the last one. I can't see how your example illustrates that, I'm afraid. In a very strange way. Nevermind. Here is some strange example: What I don't understand is why I'm forced to use guards like x==aa in cc, when aa is clearly bounded (is 7) and why in function h, the bounded u and v become free variables in the case expression. I would have liked the language design to have permitted case to pattern match against variables too, but the question is, what would the syntax be? There was a fair bit of discussion about this when the language was designed (and since), but no-one could come up with a good way of doing it. One aspect of it is this: we want f 0 = 42 f x = 3*x to work, and we want all function definitions to be translated into the core language in the same way, so you get f = \a - case a of 0 - 42 x - 3*x and given that, you can't have a variable on the LHS of - do anything other than get bound to the value of the expression in the case (a in the example). It's not just a the top level, either: f Nothing = 0 f (Just n) = n+1 just means f = \v - case v of Nothing - 0 Just n - n+1 so you can't have variables inside constructors do anything but get bound at that point. Thank you very much! Information from NOD32 This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
- the lambda expressions can be written (input) but cannot be printed (output) This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). Even C can apply a function variable to an argument (function pointers). Yes, and Haskell can do it also. But C, I guess, can't print out a source code for a function (well, there can be some weird dialects of C I'm not aware about). Haskell can't do it either. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
Cristian Baboi [EMAIL PROTECTED] writes: I mean anything that you can put between { }, and between ; Okay, there you have it then: the syntax for a block is a {, followed by elements separated by ;s and terminated by a }. Perhaps you are really asking about how the layout rule works? (Which has already been answered, btw.) Is this ([1 ,2 ,3 ,4]) a tuple or what ? It has commas in it! Good observation. Lists also have commas in them, and strings can, too. ,,, is not a tuple, either. A tuple would have a (, and subexpressions separated by commas, and terminated by ). The subexpressions would need to be maximal, and have no superexpression except the tuple. I must admit I don't understand why you find this difficult, I've had my share of problems grokking Haskell, but tuple syntax has always seemed quite natural. - the fact that lambda expressions are not the same thing as algebraic data values It might help to know why you think they might be the same; the syntax is different and the name is different... Ah, just a thought, nothing more. Lambda expressions are values, which is just data, after all. Yes. Even C can apply a function variable to an argument (function pointers). Would you say that functions and structs in C are the same thing because of this? This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). Even C can apply a function variable to an argument (function pointers). What make Haskell different beside the lazy evaluation and mutable variables things ? Referential transparency? But if you are happy about how C can print functions, perhaps you want to do: instance Show (a - b) where show x = A function Main show (+) A function -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
On Tue, 18 Dec 2007 12:49:52 +0200, Miguel Mitrofanov [EMAIL PROTECTED] wrote: - the lambda expressions can be written (input) but cannot be printed (output) This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). Even C can apply a function variable to an argument (function pointers). Yes, and Haskell can do it also. But C, I guess, can't print out a source code for a function (well, there can be some weird dialects of C I'm not aware about). Haskell can't do it either. Well, LISP can, if I remember it right. Information from NOD32 This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
Yes, and Haskell can do it also. But C, I guess, can't print out a source code for a function (well, there can be some weird dialects of C I'm not aware about). Haskell can't do it either. Well, LISP can, if I remember it right. Only in an interpreter, if I remember it right. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to Haskell
Miguel Mitrofanov [EMAIL PROTECTED] writes: Well, LISP can [print functions], if I remember it right. Only in an interpreter, if I remember it right. I think Emacs used to print #function or something for functions. It seems to keep around the reresentation now. Anyway, LISP has a bunch of different equalities (at least: =, eq, eql, equal), so there are clearly different trade offs. -k ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: New to Haskell
Cristian Baboi [EMAIL PROTECTED] writes: On Tue, 18 Dec 2007 11:56:36 +0200, Jon Fairbairn [EMAIL PROTECTED] wrote: Cristian Baboi [EMAIL PROTECTED] writes: - the syntax for a block Not sure what you mean by block. do a - [1..10] b - [3,4] return (a,b) is an expression... you can write that same expression as do {a - [1..10]; b - [3,4]; return (a,b)} too. I mean anything that you can put between { }, and between ; That's a bit like asking for the syntax of anything you can put between ( and ); The braces are used for grouping, and can group different things: case 2 of {1 - 2 ; 2 - 2} do {a - Just 1; return a} Is this ([1 ,2 ,3 ,4]) a tuple or what ? It has commas in it! Not in any meaningful sense... - what guarantees are made by the LANGUAGE that an IO action (such as do putStrLn Hello world ) is not performed twice As has been pointed out, «do putStrLn Hello world» is an expression that you can bind to a variable and use as many times as you like. Yes, but that was not the question. What make you so sure it will be printed the exact number of times you intended ? I don't understand your question at all, then. How many times it gets printed depends on how many times the programme is run, for one thing. Otherwise, it's a matter of the definition of the semantics of the language. Evaluation of a Haskell programme proceeds from evaluation of «main», which returns an object of type IO -- a sequence of Input/Output operatens -- that is run. IO doesn't happen when you evaluate an IO action, it happens when the IO action is run. For example, if you define f x = seq (putStrLn foo!) (x+1) and have main = print (f 2) the «putStrLn foo!» is evaluated because seq forces its first argument, but the only output you get is 3. This is a fundamental property of the language. A lambda expression is programme and at runtime the system doesn't know one lambda expression from another (all it can do with one is apply it to something). Even C can apply a function variable to an argument (function pointers). The secret of good language design is not what the language allows, it's what the language forbids. -- Jón Fairbairn [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: New to Haskell: The End
Henning Thielemann lemming at henning-thielemann.de writes: - it is lazy with class - it is strongly typed - it has automatic memory management - it has a standard library - it has a compiler - it is available on several platforms - it has a community - it is free There MUST be at least two adjectives added: it has a FAST compiler (compare to MzScheme for example) it is strongly and PARAMETRICALLY typed And perhaps it has MONADS I am learning Haskell 3 weeks now and have the common difficulties to understand them, but at the first sight this seems an extremely flexible and nevertheless clean solution to the problem. And it doesn't stop at monads, there are comonads and arrows too. And all this very actively and countiuously revised and developed further. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: New to Haskell: The End
Joost Behrends wrote: it has MONADS Interestingly, this is not even a language feature, it just happens that the concept of monads can be expressed in Haskell. (Ok, ignoring syntactic sugar in form of do-notation for the moment. And ignoring that constructor classes have been introduced because monads were such a cool use case). Regards, apfelmus ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to haskell: unresolved overloading question
Keean Schupke wrote: There are problems with this approach... instance (Ord a, Num a) = ApproxEq a where x ~= y = (abs (x-y) 1) However it should do what you want with just -foverlapping-instances -fundecidable-instances. Only -fallow-incoherent-instances allowes to resolve 3.5 ~= 2.6 without type-annotations! But when adding: instance ApproxEq Double where x~=y = False the above result remained True (via Defaulting) whereas 3.5 ~= (2.6 :: Double) became False. (An the other hand there was no need to add such an instance for Double.) It is also not possible to add a further instance like: instance (Ord a, Fractional a) = ApproxEq a where x ~= y = (abs (x-y) 0.1) (hoping that fractionals are treated more accurate that nums) Only one instance for ApproxEq a is allowed. Since the subject is New to haskell I recommend to stay Haskell98 compliant, i.e. avoid overlapping instances and add type signatures as necessary. If you add the option -Wall (or -fwarn-type-defaults) to ghci you'll get a warning whenever defaulting occurs (like in 1.5 == 1.5 to Double) (Putting default () in your source file prevents defaulting, but I don't recommend to do so only to force types on decls like f = 1.5) Christian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: New to haskell: unresolved overloading question
Tomasz Zielonka wrote: Why not forget about ApproxEq for () and Bool and simply define a function? (~=) :: (Ord a, Num a) = a - a - Bool x ~= y = abs (x-y) 1 Indeed, this works best. The instance for lists is also not too important. Christian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to haskell: unresolved overloading question
Am Dienstag, 22. Februar 2005 00:34 schrieb Daniel Fischer: snip That's a very common problem for newbies, so don't panic. In older versions of hugs (November 2002, e.g.), you would have got an unresolved overloading also from entering [] to the prompt (this is no longer so). If such things happen, add an explicit typing, if that doesn't help, then you have a problem. To deepen the confusion, hugs has no problems with Prelude 1.5 == 1.5 True I'm not quite sure why this works, must be due to type defaulting, however, Ah, I found out, carefully rereading the pertinent paragraph of the report (4.3.4) informed me that, for the defaulting rules to be applied, *all Classes in the context must be defined in the Prelude or the standard libraries*, which of course ApproxEq isn't. If you include default () or default (Integer) in your script, 1.5 == 2.4 will also lead to an unresolved overloading, so defaulting really works. Bye for now, Daniel ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: New to haskell: unresolved overloading question
There are problems with this approach... Instance heads are only chosen by the pattern not the constraints, so: instance (Ord a, Num a) = ApproxEq a where x ~= y = (abs (x-y) 1) Will match any type at all (whether a member of Ord or Num or not) and then will fail if the particular type is not an instance of Ord or Num. Using incoherent-instances is almost certainly broken as it just stops the compiler complaining is a generic instance (like above) overlaps with a specific instance in the wrong way. Using the flag just makes GHC silently choose the _most_generic_instance_. Almost alwaysApproxEq we want the opposite behaviour and want GHC to choose the most specific instance in the case of overlap. However it should do what you want with just -foverlapping-instances -fundecidable-instances. Keean. Christian Maeder wrote Blair Fraser wrote: I'm new to haskell and I'm working through the class section of the gentle tutorial. I decided to implement an ApproxEq class (just for fun), but have run into problems. The code below works fine for units and bools, but gives an unresolved overloading error for Floats and Doubles. What's going on here? What does the error message mean and what did I do wrong? (I'm in Hugs.) -- ApproxEq: My first class. class (Eq a) = ApproxEq a where (~=) :: a - a - Bool x ~= y = x == y -- By default, check equality -- These two override default with same, just checking if it works instance ApproxEq () where () ~= () = Trueinstance ApproxEq Bool where x ~= y = x == y -- These two override default with different -- consider floating points equal if they differ by one or less instance ApproxEq Float where x ~= y = (abs (x-y) = 1) instance ApproxEq Double where x ~= y = (abs (x-y) = 1) More elegant seems to be: instance (Ord a, Num a) = ApproxEq a where x ~= y = (abs (x-y) 1) However, this requires extensions to Allow unsafe overlapping instances: hugs -98 +O ghci -fglasgow-exts -fallow-overlapping-instances -fallow-undecidable-instances -fallow-incoherent-instances -- This one dosn't work either, but it just depends on the other two instance ApproxEq a = ApproxEq [a] where [] ~= [] = True (x:xs) ~= (y:ys) = x ~= y xs ~= ys _ ~= _ = False Thanks, Blair ___ 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] Re: New to haskell: unresolved overloading question
Am Montag, 21. Februar 2005 17:32 schrieb Christian Maeder: Blair Fraser wrote: I'm new to haskell and I'm working through the class section of the gentle tutorial. I decided to implement an ApproxEq class (just for fun), but have run into problems. The code below works fine for units and bools, but gives an unresolved overloading error for Floats and Doubles. What's going on here? What does the error message mean and what did I do wrong? (I'm in Hugs.) -- ApproxEq: My first class. class (Eq a) = ApproxEq a where (~=) :: a - a - Bool x ~= y = x == y -- By default, check equality -- These two override default with same, just checking if it works instance ApproxEq () where () ~= () = True instance ApproxEq Bool where x ~= y = x == y -- These two override default with different -- consider floating points equal if they differ by one or less instance ApproxEq Float where x ~= y = (abs (x-y) = 1) instance ApproxEq Double where x ~= y = (abs (x-y) = 1) More elegant seems to be: instance (Ord a, Num a) = ApproxEq a where x ~= y = (abs (x-y) 1) However, this requires extensions to Allow unsafe overlapping instances: hugs -98 +O ghci -fglasgow-exts -fallow-overlapping-instances -fallow-undecidable-instances -fallow-incoherent-instances -- This one dosn't work either, but it just depends on the other two instance ApproxEq a = ApproxEq [a] where [] ~= [] = True (x:xs) ~= (y:ys) = x ~= y xs ~= ys _ ~= _ = False Thanks, Blair For the original code, no extensions are necessary (and neither hugs nor ghc does complain). Probably something like ApproxEq 1.5 ~= 2.4 ERROR - Unresolved overloading *** Type : (Fractional a, ApproxEq a) = Bool *** Expression : 1.5 ~= 2.4 happened, that doesn't mean there is any problem with the code, only that the interpreter doesn't know what type 1.5 and 2.4 have. Adding a type to either resolves: ApproxEq 1.5 ~= (2.4::Double) True ApproxEq [1,2,3] ~= [1.6,2.8,3.] ERROR - Unresolved overloading *** Type : (Fractional a, ApproxEq a) = Bool *** Expression : [1,2,3] ~= [1.6,2.8,3.] ApproxEq [1,2,3] ~= ([1.6,2.8,3.]::[Float]) True That's a very common problem for newbies, so don't panic. In older versions of hugs (November 2002, e.g.), you would have got an unresolved overloading also from entering [] to the prompt (this is no longer so). If such things happen, add an explicit typing, if that doesn't help, then you have a problem. To deepen the confusion, hugs has no problems with Prelude 1.5 == 1.5 True I'm not quite sure why this works, must be due to type defaulting, however, including default (Double) doesn't help ApproxEq, so there must be more to it in the (==) case. BTW, if you add Christian's instance, for hugs, -98 +o (lower case o, allow overlapping instances) suffices, for ghci, -fallow-incoherent-instances is not necessary. Stay with Haskell, it's great (though not perfect)! Daniel ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe