Is there a connection between calling a function and creating an object in pure functional languages?

Imagine a simple (composed) language where functions look like this:

function f(a, b) = c + 42 where c = a * b 

(Let's say this is a subset of Lisp that includes "defun" and "let".)

Also imagine that it includes immutable objects that look like this:

 struct s(a, b, c = a * b) 

Again, the analogy with Lisp (this time a superset), say, such a definition of the structure should generate functions for:

 make-s(a, b) sa(s) sb(s) sc(s) 

Now, given the simple setup, it seems clear that there are many similarities between what happens behind the scenes when you invoke f or make-s. When "a" and "b" are transmitted during invocation / instantiation, enough information is available to calculate "c".

You might think of creating an instance of the structure as a function call, and then saving the resulting symbolic environment for later use when calling the generated access functions. Or you might think of evaluating a function as creating a hidden structure, and then using it as a symbolic environment with which to evaluate the final expression of the result.

Is my toy model so simplistic that it is useless? Or is this a really useful way to think about how real languages ​​work? Are there any real languages ​​/ implementations that someone who does not have CS experience, but with an interest in programming languages ​​(i.e. me), should learn more about this in order to learn this concept?

Thanks.

EDIT: Thanks for the answers so far. To sort things out a bit, I think I'm wondering if there are any real languages ​​that people who speak the language speak. "you should think that objects are essentially closures." Or, if there are any realities of the real language, where is the case when an object instance and a function call are actually shared by some common (non-trivial, that is, not only library calls) code or data structures.

Is the analogy that I am doing that I know different, earlier than deeper than a simple analogy in any real situations?

+6
language-agnostic immutability functional-programming lisp
source share
4 answers

Both f and make-s are functions, but the similarities do not go much further. Using f calls a function and executes its code; applying make-s creates a structure.

In most language implementations and modeling, make-s is a different kind of object from f : f is closure, while make-s is a constructor (in functional languages ​​and a logical value that is close to the value of object-oriented languages).

If you like to think in an object-oriented way, then both f and make-s have an apply method, but they have completely different implementations of this method.

If you like to think about the basic logic, f and make-s are of type build on a constructor of the samme type (constructor of a function type), but they are built in different ways and have different destruction rules (application-application or constructor).

If you want to understand this last paragraph, I recommend Types and Programming Languages from Benjamin C. Pierce. Structures are discussed in Β§11.8.

+1
source share

You can't get much cleaner than lambda calculus: http://en.wikipedia.org/wiki/Lambda_calculus . Lambda calculus is actually so pure, it has only functions!

The standard way to implement a pair in lambda calculus is as follows:

 pair = fn a: fn b: fn x: xab first = fn a: fn b: a second = fn a: fn b: b 

So pair ab , what you might call "struct" is actually a function ( fn x: xab ). But this is a special type of function called closure. Closing is essentially a function ( fn x: xab ) plus values ​​for all the β€œfree” variables (in this case a and b ).

So, creating an instance of "struct" is like calling a function, but more importantly, the actual "structure" itself is like a special type of function (closure).

If you think about how you will use the lambda calculus interpreter, you can see symmetry on the other hand: you can implement closure as an expression plus a structure containing the values ​​of all free variables.

Sorry if this is all obvious and you just need an example of the real world ...

+3
source share

There is a connection between objects and closure. http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html

The following creates what some can call a function, while others can call an object:
Adapted from SICP ( http://mitpress.mit.edu/sicp/full-text/book/book-ZH-21.html )

 (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT" m)))) dispatch) 
+1
source share

Is my toy model so simplistic that it is useless?

Essentially, yes. Your simplified model basically boils down to saying that each of these operations involves performing a calculation and placing the result somewhere. But it is so general that it covers everything that a computer does. If you did not perform the calculations, you would not do anything. If you didn’t put the result somewhere, you would have done the work in vain, since you have no way to get the result. So, everything that is useful to you with a computer, from adding two registers along with getting a web page, can be modeled as performing a calculation and putting the result somewhere that can be accessed later.

+1
source share

All Articles