Re: constants and functions without arguments
Tom Pledger wrote:
Andreas Leitner writes: : | Given a lazy pure functional language do we need to differntiate | (in syntax) between constants and functions without agruments? And | if we don't need to, does Haskell make a difference?
Haskell always treats a declaration of the form
foo = ...
as a pattern binding, not as a function binding. Sections 4.4.3 and 4.5 of the Haskell 98 report give more details.
So, in that sense, every function in Haskell takes an argument. There's nothing to prevent you from adding a dummy parameter to turn a constant (i.e. a simple pattern binding) into a function. The only reason I've heard for doing so, is to work around the monomorphism restriction.
Exactly thats' the reason for my question: Overcome ther monomorphism. I come from an Eiffel background and there we have the uniform access principle, which is a very important thing in that world. It means that you access an attribute just like a function with no parameters. This is actually a form of information hiding as you can change the underlying implementation (an attribute to a function and vice versa). The classic examlple for this is class POINT, which has an interface like that: -- POINT -- group 1 x: DOUBLE y: DOUBLE -- group 2 rho: DOUBLE abs: DOUBLE -- Now, you can choose to either implement group 1 as attributes, or group 2 and calculate the other one. With the uniform access principle, which says that accessing attributes and functions with no arguments happen in the same syntactic way, you can change the implementaion without notice. No user of this class needs to know. I am to new to FP to say whether this is equaly important in FP-languages, since the semantics are different (constants vs. attributes and immutable values vs. objects), but for now I am just currious wether it is possible theoretically. I mean couldn't one say that there are no constants, just functions with no arguments or the Void/Unit argument that return an expression. Since we have lazy evaluation, there won't be a problem at runtime, but would the type system allow such a thing? tia, Andreas
Andreas Leitner wrote at the end of his discussion about constants/functions sans arguments:
I mean couldn't one say that there are no constants, just functions with no arguments or the Void/Unit argument that return an expression. Since we have lazy evaluation, there won't be a problem at runtime, but would the type system allow such a thing?
Lennart Augustsson:
From a pedantic point of view your question makes no sense. The definition of a function is something that takes an argument and transforms it to a result. So a function always has exactly one argument. Period.
But from a practical point of view, yes you can regard constants as functions with no arguments. And it makes sense from a syntactic point of view: ...
There are different kinds of pedantry. In Clean there are constants-constants, and constants-functions, or rather unevaluated graphs, and an assignment x = expr may mean something different from x =: expr If expr produces a loooong lazy structure, sometimes treating it as an unevaluated thunk (or not reduced graph) is better than having the "final" result, although in a pure functional language there are no differences. This is another problem, most probably beyond what interests A. L., but as you see, people think about such things. Jerzy Karczmarczuk Caen, France
participants (2)
-
Andreas Leitner -
Jerzy Karczmarczuk