views:

822

answers:

10

With the renewed interest in functional programming languages, I've seen some similitudes between Smalltalk and FPL, namely closures ( BlockClosures in Smalltalk ) Yet, Smalltalk is not a FPL?

What would be needed to consider it as such?

+5  A: 

Smalltalk is a purely Object Oriented Language, where almost all code is basically objects interchanging messages. Functional programming is oriented to function calls, and compositions between functions to create more complex behavior (avoiding state for data, as opposed to internal states that objects have in any Object Oriented language).

Manuel
Well, "almost all" is anything other than expressions like "1" or "a := b". Also, composition of functions is exactly what I mean by "smalltalkers naturally write functionally in Smalltalk". Internal states (collections of instance variables) are identical to externally defined structs. Like a CONS cell, say. Or a list. Or a hashmap. (Important proviso: you've actually written the methods on your classes in a functional way, like the Point class example I give.)
Frank Shearar
>> Internal states (collections of instance variables) are identical to externally defined structs. << Except for the ways they are not, for example - their accessibility.
igouy
Er. Yes, you CAN write mutable state stuff in Smalltalk. You can in Common Lisp too, and that's commonly regarded as a functional programming language. So perhaps your definition of an FPL is "a language in which you can ONLY write functionally"? That rules out a whole bunch of languages that most people regard as functional: Common Lisp, Scheme, Erlang, ...
Frank Shearar
@Frank Shearar - I didn't mention mutable state so presumably your comment is directed to Manuel?
igouy
@igouy sorry, not my clearest of comments. "mutable state" was directed at Manuel, but "your definition of an FPL" was directed to you.
Frank Shearar
+3  A: 

Purely functional languages usually have immutable variables, making them more like the variables in a mathematical sense.

Matthew Schinckel
+13  A: 

There's no accepted definition of functional programming language.

If you define functional language as the language that supports first class functions, then yes, Smalltalk *is* a functional language.

If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, Smalltalk *is not* a functional language.


I'd encourage you to read the following related blog posts (and also the comments below them):

missingfaktor
Indeed, Smalltalk is an almost functional language.There are extensions to Smalltalk that provide functional pattern matching, e.g. http://map.squeak.org/package/3772b420-ba02-4fbd-ae30-8eadfc323b7b.Furthermore, Newspeak (http://newspeaklanguage.org/) is a programming language in the tradition of Self and Smalltalk that supports immutability as one of its core concepts.
Lukas Renggli
Doesn't that definition make C#, Javascript and pretty much every language from this century functional?
Diego Mijelshon
@Diego: As I said, there's no accepted definition of the term "functional programming language". So, by first definition, yes. By second definition, no.
missingfaktor
@Missing Faktor - Does Smalltalk even provide a way to declare named functions?
igouy
@igouy: I might be wrong but isn't `func` in this example a named function?
missingfaktor
@Missing Faktor - What example?
igouy
@iguoy: I had included a link in my comment. It seems StackOverflow ate it. Anyway here it is again: http://ideone.com/NoB1C
missingfaktor
@iguoy: A better example: http://ideone.com/4NNOv (Thanks to [@SergeStinckwich on Twitter](http://twitter.com/SergeStinckwich/status/21822876187)).
missingfaktor
@Missing Faktor - comment fields don't provide enough flexibility, so my answer is now edited into my reply to OscarRyz.
igouy
@Downvoter: Care to explain your downvote?
missingfaktor
+8  A: 

Smalltalk might not be a "pure functional language" (whatever that might be). However, natural use of Smalltalk often results in functional code. (What I guess the Scala people would call "object-functional".)

For example, Squeak's Point class has a functional API: methods like 1@1 translateBy: 1@1 return new Points with the new state, instead of mutating internal state. (This makes Point practically immutable, since the only way to change the internal state of the object is through reflection mechanisms like #instVarAt:.)

It's quite normal to use higher order functions in Smalltalk like maps, filters, folds, and the like. Given that these are baked into the Collections API, it's often easier to write code that uses Collections in a functional way than not.

So many people have so many definitions of "functional programming language" that the question "is Foo a FPL" is as useless a question to ask as "is Foo an object oriented language".

Having said that, here's my two cents: a functional programming language is a language in which it is natural and idiomatic to employ functional techniques: first-class functions, avoidance of mutable state, side-effect free functions, higher-order functions.

By that description, Smalltalk is a functional programming language. It calls first-order functions "methods" or "blocks" depending on whether or they're named or anonymous. Objects store state in instance variables. Higher order functions are simply methods that take blocks as parameters.

Having said that, yes, you can write Smalltalk in a non-functional manner. It does allow for mutable state. Does that stop Smalltalk from being called a functional language? If so, neither are these languages functional: Common Lisp, Erlang (shared state through the process dictionary, ets/dets).

So, in brief, Smalltalk is an FPL because:

  • Functions are first-class entities (objects, in Smalltalk - CompiledMethods or BlockClosures, to be precise).
  • Smalltalk supports closures (it calls them blocks).
  • Smalltalk allows one to write functionally in a natural, idiomatic manner.

and Smalltalk is not an FPL because:

  • You as programmer must ensure that your data structures (objects) are immutable (by having setters/mutators return copies of the object with the mutated state).
  • You as programmer must ensure that your methods (functions) are side-effect free.

(Some Smalltalks do apparently support immutable objects. My experience is limited to Squeak, which does not support VM-level immutability.)

Edit: I don't understand igouy's need for named function definitions other than through defining methods on an object. But anyway, here we go:

Smalltalk at: #func put: [:x | x + 1].
func value: 1.
Frank Shearar
>> Smalltalk might not be a "pure functional language" (whatever that might be) << Is Smalltalk even an impure functional language?
igouy
In Smalltalk the program is organized into objects but in a functional programming language the program is organized into functions. There's a well understood and fundamental difference in how we structure programs in OOP and FP. For an example, see "Synthesizing Object-Oriented and Functional Design to Promote Re-Use" http://www.cs.brown.edu/~sk/Publications/Papers/Published/kff-synth-fp-oo/
igouy
>> Squeak's Point class has a functional API << You say the behaviour is provided by accessing instances of Point class, not defined in a family of functions.
igouy
@igouy: "Organised into objects" is not an argument against Smalltalk not being a functional language. See Common Lisp (and CLOS in particular). You say "object" and I say "a set of functions that work on the same kind of struct, in their own namespace."
Frank Shearar
@iguoy: >> Squeak's Point class has a functional API << Did you read the class? Behaviour is provided by functional methods (i.e., no side effects, no mutable state). It's defined in a family of functions that we happen to call methods.
Frank Shearar
>> "Organised into objects" is not an argument against Smalltalk not being a functional language << Smalltalk is not organised into functions - that is an argument against Smalltalk being a functional language.
igouy
>> Smalltalk at: #func put: [:x | x + 1]. << Is that in fact how Smalltalk programs are written? Is that in fact how the Smalltalk libraries are written?
igouy
Did you read "Synthesizing Object-Oriented and Functional Design to Promote Re-Use" ?
igouy
Did you read the Point class? I wish I could find that paper I read recently that described Scheme as the first proper object-oriented language other than Simula-67. Yes, Scheme. Just like Erlang's a functional programming language and an OO language. (Just ask Joe Armstrong.)
Frank Shearar
Years ago I had a brief email discusson with Joe Armstrong about his article "Why OO Sucks" - trying to understand his point of view and clear up what seemed to me misunderstandings. "Why OO Sucks" is still cached by Google - http://webcache.googleusercontent.com/search?q=cache:M-JWFee4i6MJ:www.sics.se/~joe/bluetail/vol1/v1_oo.html+joe+armstrong+oo
igouy
>> Did you read the Point class? << What revelation do you expect me to experience on seeing methods that return values rather than change state?
igouy
Yes, and Armstrong changed his mind: "Erlang may be the ONLY object oriented language." - http://www.infoq.com/interviews/johnson-armstrong-oop (And we're straying off-topic now)
Frank Shearar
@igouy re Point class: that you have a bunch of named, side-effect functions. That you have a functional API for an object. That, in fact, OO and FP overlap. Really, I expect _you_ to know this.
Frank Shearar
@Frank Shearar - my guess is that the FORTRAN I used in 1975 allowed me to declare side effect free functions - Do you think that makes FORTRAN a functional programming language?
igouy
@igouy, you haven't defined what your idea of an FPL is. Mine is simple: if your language naturally leads you to write in an FP style, then it's an FPL. Not "oh you can fake it by passing void * around", but "oh, it's idiomatic."
Frank Shearar
@Frank Shearar - So please say whether being able to declare side effect free functions in FORTRAN is enough in itself, according to your simple idea, to make FORTRAN an FPL?
igouy
You're twisting my words. At no time have I claimed this. I said that IF FORTRAN supported ALL OF: first class functions, closures, support for immutable state, NATURAL AND IDIOMATIC USE OF FP, THEN I would call FORTRAN an FPL. I don't speak FORTRAN, so tell me: does FORTRAN support all these things? Does it make writing in FP easy?
Frank Shearar
@Frank Shearar >> You're twisting my words. At no time have I claimed this. << And I haven't said you did, I have asked a very very simple question - please say whether being able to declare side effect free functions in FORTRAN is enough in itself, according to your simple idea, to make FORTRAN an FPL?
igouy
No. Simply having side-effect free functions is _not_ sufficient to make an FPL. (Counter-example: C.) So what do you regard as the sufficient conditions for an FPL?
Frank Shearar
Given that - having side-effect free functions is not sufficient to make an FPL - if for sake of argument we ignore the fact that the code is object orientated and say that the methods on Squeak's Point class are functions, then would that be sufficient to make Smalltalk an FPL?
igouy
Clearly not - you can do this in C. What I don't understand is why you aren't attacking the "Smalltalk is an FPL" position from a strong angle - if you said "Hey, it doesn't support immutable data. You have to ensure immutability through convention, and Smalltalk doesn't even warn you that something has side effects, like in Common Lisp where you at least see nconc or in Scheme you see set! then I could see your point. What I fail to see is how organising functions/methods in a class, versus organising them in a module/package, makes any difference to anything.
Frank Shearar
@Frank Shearar >> Clearly not - you can do this in C. << So you understand that your previous repeated references to Squeak's Point class were irrelevant to the point of being bogus.
igouy
@Frank Shearar >> What I fail to see is how organising functions/methods... << I'm curious, did you look at "Synthesizing Object-Oriented and Functional Design to Promote Re-Use"?
igouy
@igouy Yes, it's an interesting, neat paper. And? It doesn't explain why you think that (a) a method is not a function and (b) how grouping these methods/functions in something called a class is somehow less FP than grouping them in a package or module. What the paper does illustrate is how Visitor is a hack in both the complimentary and pejorative senses of the word.
Frank Shearar
@igouy re Point: I was trying to address the _real_ issue about Smalltalk being/not being an FPL, which is management of state. So please, attack that, because that's actually a valid argument. Saying "Smalltalk's not an FPL because it lacks functions" is nonsense. I am starting to think Smalltalk's not an FPL because you, the programmer, have to supply the immutability, and that's error prone. _State_, not "methods aren't functions".
Frank Shearar
@Frank Shearar >> the immutability << 2 days ago you said - "Yes, you CAN write mutable state stuff in Smalltalk. You can in Common Lisp too, and that's commonly regarded as a functional programming language." And some purely functional languages provide for destructive transformation of state so perhaps the phrase you're looking for is referential transparency.
igouy
@Frank Shearar >> ... an interesting, neat paper ... It doesn't explain why you think that ... << Does it state a difference between what functional and object-oriented enable?
igouy
igouy
@igouy thanks for the reference. I'm busy working my way through it.
Frank Shearar
@Frank Shearar - Oh! you're reading it! :-) You might want to skip forward 20 years to "On Understanding Data Abstraction, Revisited" (pdf) http://www.cs.utexas.edu/users/wcook/Drafts/2009/essay.pdf
igouy
@igouy: :P Yes, and that paper's on my to-read list as well, when Steele referenced it recently (http://projectfortress.sun.com/Projects/Community/blog/ObjectOrientedTailRecursion). (This is when you say "well, if you had, we wouldn't be arguing"? :) )
Frank Shearar
@igouy I finally found that Scheme/OO paper I'd been trying to remember: http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf At any rate, I have a lot more reading to do before I can continue the discussion.
Frank Shearar
@igouy I don't see how Cook says that Smalltalk's not an FPL. I see that ADT != object, and he says that either may be written in a functional or imperative manner. Two independent axes! At this point, I don't think we're ever going to find common ground, so I'll bow out with the kernel of my claim: "However, natural use of Smalltalk often results in functional code." and leave it at that.
Frank Shearar
@Frank Shearar - To which the chorus responds, natural use of Smalltalk often does not result in "functional code".
igouy
+3  A: 

Programming with the Object Oriented paradigm is creating a program by identifying and modeling the Problem Domain Entities as objects, and then make them collaborate between themselves to solve each problem instance. Programming with the Functional paradigm is modeling the problem as a mathematical problem, and creating a mathematical function (by composing other functions) that for each problem instance, computes the problem solution.

In my opinion, a functional programming language is a language that given a solution for a problem solved using the functional programming paradigm, the language can express that solution exactly as it was thought. If you need to "transform" some part of your solution so it fits in what the language can express, then it doesn't fully support the paradigm you used to think the solution.

Smalltalk can express in most cases all the solutions created using the Object Oriented Programming paradigm, but it cannot expresss primitively a lot of solutions created using the Functional Programming paradigm. That's why I don't consider it a FPL. Despite not primitively be able to express every solution that a FPL can, Smalltalk is extremelly extensible, and you can extend it to be able to express all the solutions a FPL can.

Diego Geffner
Can you name some examples of FPL things that Smalltalk can't express "primitively"?
Frank Shearar
A: 

What would be needed to consider it as such?

  • a way to declare functions rather than a way to declare methods in a class

  • a way to structure a program around functions rather than around objects

@Missing Faktor - but isn't func in this example a named function?

|func v|
func := [:x | x + 1].
v := func value: 5.

No func is a local variable. You've bound an anonymous function to the local variable func.

|func v|
func := [:x | x + 1].
func := 2.
v := func value: 5.

To see the difference, look at this Erlang example which shows both an anonymous function and a named function.

igouy
igouy, Common Lisp's defun binds an anonymous function to a symbol in a symbol table. AFAIK, Scheme's define does exactly the same. So what's your point?
Frank Shearar
It can only be a matter of moments before you reach for Turing equivalence. My point it's silly to talk about a language being a functional programming language when it doesn't provide named functions. Just like it would be silly to talk about C being an OO programming language when it doesn't provide objects.
igouy
There ARE named functions in Smalltalk, only they're called methods. Please explain how a method is NOT a named function. Yes, a method accesses state. In Point's case, those are the unreachable, immutable, instance variables. Just like any FPL you care to mention has state, in the form of ADTs or whatever.
Frank Shearar
@Frank Shearar - There's a well understood and fundamental difference in how we structure programs in OOP and FP - they are duals.
igouy
Well, I call that duality/dualism a fundamental _sameness_. Maybe that's why we're arguing.
Frank Shearar
@Frank Shearar >> Maybe that's why we're arguing << Mostly you seem to be quarrelling by focusing on the similarities and ignoring the blatant differences.
igouy
As you no doubt know from hands-on experience, working in Smalltalk is as much about figuring out commonality of actions (verbs, functions, methods) as it is about pulling out structure (defining structs, tuples, classes). FP emphasises verbs over nouns where OO emphasises nouns over verbs. I see no reason why a language cannot support both paradigms, and I assert that Smalltalkers do indeed practice both OO and FP when writing. Your definition of an FPL above includes C, which I do not regard as either an OOPL or an FPL.
Frank Shearar
An hour before this you wrote - "@igouy, you haven't defined what your idea of an FPL is..." - but now you say - "Your definition of an FPL above..."
igouy
+4  A: 

A language does not become a functional language by having named functions - by that definition, C would be functional ! More important is the functional semantics in the mathematical sense, that the result depends on the arguments alone (in particular: no side effects). By this definition, objects which can be modified by setters are contrary to a functional programming style. However, as already pointed out, even objects can be used in a functional style (Rectangle example) if you forbid side effects. And, btw. there is a duality between objects with methods and a set of functions which are defined in a closure over a private state (see SICP for details). They can simulate each other mutually:

(def (make_foo initVal1 ... initValN)
   (let ((instVar1 initVal1) ... (instVarN initValN))
      (define (method1 args) ...)
      ...
      (define (methodN args) ...)
      (define (lookup selector)
          (cond ((eq? selector 'method1) method1)
             ...
             ((eq? selector 'methodN) methodN)
             (true doesNotUnderstandCode)))
      lookup)) ; returns the lookup function (provides "access into the closure") !

(defMacro (method1 receiver.args)
    ((receiver selector) args))

(define foo (make_foo 1 2 ...))
(method1 foo ...)

the above is a simulation of "pure" functional objects, and semantically the same as a lot of Smalltalk code! However, to implement a setter-method, you have to add a method which does a (set! instVar newValue). And, because set! is non-functional, breaks the "functionality" of the scheme.

Summary: look at the semantics, not the source, luke !

blabla999
+1 for "object = struct + closures" isomorphism!
Frank Shearar
You can have setters, as long as you have your setters return a new object with the required state rather than mutating the receiver.
Frank Shearar
>> there is a duality between objects with methods and a set of functions << Yes, they are duals - they are not the same thing.
igouy
>> A language does not become a functional language by having named functions << Agreed, but when a language does not have named functions why would we think that language was trying to be a functional programming language?
igouy
Smalltalk does have named functions. They're called methods. They're lambdas bound to a symbol in a dictionary local to a class. You haven't explained how that's any difference to lambdas bound to symbols in, say, a package.
Frank Shearar
@Frank Shearar >> You haven't explained how that's any difference to... << What have I repeatedly said is different? http://www.cs.brown.edu/~sk/Publications/Papers/Published/kff-synth-fp-oo/
igouy
cool down: lets try to define some common base we all agree upon:1) functional style is using side-effect free functions;2) a functional language is one where the functional style is the most natural or preferred way of doing things. This makes lisp a functional language, although it supports non functional operations (set!, for example) and objects (clos or similar). This also makes Smalltalk a non-functional language, although it supports all that functional stuff as well.3) you can program oo-style in lisp and also functional style in smalltalk
blabla999
+1  A: 

I've seen some similitudes between Smalltalk and FPL, namely closures

There's a more basic similarity - every Smalltalk message always returns a value.

But now look at the Design Principles Behind Smalltalk

[Smalltalk] sends the name of the desired operation, along with any arguments, as a message to the number, with the understanding that the receiver knows best how to carry out the desired operation.

Does that describe what you think of as Functional Programming?

@Frank Shearar - That describes Erlang too. Is Erlang now non-functional?

Erlang is something of a chimera - sequential programming is based on functions, but concurrent programming is based on message passing between processes. So Erlang is not multi paradigm or hybrid in the way that other languages allow different styles to be used to achieve the same purpose - it uses different styles for different purposes.

igouy
That describes Erlang too. Is Erlang now non-functional?
Frank Shearar
My previous comment reads a bit on the snarky side. I don't mean to be (too) snarky. The above also describes late binding, which I don't see as being incompatible with FP.
Frank Shearar
@Frank Shearar >> Is Erlang now non-functional? << Haven't you already quoted Joe Armstrong saying "I might think, though I'm not quite sure if I believe this or not, but Erlang might be the only object oriented language"?
igouy
@igouy: so it's your position that a language can EITHER be an FPL OR an OOPL, but never both?
Frank Shearar
@Frank Shearar - http://en.wikipedia.org/wiki/Mule
igouy
@jgouy I miss your point. I'm stubborn as a mule, or do you mean the MUltiLingual Extension to Emacs might teach me something?
Frank Shearar
@Frank Shearar - I'll humour you - A hybrid between a Horse and a Donkey isn't a Horse and it isn't a Donkey, it's a Mule.
igouy
@igouy Thanks for clarifying. And I'm glad to see you already answered my next question, re Erlang :) I think in Erlang's case there's a neat split between OO and FP based on scale: within a process, you have a purely functional paradigm, while the communication between processes is probably best thought of in the Kay-style object oriented paradigm. (Kay's often said that people missed the point of Smalltalk - that the gem was message passing, not objects: http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html)
Frank Shearar
@Frank Shearar - And a creature with a lion's head, a goat's body and a serpent's tail - isn't a lion, and isn't a goat and isn't a serpent - it's a chimera.
igouy
+1  A: 

Thank you all for all the answers.

I'll add mine here which basically is my ( probably miss) understanding of yours.

I think the criteria to call something OO or FP is they way "stuff" is built using the language; not how easy or hard is to do it, I mean, the mental paradigm used.

For instance, as the links shown, it may be harder to type something in Scala than in OCaml, but that doesn't make it less FPL ( maybe incomplete or impure, but definitely functional ); because the aim of Scala is to use functions as primary building blocks, rather than objects.

On the other hand, making easy to write a function in a language doesn't make it functional, if the style or the aim is to use another artifacts. Smalltalk for instance, make it very easy to write an anonymous block or to provide a pluggable sorting comparator, but that doesn't make it ANY Functional at all, because the focus is to use objects and pass messages. Blockclosures are just a mechanism to encode these messages ( not functions ) even if they look like just like they were functions.

The confusion comes, because OO and FP are orthogonal ( as Rahul stated via twitter ) , so, what it looks like a encoded message in Smalltalk, looks pretty much like an anonymous function in Scala. But doing something similar doesn't converts a language of one paradigm into another.

To make it worst, Scala also uses the OO paradigm, to make it easier for mainstream ( Java, C++, C# ) programmers to give the jump, and if you see, it have had way more success than any of the others FPL's did. And of course this was thank to Java ( IMHO if Clojure has any success will be for exactly the same reason, the JVM )

In conclusion: A programming language may be classified by the paradigm it uses to build "things". There are languages that are pure like [Smalltalk : OO, Haskell: Functional, C: Procedural], or hybrids, like Scala or multi paradigm , like C++, Ruby, Python.

The fact you can write one style with the other language, doesn't make it that language of that style. Like simulating Objects with Lisp doesn't make it OO nor using functions with Java make it Functional.

To answer the question, to be considered functional, Smalltalk would need to use functions as building blocks, which it doesn't because it uses objects.

OscarRyz
Your last line pretty much says it. Playing with a language designed to be multi paradigm (I don't think that would be Ruby and maybe not Python and C++ is too big) gives a good feel for what it means for a language to have a dominant style. I found it very interesting playing with Mozart/Oz because reworking a program from one style to another is very interactive - you can start off with OOP rework it into an imperative style, iteratively take out the destructive assignment reworking into a declarative style, and then go crazy with concurrent logic programming.
igouy
@igouy - agreed. btw. I also found OZ quite cool.
blabla999
+2  A: 

Just happened to trip over the following link and thought it might prove interesting in the context of this discussion: Functional Programming in Smalltalk

Share and enjoy.

Bob Jarvis
+1, Nice link. :)
missingfaktor