The Artima Developer Community
Sponsored Link

Weblogs Forum
Functions as Classes: Where did I steal it from?

14 replies on 1 page. Most recent reply: Nov 7, 2005 11:07 AM by MJ Stahl

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Functions as Classes: Where did I steal it from? (View in Weblogs)
Posted: Jun 26, 2005 12:11 PM
Reply to this message Reply
Summary
I have been thinking about making functions and classes equivalent in Heron, but I can't figure out where I stole the idea from.
Advertisement
I have been playing with the Heron specification lately and I am looking at an interesting possibility: making functions and classes equivalent. I know this can't be an original idea, but I don't know which languages do this kind of thing. Hopefully someone out there can point me in the right direction? Take note that while this code resembles Scala, the big difference is that functions are classes, as opposed to objects.

In this hypothetical Heron a function becomes a class with an implicit public result field.

Here is what it might look like if Heron functions became classes:

unit heron.collections;

interface Sequence[T : type]
{
  // deferred function classes
  define Foreach[F[T]]();
  define Concat(x : self);

  // public type ( alias )
  define Element = T;

  // extension class
  define Count() : int {
    define counter[T](T x) {
      enclosing.result++;
    }
    Foreach[counter]();
  }
}

// implicitly implements Sequence
define List[T : type](h : T, t : self)
{
  T head;
  self tail;
  head = h;
  tail = t;

  define Foreach[F[T]]() {
    F[T](Head);
    if (tail != null) {
      Tail.Foreach[F[T]]();
    }
  }
  define Last() : self {
    self curr = this;
    while (curr.tail != null) {
      curr = curr.tail;
    }
  }
  define Concat(x : self) {
    Last().tail = x.Clone();
  }
}

// type aliasing
define List[T : type](h : T)
  = List[T](h, null);

// class as function
define Pair[T : type](T x0, T x1) : List[T] {
  result = List[T](x0, List[T](x1));
}
So far this seems like a very reasonable thing for a language to do. Are there any possible drawbacks to this kind of design for a language that I haven't thought of?

By the way, some of my more devoted readers may notice that there are a few syntactic changes in this example. This is part of another idea I am playing with for making Heron more palatable. I am considering giving Heron more Java-like semantics as opposed to the C++ approach I had been previously championing.


Ged Byrne

Posts: 22
Nickname: gedb
Registered: Nov, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 27, 2005 9:06 AM
Reply to this message Reply
Chris,

It looks a lot like Eiffel's Agents.

http://docs.eiffel.com/eiffelstudio/general/guided_tour/language/tutorial-12.html

For example, eiffels for_all looks like this:

for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
-- Is test true for all items of target?
require
test_exists: test /= Void

http://docs.eiffel.com/eiffelstudio/libraries/base/reference/structures/iteration/iterator_flatshort.html#f_for_all

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Functions as Classes: Where did I steal it from? Posted: Jun 27, 2005 9:59 AM
Reply to this message Reply
Hi Ged,

> for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]):
> BOOLEAN

In this case test is a variable of type FUNCTION[...]. This kind of first-class function construct is common in many langauges and is roughly equivalent to a function pointer.

On the other hand what I am referring to is parameterization with the function as a type.

Ged Byrne

Posts: 22
Nickname: gedb
Registered: Nov, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 27, 2005 12:44 PM
Reply to this message Reply
Chris,

Eiffel has an entirely separate pointer class used when wrapping C libraries.
http://docs.eiffel.com/eiffelstudio/libraries/base/reference/kernel/classic/pointer_flatshort.html

Agents are full blown objects, providing access to featues such as the operands, the target (instance being acted upon) and conditions (pre and post).

The object hierarchy has classes FUNCTION and PROCEDURE, both descendants of ROUTINE.
http://docs.eiffel.com/eiffelstudio/libraries/base/reference/kernel/classic/function_chart.html
http://docs.eiffel.com/eiffelstudio/libraries/base/reference/kernel/classic/procedure_chart.html
http://docs.eiffel.com/eiffelstudio/libraries/base/reference/kernel/classic/routine_chart.html

The ideas for ETL3 stretches the whole idea further, and provides a type-safe and simplified alternative to reflection. Unfortunately none of the Eiffel compilers fully implement it yet.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Functions as Classes: Where did I steal it from? Posted: Jun 27, 2005 2:51 PM
Reply to this message Reply
> Agents are full blown objects, providing access to featues
> such as the operands, the target (instance being acted
> ed upon) and conditions (pre and post).
>
> The object hierarchy has classes FUNCTION and PROCEDURE,
> both descendants of ROUTINE.

Yes but my point is that I want to use a function as a type. An object is an instance of a type, not in of itself a type.

Ged Byrne

Posts: 22
Nickname: gedb
Registered: Nov, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 28, 2005 6:55 AM
Reply to this message Reply
Chris,

Sorry, I thought you were talking about objects that represent functions.

Does this mean that within Heron there would be no distinction between a Function and a Class. There would just be functions, and Classes would be built by using nested functions?

Sorry if I'm struggling, but I haven't looked at Heron for a few months. Is the define keyword new? I don't remember it from last time I looked at Heron.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Functions as Classes: Where did I steal it from? Posted: Jun 28, 2005 9:04 AM
Reply to this message Reply
> Chris,
>
> Sorry, I thought you were talking about objects that
> represent functions.

