News for hackers. Stuff that matters.
Re:Codata in action, or how to connect FP and OOP by mirekrusin on Monday August 10, 2020 @02:29AM (#24106218)
" (...) - In an OO language, you can add a new case by creating another subclass and implementing the methods. But, if you want to add a new function to a datatype, you have to add it to the superclass and implement it on all the existing subclasses, which means modifying a lot of files and recompiling most of the program.

- In a FP language you can add a new function to an existing datatype just by adding a function. But, if you want to add a new case, you have to add the constructor and modify all the functions that use the datatype, which means modifying a lot of files and recompiling most of the program.

(...)"

...and by using multiple dispatch (as in Julia) you can do both. And it just works. True code reuse. Why did it take us so long to recognise this?

Re:Codata in action, or how to connect FP and OOP by tsimionescu on Monday August 10, 2020 @03:21AM (#24106503)
I don't think multiple dispatch fixes this problem in any way.

The idea here is that you can have an opaque interface. Then, adding new implementations of that interface is very easy. But, if you want to extend the interface, you need to modify all of the existing implementations. Multiple dispatch suffers of this problem just as much as single dispatch.

In the other case, you have a transparent interface - anyone can build code based on your data type. But, if you add more cases to your data type, anyone who had functionality built on it needs to take into account the new cases.

I think that this is in inherently an unavoidable problem. You can of course expose both kinds of constructs in your language, but you can't make interface implementation easily modifiable, and you can't make sum types easily extensible. The programmer ultimately has to choose the one that they believe will best capture the domain and its probable evolution.

What multiple dispatch can do is take some cases where sum types are obviously preferable to single dispatch (operations on related objects) and apply the interface abstraction to those cases as well (without reaching for horrible solutions like the visitor pattern).

Edit: thinking a bit more about the article, what they are showing is that you can essentially use the visitor pattern (and multiple dispatch) to implement GADTs instead of using them for regular multiple dispatch with inheritance. Basically, you can use them bring completely disparate types under the same umbrella, just like a GADT. I'm not sure that this still allows subtyping of that GADT as if it were a regular class.

Re:Codata in action, or how to connect FP and OOP by codr7 on Monday August 10, 2020 @07:53AM (#24108270)
There is no need to modify existing implementations when extending an interface using multiple dispatch.

You would need to add the required implementations, but there's really no way around that since the logic has to go somewhere.

And as soon as you start actually dispatching on multiple arguments, using sum types to solve the same prolem quickly turns into spaghetti.

Re:Codata in action, or how to connect FP and OOP by tsimionescu on Monday August 10, 2020 @08:30AM (#24108530)
> There is no need to modify existing implementations when extending an interface using multiple dispatch.

Sure there is. If I have a function that takes a Foo and a Bar, and it calls a generic method on them, the generic method must have methods that cover the real subtypes of Foo and Bar. If someone had created subtypes of Foo and Bar and defined methods on all the generic methods that they knew about, and now you create a new generic method, you have absolutely broken the interface just as much as adding a new method to Iterable in Java.

Re:Codata in action, or how to connect FP and OOP by codr7 on Monday August 10, 2020 @10:01AM (#24109310)
It must have enough methods to get the behavior you want, that doesn't always mean all subtypes.

Assuming you're running a version of Java that supports default implementations, which may often be used to avoid breaking the interface.

Sum types are a different story, as you would have to dive into existing code.

Re:Codata in action, or how to connect FP and OOP by contravariant on Monday August 10, 2020 @05:17AM (#24107168)
> I'm not sure that this still allows subtyping of that GADT as if it were a regular class.

It does, more or less, since any subtype will have to implement the "accept" method for the visitor, which has been defined to only 'accept' the values that can be represented by the GADT. Any subtype will need to define how its possible values map to that of the supertype.

Re:Codata in action, or how to connect FP and OOP by hlasdjlfhalwjk on Monday August 10, 2020 @05:18AM (#24107171)
Some Rust libraries use a trick to make sum types extensible without introducing breaking changes when a new invariant is added. There is a private/undocumented invariant in the type so every pattern match over the type must have a default/catch-all/wildcard branch (e.g. it is not possible to do an exhaustive match by naming all existing variants). By having a default branch, the match is always exhaustive and there will be no error if a new variant is added. For fields in a sum type something similar can be done: an undocumented, hidden field that cannot be used when the variant is destructed. Therefore it can be only partially destructed and in this case it doesn't matter how many other fields there are in the type variant.

Here is an example: https://github.com/rust-lang/regex/blob/master/regex-syntax/...

Re:Codata in action, or how to connect FP and OOP by ori_b on Monday August 10, 2020 @07:23AM (#24108028)
Forcing a default case is a big step back towards dynamic typing.
Re:Codata in action, or how to connect FP and OOP by heavenlyblue on Monday August 10, 2020 @09:45AM (#24109142)
“Hey - we have a solution to your type problem; we just disabled type checking. Genious”
Re:Codata in action, or how to connect FP and OOP by pa7x1 on Monday August 10, 2020 @07:53AM (#24108268)
Relevant presentation by one of the creators of Julia: https://www.youtube.com/watch?v=kc9HwsxE1OY
Re:Codata in action, or how to connect FP and OOP by amboo7 on Monday August 10, 2020 @02:50AM (#24106338)
Common Lisp Object System had multiple dispatch for ages. Some other religions do not like it.
Re:Codata in action, or how to connect FP and OOP by StefanKarpinski on Tuesday August 11, 2020 @09:58PM (#24127909)
A subtlety we've discovered through success and growth of Julia is that even if your language has some solution to the expression problem, if it's has non-zero cost and isn't the default then people won't actually use it, which undermines much of the benefit. Systems like CLOS, Clojure and even R, which have multimethods but where they're opt-in and use slow hash-based implementations, don't in practice see most of the benefits that we've seen in Julia from multiple dispatch in terms of code reuse and composability. Another way to say this is that if multiple dispatch is just a feature that you reach for when you need to do something fancy, then it looks like just another feature; when it's ubiquitous it becomes transformative.
Re:Codata in action, or how to connect FP and OOP by kjeetgill on Monday August 10, 2020 @03:43AM (#24106622)
Any OOP with inheritance does multiple dispatch. Java, C++ vitual functions, python via ducktyping.

The article's concern is real but overstated in my opinion. When adding a new function in a hierarchy you'll either have a good default or you'll be implementing via type matching branches or 10 implementations in each type behind multiple dispatch.

The difference, to me, is minimal.

Re:Codata in action, or how to connect FP and OOP by MaxBarraclough on Monday August 10, 2020 @07:27AM (#24108054)
> Any OOP with inheritance does multiple dispatch. Java, C++ vitual functions

This is incorrect. C++ and Java are typical examples of languages that lack multiple dispatch, but are able to emulate it using the visitor pattern, as tsimionescu already mentioned.

https://en.wikipedia.org/wiki/Multiple_dispatch#Emulating_mu...

Re:Codata in action, or how to connect FP and OOP by kjeetgill on Monday August 10, 2020 @12:55PM (#24111345)
Ah, yea. I was confusing dynamic dispatch for multiple dispatch.

My bad!

Re:Codata in action, or how to connect FP and OOP by tsimionescu on Monday August 10, 2020 @04:06AM (#24106754)
> Any OOP with inheritance does multiple dispatch. Java, C++ vitual functions, python via ducktyping.

Java, Python and C++ require Visitor machinery that you ahev to write, for each case, to implement multiple dispatch (and Visitor only works for double dispatch, if you want more than 2 arguments you need even more boilerplate). Julia and CLOS essentially have that machinery built in.

In case this is not clear, multiple dispatch refers to choosing which method implementation to call based on the specific runtime subtypes of all function arguments, not just the runtime type of `this` (which is all that Java, C++, and even Python can dispatch based on). It's basically like applying C++ function overloading rules at runtime.

> The article's concern is real but overstated in my opinion. When adding a new function in a hierarchy you'll either have a good default or you'll be implementing via type matching branches or 10 implementations in each type behind multiple dispatch.

That's assuming you can modify all existing implementations. If you can't (e.g. if you wanted to modify Iterable to add a new method) then the solution is just.. don't do that. Note that the article also doesn't offer a solution - they're just showing that multiple dispatch can be used to implement a different construct with different limitations (GADTs).

Re:Codata in action, or how to connect FP and OOP by jcelerier on Monday August 10, 2020 @04:11AM (#24106774)
> Java, Python and C++ require Visitor machinery that you ahev to write, for each case, to implement multiple dispatch (and Visitor only works for double dispatch, if you want more than 2 arguments you need even more boilerplate). Julia and CLOS essentially have that machinery built in.

we're in 2020

     using value = std::variant<int, float, string>;
     void foo(value a, value b, value c) {
       struct {
          void operator(int, float, string) { }
          void operator(float, int, string) { }
          void operator(float, string, int) { }
          ///... etc... 
       } visitor;

       std::visit(visitor, a, b, c);
     }
works fine (and has worked fine since a long time with either boost.variant or in C++11 with eggs.variant, etc...)
Re:Codata in action, or how to connect FP and OOP by tsimionescu on Monday August 10, 2020 @08:19AM (#24108455)
Sure, it's 2020 and you have less boilerplate to write. It's still a little more than Julia or CL. Also, Java doesn't have anything similar in the std lib as far as I know.

More importantly, this is not as powerful as true multiple dispatch like in CLOS. There is no way to call other methods, there is no way to specialize partially (e.g. combine the variant with subtypes, and define methods only for some combinations of subtypes and super types on the parameters, and allow the implementation to choose the most specific one).

Edit: to add a few more details on my second point. CLOS (and I assume Julia too) allows you to call 'parent' implementations from a child implementation, similar to calling base type methods in virtual dispatch - you can call the next-most-specific method that would have matched the params, which you can't with the visitor. Also, say I have a generic function F(Foo, Bar). I could define methods for F(FooSubtype1, Bar) and F(Foo, BarSubtype1), and then call F(FooSubtype1() , BarSubtypeOfSubtype1() ) and have the right implementation get called (F(Foo, BarSubtype1)).

Re:Codata in action, or how to connect FP and OOP by codr7 on Monday August 10, 2020 @08:00AM (#24108324)
Well, you can write Fortran in any language.

It's far from the same thing though, less convenient and less flexible.

Multiple dispatch doesn't require a centralized declaration of the possible types and allows putting implementations in different files.

Re:Codata in action, or how to connect FP and OOP by 08-15 on Monday August 10, 2020 @03:32AM (#24106560)
> Why did it take us so long to recognise this?

It didn't take us long. Schemers did multiple dispatch at least 40 years ago. SICP explains how to do it.

(Arguably, both R and Julia are Lisp dialects under the hood, just disguised behind a more arcane syntax. Which goes to show that 'practioners' take Lisp seriously exactly as long as they don't know it's Lisp.)

Re:Codata in action, or how to connect FP and OOP by FridgeSeal on Tuesday August 11, 2020 @02:48AM (#24117794)
Too many programmers (in my opinion) give up far too early due to surface level syntactical differences.

I have a friend who is like this, looked at some Rust code, saw closures with bars instead of parentheses and practically disregarded it on the spot. Took a lot of convincing that they wouldn’t notice after 15 minutes of actually using the language, and what do you know, I was right.

Re:Codata in action, or how to connect FP and OOP by pansa2 on Monday August 10, 2020 @04:29AM (#24106880)
Solving the expression problem requires maintaining static type safety. AFAIK Julia doesn’t do this because it’s dynamically typed.
Re:Codata in action, or how to connect FP and OOP by shele on Monday August 10, 2020 @05:27AM (#24107227)
People using Julia are quite happy how it alleviates the expression problem compared to languages they used before. Your argument, if I get it right, that Julia can't solve the expression problem because "the expression problem requires maintaining static type safety" reminds of the "Bumblebees can't fly" fable.
Re:Codata in action, or how to connect FP and OOP by saithound on Monday August 10, 2020 @05:49AM (#24107375)
Well, the expression problem is a clearly formulated question: how can we have three desirable things at the same time (no need to modify existing code, no need for code repetition and no need for runtime type errors [1])? See also the project management triangle: if you need something fast, good and cheap, then fast, good and expensive won't be a satisfactory answer, no matter how many millionaires are satisfied with the latter.

[1] https://link.springer.com/chapter/10.1007/978-3-540-24851-4_...

Re:Codata in action, or how to connect FP and OOP by shele on Monday August 10, 2020 @08:26AM (#24108499)
Honestly, if you want to discuss the progress made on the expression problem by a dynamical language whose power and extensibility partly comes from a collection of informal, consensus based interfaces you have to start from a different theory.
Re:Codata in action, or how to connect FP and OOP by Hitton on Monday August 10, 2020 @05:39AM (#24107296)
Most people using language X are quite happy how X does things compared to languages they used before. That's why they use the language X. That's also the reason why I consider this type of argument unhelpful.
Re:Codata in action, or how to connect FP and OOP by shele on Monday August 10, 2020 @05:51AM (#24107386)
No, not "most people" and not "happy how X does things" but specifically people who have thought about the expression problem's opinion about how Julia alleviates the expression problem compared to other languages they know.
Re:Codata in action, or how to connect FP and OOP by mirekrusin on Monday August 10, 2020 @06:31AM (#24107613)
I don't think you can classify Julia like this.

It's kind of both.

Julia has types on bindings, in that sense it is statically typed.

Re:Codata in action, or how to connect FP and OOP by pansa2 on Monday August 10, 2020 @06:39AM (#24107653)
“Julia is squarely in the dynamic camp” - Stefan Karpinski, Julia co-creator

https://stackoverflow.com/a/28096079

Re:Codata in action, or how to connect FP and OOP by mirekrusin on Monday August 10, 2020 @08:17AM (#24108441)
...but you've ommited what I meant "(...) Unlike many dynamic languages, however, Julia has a fairly sophisticated language for talking about types, and you can annotate expressions with types. (...)"

Also other answers have some good information, ie:

" (...) It can also be useful to indicate the argument types to restrict the kind of parameters passed when calling. Our function header for floating point numbers would then look as function mult(x::Float64, y::Float64). When we call this function with mult(5, 6), we receive an error, ERROR: 'mult' has no method matching mult(::Int64, ::Int64 ), proving that Julia is indeed a strongly typed language. It does not accept integer parameters for the floating point arguments. (...) "

...in other words saying that "Julia is dynamic langauge, full stop." doesn't answer the question fully as for many readers it implies "weak typing", however it's dynamic, but strongly-typed, with state of art optimisations available as in statically typed languages, with proper multiple dispatch on all arguments (not single/this dispatch as in ie. java) etc.

It is definatelly worth playing with the language for people who hesiate on learning it - Julia provides some very interesting features not seen anywhere else.

Re:Codata in action, or how to connect FP and OOP by codr7 on Monday August 10, 2020 @08:05AM (#24108350)
Well, that's like...his perspective.

It's a spectrum, not either or.

Julia and Common Lisp are somewhere in the middle.