News for hackers. Stuff that matters.
Re:Towards Scala 3 by YorkshireSeason on Friday April 20, 2018 @10:42AM (#16885142)

   Haskell is category theory 
   as a language.
I disagree with this. Haskell's creation [1, 2] predates the realisation (by the FP community at large) of the close connection between parts of category theory and parts of pure functional programming, which happened in the 1990s, perhaps driven by Moggi's realisation that monads are a fundamental abstraction in computation that can reconcile effects with pure functional computation. More importantly, there is no category "Hask" of Haskell programs, and there isn't even a candidate category that's even close.

One of Odersky's motives in creating Scala was bringing the power of Haskell into the JVM world, although this is not the only motive (tight integration of OO and FP being another).

Haskell's key innovation over its predecessors (Miranda and ML) was the addition of higher-kinded types, which in turn made ad-hoc polymorphism digestable in a typed world (in the form of type-classes). The combination of HKTs and type-classes enables the monadic abstractions that contemporary Haskell programming is widely known for today.

Note that Scala has HKTs and type classes (via implicits), so Haskell programs can typically transliterated into idiomatic Scala without major complications.

[1] P. Hudak, J. Hughes, S. Peyton Jones, P. Wadler, A History of Haskell: Being Lazy With Class. http://haskell.cs.yale.edu/wp-content/uploads/2011/02/histor...

[2] Interview with Simon Peyton-Jones. http://www.cs.cmu.edu/~popl-interviews/peytonjones.html

[3] E. Moggi, Notions of Computation and Monads. https://core.ac.uk/download/pdf/21173011.pdf

Re:Towards Scala 3 by saosebastiao on Friday April 20, 2018 @11:15AM (#16885438)
> One of Odersky's motives in creating Scala was bringing the power of Haskell into the JVM world, although this is not the only motive (thigh integration of OO and FP being another).

I'm not sure why you would think that. Odersky has long claimed ML as his primary functional programming influence, not Haskell. It does have HKT, so there's that...but that's about it. Typeclasses aren't really a part of the language. They were made possible by implicits. They have since had some language level support (context bounds), but that is more of a nice to have than a primary influence.

Just because Scala can "do" Haskell doesn't mean odersky was inspired by it any more than ML or Java.

Re:Towards Scala 3 by YorkshireSeason on Friday April 20, 2018 @11:29AM (#16885564)
From an abstract PL theory POV, Haskell's key innovation over ML was HKTs. It was an experiment at the time, but one that was successful beyond all expectation: I cannot imagine designing a new PL without HKTs. (Note that Rust is also trying to add HKTs, but has been running into difficulties with type-based lifetime tracking IIRC.)

Implicits are a generalisation of default arguments (I don't know where they were pioneered, maybe C++). Implicits were first tried in Haskell [1]. Scala refined them in several steps, the last being [2] which represents implicits on the type leve. Mimicking type classes via implicits is a well-established Scala idiom [3].

[1] J. R. Lewis, J. Launchbury, E. Meijer, M. B. Shields, Implicit Parameters: Dynamic Scoping with Static Types. https://galois.com/wp-content/uploads/2014/08/pub_JL_Implici...

[2] M. Odersky, A. Biboudis, F. Liu, O. Blanvillain, Simplicitly: Foundations and Applications of Implicit Function Types. https://infoscience.epfl.ch/record/229878/files/simplicitly_...

[3] B. C. d. S. Oliveira, A. Moors, M. Odersky, Type Classes as Objects and Implicits. http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf

Re:Towards Scala 3 by lptk on Saturday April 21, 2018 @10:23AM (#16891520)
> Haskell's key innovation over ML was HKTs

In fact, ML modules have had higher-kinded types [1] since before Haskell even existed. I guess Haskell's main innovation in this domain is really its very convenient higher-kinded parametric polymorphism with type classes.

[1] MacQueen, D B (1984). Modules for Standard ML. Conference Record of the 1984 ACM Symposium on LISP and Functional Programming Languages. 198-207.

Re:Towards Scala 3 by YorkshireSeason on Thursday April 26, 2018 @06:49AM (#16930708)
Thanks. That's really interesting. I need to read MacQueen's paper. Is this full HKT, in the sense that all constructions wiht HKTs can be encoded in MacQueen's system?
Re:Towards Scala 3 by lptk on Monday April 30, 2018 @06:09AM (#16957273)
If by "all constructions wiht HKTs", you mean what can be done with HKTs in Haskell, then I'd say yes. It is well-known that ML modules provide a very advanced level of expressiveness, especially since OCaml's introduction of first-class modules. Also, Scala took this idea further and provides principled recursive first-class modules (which Scala calls dependent object types).

The problem is that modules in ML have a verbose syntax and are clunky to use compared to type classes. OCaml's modular implicits aim to make this better (see https://arxiv.org/abs/1512.01895), taking inspiration from Sala's implicits.

Re:Towards Scala 3 by YorkshireSeason on Thursday May 03, 2018 @01:47PM (#16988093)
I understand that making modules first-class gives them a lot of expressivity. But they are a fairly recent development" at least in OCaml the come much after Haskell. But are modules in MacQueen's sense first-class?
Re:Towards Scala 3 by saosebastiao on Friday April 20, 2018 @12:07PM (#16885939)
> Implicits are a generalisation of default arguments.

There is an extreme semantic difference between an implicit parameter and a default argument. Have you spent any time at all using them? That's like claiming HKT is just a generalization of type constructors. Would you claim that HKT isn't anything new or novel because constructors have been around forever?

Re:Towards Scala 3 by YorkshireSeason on Friday April 20, 2018 @12:34PM (#16886232)
> Have you spent any time at all using them?

Sure I have used both.

Default arguments and implicits both have the same key idea: you can omit arguments to functions, and the compiler, guided by type information, synthesises the missing arguments during compilation. In order to understand the difference between both, it is crucial to realise that this compile-time synthesis of missing arguments has two related but different dimensions.

- Declaration that an argument is allowed to be omitted (and hence synthesised automatically).

- Declaration of the missing argument that is used in this synthesis.

Default arguments merge these two into one, e.g. with

   def f ( int x ) ( bool b = false ) = ...
   f (2)
   f (2)
all calls f(2) become f(2)(false). The problem with this is that the devault value to be used in synthesis cannot be context dependent.

Implicit arguments separate these two, enabling the programmer to make default context dependent, e. g.

   def f ( int x ) ( implicit bool b ) = ...
   implicit val c = true
   f (2)
   implicit val c = false
   f (2)
Now the first call f(2) is rewritten to f(2)(true), while the second becomes f(2)(false).

> Would you claim that HKT isn't anything new or novel because constructors have been around forever?

I'm not sure I see the connection: constructors are program constructs, while HKTs are "types for types".

Re:Towards Scala 3 by lmm on Friday April 20, 2018 @05:32PM (#16888596)
> Default arguments and implicits both have the same key idea: you can omit arguments to functions, and the compiler, guided by type information, synthesises the missing arguments during compilation.

Saying implicits are a generalization of default arguments because they're both synthesized during compilation is like saying steam trains are a generalization of pipes because they're both made of metal. It elides too big a difference to be helpful, to the point that it's actually misleading.

Re:Towards Scala 3 by YorkshireSeason on Saturday April 21, 2018 @05:20AM (#16890665)
I'm not saying implicits and default parameters are the same thing. I'm saying the key idea behind implicits is the realisation that default arguments merge two ideas that should be kept separate, if we want context dependent defaults. Some of implicit's other features can (and have been) added to default arguments, see my other reply.
Re:Towards Scala 3 by lmm on Sunday April 22, 2018 @11:37AM (#16896598)
> I'm saying the key idea behind implicits is the realisation that default arguments merge two ideas that should be kept separate, if we want context dependent defaults.

This isn't true though - that's not what implicits are for and not how they're used. Indeed the clearest proof is that Scala still has default arguments, and they still have the behaviour given in your OCaml example. They're different things.

Re:Towards Scala 3 by YorkshireSeason on Wednesday April 25, 2018 @06:07AM (#16920087)
I did not say implicits and default arguments are the same thing. I said that implicits improve upon default arguments in various ways, the key realisation being the split between providing elided values in a context-dependent way, and permitting the elision of values.

I should not have said the key idea, but a key idea.

Default arguments are convenient if you don't need context dependence of elided arguments. I'd probably have remove defaults in order to have a smaller and more simple language.

Re:Towards Scala 3 by ionforce on Friday April 20, 2018 @05:08PM (#16888451)
Maybe a disagreement here is fueled by the power level difference in these two features. Beyond the superficial injection examples, Scala implicits also support injecting values constructed dynamically and also recursive/derived implicits.
Re:Towards Scala 3 by YorkshireSeason on Saturday April 21, 2018 @05:17AM (#16890656)
> injecting values constructed dynamically

You can also do this with default arguments, e.g. like so:

   let f x y = 
      let g (a = x+1) b = ...
      ...
For example the following is valid OCaml:

   let bump ?(step = 1) x =
     let waa ?( a = step+17 ) b = a * b in
     x + step + waa 666;;
Here the function waa has a default argument whose default argument depends on bump's default argument.

> recursive/derived implicits

Recursion is quite restricted, for example this is not valid Scala:

    def f ( x : Int ) ( implicit y : Int ) : Int = {
      implicit val a = 17
      f ( x+1 ) }
Re:Towards Scala 3 by lptk on Saturday April 21, 2018 @10:09AM (#16891476)
Your example of nested functions with default arguments does not correspond to what derived implicits do. As a result of implicit resolution, an expression with an arbitrary number of subexpressions may be synthesized, the shape of which depends on the types involves. Default parameters simply cannot do that.

The example you give to justify "recursion is quite restricted" is not a restriction on recursion at all, it's an ambiguity problem. Define `a` as `y` to shadow the function parameter, and it compiles.

Re:Towards Scala 3 by YorkshireSeason on Saturday April 21, 2018 @10:37AM (#16891568)
> does not correspond to what derived implicits do

I did not claim it did. The first example disproved ionforce's claim that default values cannot be constructed dynamically.

> Define `a` as `y` to shadow the function parameter,

Whoops, yes that's correct. I didn't realise this was possible.

Re:Towards Scala 3 by lptk on Saturday April 21, 2018 @10:58AM (#16891641)
> The first example disproved ionforce's claim that default values cannot be constructed dynamically.

But I don't think that is what they meant. I think they meant something along the lines of what I said above:

> an expression with an arbitrary number of subexpressions may be synthesized, the shape of which depends on the types involves

Re:Towards Scala 3 by YorkshireSeason on Wednesday April 25, 2018 @06:08AM (#16920097)
OK, maybe I misunderstood ionforce. I don't deny that type-driven synthesis is a core element of implicits.
Re:Towards Scala 3 by naasking on Friday April 20, 2018 @01:58PM (#16886978)
Thanks for the Simplicity reference, hadn't seen that yet. Seems like it builds on the implicit calculus [1], which applies more generally to any lambda calculus than in the Scala context.

[1] http://homepages.inf.ed.ac.uk/wadler/papers/implicits/implic...

Re:Towards Scala 3 by jowiar on Friday April 20, 2018 @11:14AM (#16885417)
Would love to see a citation re: Odersky (or if he'd chime in here).

My general assumption is that if Scala was intended to be category theory, implemented, something along the lines of scalaz or cats would have been built into the language -- the philosophy of the language does not include a JS-esque philosophy of small stdlib, big library ecosystem.

Re:Towards Scala 3 by yawaramin on Friday April 20, 2018 @10:14PM (#16889650)
See this talk https://youtu.be/P8jrvyxHodU

He mentions module systems fairly early on.

Re:Towards Scala 3 by lptk on Saturday April 21, 2018 @10:31AM (#16891554)
What does this have to do with the original assertion that "one of Odersky's motives in creating Scala was bringing the power of Haskell into the JVM world"?

AFAIK, Odersky doesn't particularly like people trying to replicate Haskell patterns in Scala. For example, he thinks using monads for most effects is inappropriate.

Re:Towards Scala 3 by yawaramin on Saturday April 21, 2018 @12:35PM (#16892070)
Threading fail. I thought I was replying to someone who had asked for a citation for:

> Odersky has long claimed ML as his primary functional programming influence, not Haskell.