Sorry for the confusion, it is my fault.

> Does this mean that within Heron there would be no
> distinction between a Function and a Class. There would
> just be functions, and Classes would be built by using
> nested functions?

Actually I was playing with the idea that all functions could simply be replaced by class constructors.

> Sorry if I'm struggling, but I haven't looked at Heron for
> a few months. Is the define keyword new? I don't
> remember it from last time I looked at Heron.

I don't know if any of these ideas will make it in. The define keyword does exist in the current Heron implementation currently and is equivalent to C++ typedef. It creates a type alias. In the example I posted however I intended it to mean to define a new type.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Functions as Classes: Where did I steal it from? Posted: Jun 28, 2005 10:01 AM
Reply to this message Reply
I only skimmed over this, but it sounds a little like classes in JavaScript. Or maybe it is the reverse? In JavaScript you can use any arbitrary function as the constructor of a class. Actually, it is quite a confusing mess and makes object oriented programming in JavaScript unpalatable, IMO.

Ged Byrne

Posts: 22
Nickname: gedb
Registered: Nov, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 29, 2005 2:14 AM
Reply to this message Reply
Chris,

I think I understand now. Interesting.

Is there any chance of an extended example. Is there a specific problem that this could be used to solve?

Would all Functions be class constructors?

Say, for example, I write my own function Cos(x). Would this be a class? Would class resources be allocated and deallocated each time I use it?

Keith Gaughan

Posts: 17
Nickname: kgaughan
Registered: Feb, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 29, 2005 2:55 PM
Reply to this message Reply
> I only skimmed over this, but it sounds a little like classes in JavaScript.

JavaScript doesn't have classes. It has objects and objects alone, which are cloned and modified. That's the magic of prototype-based programming.

> In JavaScript you can use any arbitrary function as the constructor of a class.

Yes and no. The "constructor" is just a function that gets invoked when you clone it using the new operator.

> Actually, it is quite a
> confusing mess and makes object oriented programming in
> JavaScript unpalatable, IMO.

If you're dumping all that stuff in the constructor, you're doing something very, very wrong. It wastes memory through creating unnecessary closures, and it's confusing to boot.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Functions as Classes: Where did I steal it from? Posted: Jun 29, 2005 7:34 PM
Reply to this message Reply
> Chris,
>
> I think I understand now. Interesting.
>
> Is there any chance of an extended example.

Yes, but it will be a brand new post.

> Is there a
> specific problem that this could be used to solve?

What it does assure inlining of functions in higher order functions, so it is potentially a very efficient functional programming system.

> Would all Functions be class constructors?

That was an idea.

> Say, for example, I write my own function Cos(x). Would
> this be a class?

Yes.

>Would class resources be allocated and
> deallocated each time I use it?

Yes, but on the stack like in C++. So it would have the same overhead as creating a function stack frame.

I'll make a new blog post that is much clearer. I appreciate your feedback on this!

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 30, 2005 6:58 AM
Reply to this message Reply
There's been a lot of motion around this idea recently. Take a look at the demo of subtext
at: http://www.subtextual.org (http://subtextual.org/demo1.html).

The internal engine of the tools made by http://www.dynamicaspects.com/ (formerly Ergnosis) apparently unify objects and functions also.

Zoe

Posts: 5
Nickname: zoeinfo
Registered: Sep, 2003

Re: Functions as Classes: Where did I steal it from? Posted: Jun 30, 2005 12:20 PM
Reply to this message Reply
Something along these lines perhaps:

http://www.lua.org/pil/16.5.html

Tim LS

Posts: 37
Nickname: parchandri
Registered: Jul, 2005

Re: Functions as Classes: Where did I steal it from? Posted: Jul 16, 2005 9:10 PM
Reply to this message Reply
Once I understood it, I think I like this idea. It feels to me more like the class is a function (the constructor?).

You can then read the whole class definition as a function, which defines member variables, defines member functions, and finally returns the object isntance.
<P>
Then conceptually each object is built by calling any function, and objects are the same type if they have the same members defined by that function.
<P>
Taking this further, you could have conditional member definitions, which would let you create specialisations of the class at compile or run-time. I can't think of any earthshaking uses for this, so I'll just suggest optimizations for now, like this 'optimization' of multiplication for numbers:

<pre>
if( val == 0 ) define *(T x){ return 0; }
else define *(T, x){ return val * x; }
</pre>

MJ Stahl

Posts: 2
Nickname: mjstahl
Registered: Nov, 2005

Re: Functions as Classes: Where did I steal it from? Posted: Nov 7, 2005 11:07 AM
Reply to this message Reply
One language that may have inspired this was Wouter van Oortmerssen's Bla language.

http://wouter.fov120.com/bla/

Bla is a function language that was implemented to experiment with the idea of first-class environments. I have listed a code example below.

X() = self where
a = 1
b() = a + 1

I believe Wouter did an excellent job in combining the ideas of first-class functions and objects... although it seems to be pretty easy once you see Javascript (prototypical) and then Bla (class-based).

Best regards,

-M.

Flat View: This topic has 14 replies on 1 page
Topic: Optimization: Me versus Guy Steele Previous Topic   Next Topic Topic: Encapsulation Violation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use