ohua-dev / ohua-core

Core Haskell library for the compiler
https://ohua-dev.github.io
Eclipse Public License 1.0
5 stars 0 forks source link

[Feature] Add tuple operator #6

Closed Feliix42 closed 5 years ago

sertel commented 5 years ago

If we add a (,) operator/fn then we should also add fst and snd.

JustusAdam commented 5 years ago

I am not sure about that. (,) works different than the equivalent in haskell. It is (currently) multiple arity and creates any kind of tuple. Aka (,) a b c creates (a,b,c).

Maybe we shouldn't add something like fst but instead a static indexer like ohua.lang/0 to get the 0'th element ohua.lang/1 for the first element. These would not be available to the user, but created by the compiler when desugaring destructuring.

sertel commented 5 years ago

Where do we need a multi-arity (,) in the compiler?

JustusAdam commented 5 years ago

Well, its what the parser creates anytime a user writes (a, b, c). In particular this is necessary if a user writes an algo that returns multiple values.

For instance

let res = smap (\ a -> (sf1 a, sf2 a, sf3 a)) coll
in res2 = smap (\ (a, b, c) -> ... ) res
in ...

In this case the first lambda desugars to

\a -> 
let a0 = sf1 a in
let a1 = sf2 a in
let a2 = sf3 a
in (,) a0 a1 a2
JustusAdam commented 5 years ago

If you want I can change the the parser to create (,), (,,), (,,,) etc. It may in fact be better to do that ... not sure.

sertel commented 5 years ago

This is not quite what I meant. As you point out yourself, this is "use"-code. As such, normally the user would have to provide the according stateful functions.

With the above, it seems that we added support for tuples to our algo language or at least to our standard library. Which means, we force each integration to implement these stateful functions.

It seems not such a big deal though.

But if we do then I would like to clarify the relationship between ohua.lang/scope ohua.lang/array and the various incarnations of ohua.lang/(,)

Please note that a multi-arity tuple might not be as trivially implementable as it seems in a statically typed language. For example, it works in Rust only because of Rust macro system. I guess for a language such as Go, this could be a problem.

(Go has var arg support though: https://stackoverflow.com/questions/19238143/does-golang-support-variadic-function#19238249)

sertel commented 5 years ago

I implemented our internal version of nth in the Rust backend. I don't think we need anything that builds a tuple for now in the compiler.