News for hackers. Stuff that matters.
Re:What can we learn from how compilers are designed? by Bizarro on Friday March 16, 2018 @07:17PM (#16604673)
That's why I like Clojure's philosophy of a few data structures and many functions operating on them.

Of course Alan Perlis wrote "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." in 1982.

But pipelining outputs into inputs (you'll see it a lot in F# too) is just a simple, flexible concept.

As Rich Hickey once said, "We're data processors".

I think we somehow took a wrong path when OO got embraced as wholly as it did back in the 90s or so.

Re:What can we learn from how compilers are designed? by sharpercoder on Friday March 16, 2018 @08:58PM (#16605040)
Maintaining state and mutating state is useful and neatly done in OO. That is, when done right, which is hard. I certainly agree that for processing data, good functional languages reign supreme. They are usually terser while more expressive.

It seems that solving problems need their own tool. Compilers typically fit a data processing pipeline. Yet, a use case such as editing an AST (e.g. a text editor showing incomplete syntax state) probably could better use OO as a tool.

Most current software then simply uses the wrong tool for the job; they do data processing with OO, and state representation and mutation using functional pure idioms. A problem that is not yet solved very well in the industry is interplay with these idioms. .NET with C# and F# comes to mind, which allows for easy workflows leveraging both OO and functional.

Re:What can we learn from how compilers are designed? by agumonkey on Saturday March 17, 2018 @02:28PM (#16608300)
The "best" design in stateful parts of systems has yet to come to my eyes. IMO FP gives you better foundations to start stateless then see where you can group things into private mutable memory. Not that I know how to do it but at least I can reason instead of invoking innate black art or long experience.
Re:What can we learn from how compilers are designed? by mbrodersen on Sunday March 18, 2018 @08:44PM (#16615419)
> Maintaining state and mutating state is useful and neatly done in OO

I started laughing when I read "neatly done". I assume you don't mind long range state mutating effects making your code a lot more complex to reason about than it needs to be. Or the "grab a banana and you grab the whole planet" dependency issues OO almost inevitably introduce. Or ... (I could go on). OO with mutable state is anti-modular.

Re:What can we learn from how compilers are designed? by simonbyrne on Friday March 16, 2018 @08:06PM (#16604881)
> I think we somehow took a wrong path when OO got embraced as wholly as it did back in the 90s or so.

I think the particular paradigm that was adopted by C++, Java, etc. was a bad one. Having now worked quite a lot in a language with multiple dispatch (Julia), I find it really cumbersome to work in class-based single dispatch languages.

Re:What can we learn from how compilers are designed? by Bizarro on Friday March 16, 2018 @08:43PM (#16605004)
Yep, because it's forcing you to do awkward hierarchies that most people get wrong. And even if you get it right, it tends to be awkward a lot of the time.

I was always a big fan of CLOS (Of Common Lisp) and Dylan (poor Dylan).

By the way, the spiritual successor to Dylan (IMHO) Stanza http://lbstanza.org/ doesn't seem to get much love.

Re:What can we learn from how compilers are designed? by ZephyrP on Friday March 16, 2018 @08:12PM (#16604908)
Multiple (virtual) dispatch is a central feature of C++ (and I suspect Java as well).
Re:What can we learn from how compilers are designed? by groovy2shoes on Friday March 16, 2018 @08:33PM (#16604980)
Virtual dispatch in C++ and Java is dynamic dispatch, but it is singly so rather than multiply so as found in CLOS, Dylan, Julia, etc. If you want multiple dispatch in C++ and its kin, you need to jump through hoops with visitor patterns and such.
Re:What can we learn from how compilers are designed? by foota on Friday March 16, 2018 @08:39PM (#16604992)
The difference confused me for a bit, but consider the following:

  Print(Vehicle v){
    print("vehicle");
  }

  Print(Car c){
    print("car");
  }

  Print(Boat b){
    print("boat");
  }

  Vehicle test = Boat();
  
  Print(test);

In a language with multi dispatch I believe this prints boat, whereas with single dispatch it prints vehicle.
Re:What can we learn from how compilers are designed? by lmitchell on Friday March 16, 2018 @10:33PM (#16605319)
Not quite. Multiple dispatch (as mentioned in sibling comments) is basically 'virtual on both object and argument types', so like:

  class Vehicle {
    virtual void Collide(Vehicle other);
  }

  class Car {
    void Collide(Truck other) { /* hit a truck */ }
    void Collide(Car other) { /* hit another car */ }
  }

  class Truck {
    void Collide(Truck other) { /* hit another truck */ }
    void Collide(Car other) { /* hit a car */ }
  }

  Vehicle c = Car();
  Vehicle t = Truck();

  c.Collide(t); // with multiple dispatch, calls Car::Collide(Truck)
Re:What can we learn from how compilers are designed? by seanmcdirmid on Friday March 16, 2018 @11:36PM (#16605485)
You are using static overloading on the argument rather than dynamic dispatch. If the static type of truck is obscured to a super class, your behavior won't be as expected. In general, overloading shouldn't be used as a replacement for dynamic dispatch, rather it should be used to accommodate multiple incompatible types (e.g. foo(X) and foo(Y) but never foo(Z) if X <: Z and Y <: Z unless foo(Z) maintains the behavior of foo(X) and foo(Y) given an x or a y under subsumption).

Multiple dispatch is typically meant to be purely dynamic in meaning, as wiki puts it:

> Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments.[1]

(https://en.wikipedia.org/wiki/Multiple_dispatch)

Re:What can we learn from how compilers are designed? by yorwba on Saturday March 17, 2018 @01:28AM (#16605720)
The static type of both c and t is Vehicle, but by dynamic multiple dispatch Car::Collide(Truck) is selected. That's not how C++ works, but the example was exactly about illustrating how C++ doesn't work.
Re:What can we learn from how compilers are designed? by barrkel on Friday March 16, 2018 @09:16PM (#16605097)
Multiple dispatch is overriding virtual member functions on argument types, not just receiver type.

Overloading and overriding are duals; the former is statically determined, the latter dynamically determined. Just like you can overload on the full argument list, multiple dispatch lets you override on the full argument list. This lets you do things like implement the visitor pattern without double dispatch. And C++ doesn't support it without libraries.

Re:What can we learn from how compilers are designed? by zenhack on Friday March 16, 2018 @08:42PM (#16605000)
Unless it's via some new-fangled C++ feature I haven't heard of yet, I'm pretty sure C++ doesn't have multiple dispatch. Can you elaborate on what you're referring to?
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @08:20AM (#16606643)
C++ doesn't have multiple dispatch. I think the OP was referring to multiple inheritance, which is not the same thing.
Re:What can we learn from how compilers are designed? by ZephyrP on Saturday March 17, 2018 @04:18PM (#16608857)
I was thinking of runtime single dispatch with compile-time type of the arguments to that function.
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @05:01PM (#16609043)
So virtual function overloading?

Can you give a small bit of sample code to demonstrate what you mean?

Re:What can we learn from how compilers are designed? by ZephyrP on Saturday March 17, 2018 @08:17PM (#16609835)
just ordinary virtual function that discriminates upon arguments (at compile-time).
Re:What can we learn from how compilers are designed? by zenhack on Saturday March 17, 2018 @10:42PM (#16610245)
Yeah, multiple dispatch implies more than one argument involved in the dispatch decision at runtime. So that's not the same thing.
Re:What can we learn from how compilers are designed? by stochastic_monk on Friday March 16, 2018 @07:26PM (#16604715)
Similarly, I like Python and C++’s emphasis on duck-typing. I use STL containers often, and provide similar interfaces to my custom data structures for flexible, general, fast code.
Re:What can we learn from how compilers are designed? by jonathanyc on Friday March 16, 2018 @08:03PM (#16604868)
I wish there were tools to auto generate documentation on what interface a duck-typed consumer expects, e.g. what std::vector<T> expects of T.
Re:What can we learn from how compilers are designed? by stochastic_monk on Friday March 16, 2018 @08:12PM (#16604905)
Well, C++17 concepts enforce requirements, but hopefully metaclasses will do more in 20.
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @08:22AM (#16606647)
Concepts are also C++20. They were voted in, however, shortly after 17 shipped.
Re:What can we learn from how compilers are designed? by stochastic_monk on Saturday March 17, 2018 @11:17AM (#16607316)
Good point. I was thinking of the rudimentary, template-based use of concepts, not the polished, C++20 version.
Re:What can we learn from how compilers are designed? by bstamour on Saturday March 17, 2018 @02:18PM (#16608253)
Ah gotcha. Yes in the current situation we can do nearly everything that the concepts proposal for C++20 does with enable_if, void_t, and a few other helpers. Though concepts have the benefit of letting you "say what you mean", instead of relying on template magic.
Re:What can we learn from how compilers are designed? by orf on Friday March 16, 2018 @09:07PM (#16605072)
This should be more than possible with Pythons type annotations, when done right. I.e 'iterable[str]' can accept a generator, list, tuple, any class with the right __iter__ and of course a string itself.
Re:What can we learn from how compilers are designed? by wirrbel on Saturday March 17, 2018 @03:26PM (#16608582)
This is what I like about Rust and Rust's traits. You write a generic and specify the properties of the generic type parameter, for example that it must implement another trait or something.
Re:What can we learn from how compilers are designed? by mbrodersen on Sunday March 18, 2018 @08:39PM (#16615398)
> That's why I like Clojure's philosophy of a few data structures and many functions operating on them.

Which in practice means that a few data structures that are not perfect for solving a specific problem are forced (with repeated hammer blows) to bend to the problem. Like the old style LISP AList's used as a map. Probably the worst possible structure for representing a map. But yeah it kinda works as long as you don't have much data.

Re:What can we learn from how compilers are designed? by kazinator on Monday March 19, 2018 @10:37AM (#16618631)
The assocation list is a poor data structure for representing a map, if the most important requirement is to have fast access to any key of a large map.

However, the association list is an excellent data structure if an important requirement is to be able to cheaply extend a map with new keys functionally: without altering the original. It also provides the requirement that duplicate keys added to the front shadow existing ones. It is also very simple; it is easy to verify that operations are correct.

With the above properties, association lists, directly provide an easy and correct implementation model for lexical environments.

The empty top-level lexical environment is an empty list. Adding a frame with new variables, including ones that shadow existing variables, is just consing pairs onto the list. The assoc operation correctly implements lexical variable lookup. The environment for making a lexical closure is just a copy of the assoc list pointer that you have now. Because the bindings are independent pair objects in the association list, individual bindings can be located and selected, and combined into another association list. A newly created association list environment can trivially capture an arbitrary subset of bindings from another one without mutating it.

Even if we don't use this representation in production use, it provides an important reference model. Anything more complicated has to produce the same results; we can start with assoc based environments, build a lot of code, and then use that as regression test cases for going to a different environment representation.

It's good enough for pattern matching and logic languages where the number of variables isn't great, but the search has to do things that are complicated enough without a heavy-weight environment structure.

Re:What can we learn from how compilers are designed? by kbp on Monday March 19, 2018 @09:25AM (#16618150)
> AList's used as a map. Probably the worst possible structure for representing a map.

They're more flexible than hash tables and often faster; what makes them so awful? They both have trade-offs that make them better and worse in different contexts, depending what you're optimising for.

Re:What can we learn from how compilers are designed? by lispm on Monday March 19, 2018 @09:01AM (#16617994)
> Probably the worst possible structure for representing a map.

Not really.

But native hashtables had been added to Lisp in the 70s...

Re:What can we learn from how compilers are designed? by kazinator on Monday March 19, 2018 @10:44AM (#16618693)
Under the right circumstances, a lookup table could be worse. In a simple interpreter, I would hate to pass down a vector of pairs for the environment representation; it would have to be wholly duplicated to extend the environment.
Re:What can we learn from how compilers are designed? by marcosdumay on Saturday March 17, 2018 @01:56PM (#16608144)
I remember my undergrad classes where the types of software engineering diagrams were presented. There were some very informative useful data transformation diagrams that were just glossed over in order to reach the useless UML shit.

I took a very important lesson from those classes, but not the one the professor was trying to explain.

Re:What can we learn from how compilers are designed? by kazinator on Monday March 19, 2018 @03:15PM (#16622078)
It's worth reading all of those Perlis' epigrams:

103. Purely applicative languages are poorly applicable.

Give me a hundred functions on one data structure, but make thirteen of them useful mutators. :)

Re:What can we learn from how compilers are designed? by skybrian on Friday March 16, 2018 @08:54PM (#16605027)
If you want to avoid OO you'll also want to avoid treating functions as first-class values. As soon as you have structs that contain functions, you can build OO.
Re:What can we learn from how compilers are designed? by adrianN on Saturday March 17, 2018 @02:48AM (#16605876)
OO is a way of designing programs. You can avoid it in most languages if you want to and you can do it in most languages if you want to. The language only determines the amount of boilerplate you need.
Re:What can we learn from how compilers are designed? by skybrian on Friday March 16, 2018 @11:53PM (#16605526)
Since I'm getting downvoted:

Folks, this is Chapter 3 of Structure and Interpretation of Computer Programming. Technically you don't even need a struct; closures and mutability will do.

And how do you serialize data that function pointers in it?

Re:What can we learn from how compilers are designed? by coldtea on Saturday March 17, 2018 @10:35AM (#16607109)
You're not downvoted because people ignore that fact. You are downvoted because people disagree that it's important.

Yes, you can create objects with closures, but when people talk against OO programming, they mean the paradigm, not merely the possibility that a language can do objects. Forth and C can do object oriented programming just fine too, if one is so inclined.

Re:What can we learn from how compilers are designed? by HumanDrivenDev on Saturday March 17, 2018 @01:48AM (#16605756)
I am almost positive Rich Hickey hasn't read that book. The only thing more stunning than his ignorance when he talks about OO is the amount of young programmers who believe everything he says.

It blew my mind when I realised I could write objects from scratch in languages with closures, even statically typed ones! Of course each object has its own v-table in the SICP code, so probably not something you should use in production.

Re:What can we learn from how compilers are designed? by pjmlp on Saturday March 17, 2018 @03:00AM (#16605897)
I always find ironic when such young devs bash OOP, given that Clojure has protocols and multi-methods.

Or bashing Java and the JVM, without which Clojure would not exist in first place.