23

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?

18
  • 17
    Because Smalltalk is the poster boy for object oriented languages? Commented Aug 20, 2010 at 2:30
  • 6
    Being able to use functions as first-class objects doesn't make a language "functional" any more than being able to write procedures makes a language "procedural". Commented Aug 20, 2010 at 5:27
  • 4
    @Gabe: Some would argue that this one property alone is sufficient for a language to wear a "functional programming language" tag. Commented Aug 20, 2010 at 5:50
  • 14
    @CurtainDog: OO and functional are not mutually exclusive paradigms. Commented Aug 20, 2010 at 12:46
  • 5
    @CurtainDog that's a nice academic position to take, but practically it makes no sense: I'm not talking about a paper someone wrote, I'm talking about how Smalltalkers do actually write, which is a very functional manner, using objects. Methods that are side effect free, methods/closures as first-class entities (objects, just like Common Lisp, another functional programming language). No surprise, given Smalltalk's heavy Lisp influence. Commented Aug 23, 2010 at 6:25

9 Answers 9

28

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):

Sign up to request clarification or add additional context in comments.

14 Comments

Indeed, Smalltalk is an almost functional language. There are extensions to Smalltalk that provide functional pattern matching, e.g. map.squeak.org/package/3772b420-ba02-4fbd-ae30-8eadfc323b7b. Furthermore, Newspeak (newspeaklanguage.org) is a programming language in the tradition of Self and Smalltalk that supports immutability as one of its core concepts.
Doesn't that definition make C#, Javascript and pretty much every language from this century functional?
@Diego: As I said, there's no accepted definition of the term "functional programming language". So, by first definition, yes. By second definition, no.
@Missing Faktor - Does Smalltalk even provide a way to declare named functions?
@AngelO'Sphere, I am intimately familiar with what first class functions are. (Scala and Haskell are my primary languages.) Thank you for your gratuitous explanation of the term anyway. Now: Having classes at top level is orthogonal to having first class functions. Scala, Smalltalk, and many others have both characteristics. Or are you now going to ammend you definition with "must support top level function definitions"?
|
24

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.

36 Comments

>> Smalltalk might not be a "pure functional language" (whatever that might be) << Is Smalltalk even an impure functional language?
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" cs.brown.edu/~sk/Publications/Papers/Published/kff-synth-fp-oo
>> 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: "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."
@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.
|
13

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.

2 Comments

Can you name some examples of FPL things that Smalltalk can't express "primitively"?
Even though this is an old thread, I take issue with the definition of OO presented here. OO is merely expressing a program in the form of entities exchanging messages and encapsulating state; "modeling" the problem domain directly as objects doesn't enter the definition but rather - IMO - is a tool or even a trick. In that sense, a functional program can be seen as entities encapsulating zero state and interacting through very simple messages ("apply"). This would make the functional model a subset of OO.
7

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).

5 Comments

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.)
>> Internal states (collections of instance variables) are identical to externally defined structs. << Except for the ways they are not, for example - their accessibility.
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 - I didn't mention mutable state so presumably your comment is directed to Manuel?
@igouy sorry, not my clearest of comments. "mutable state" was directed at Manuel, but "your definition of an FPL" was directed to you.
5

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

1 Comment

What's immutability of a variable in a mathematical sense? For example, in the equation x^2 - 1 = 0 is x immutable? Does the iteration through all possible values of x ({-1, 1}) make it mutable? For example, the following code in Scala: val results: Set[Double] = solve("x^2 - 1 = 0") returns all possible values of x to results, but the function 'solve' CAN do some mutations of an internal variable, so in Smalltalk any instance can mutate its internal state to elaborate the answer. Anybody could also write a code responding with multiple results in one object in Smalltalk.
5

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 !

8 Comments

You can have setters, as long as you have your setters return a new object with the required state rather than mutating the receiver.
>> there is a duality between objects with methods and a set of functions << Yes, they are duals - they are not the same thing.
>> 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?
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.
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
|
2

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.

1 Comment

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.
1

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.

8 Comments

That describes Erlang too. Is Erlang now non-functional?
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 >> 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: so it's your position that a language can EITHER be an FPL OR an OOPL, but never both?
@jgouy I miss your point. I'm stubborn as a mule, or do you mean the MUltiLingual Extension to Emacs might teach me something?
|
0

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.

9 Comments

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.
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 - There's a well understood and fundamental difference in how we structure programs in OOP and FP - they are duals.
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.
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..."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.