Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

() -> a = a #50

Closed
zaoqi opened this issue Jul 11, 2017 · 2 comments
Closed

() -> a = a #50

zaoqi opened this issue Jul 11, 2017 · 2 comments

Comments

@zaoqi
Copy link

zaoqi commented Jul 11, 2017

() -> a = a
Haskell:

main :: IO ()

Koka:

main : () -> io ()
@dhil
Copy link
Collaborator

dhil commented Jul 11, 2017

I am not sure whether you are asking an question or stating a falsehood. I shall assume the former.

Koka is a strict functional language with n-ary functions. By contrast Haskell is a lazy functional language with unary functions. It is possible to simulate n-ary functions (to some extent) in Haskell by using tuples.

The function type () -> a denotes a nullary function (often called a thunk) which returns an element of type a. In Haskell the type a denote a value of type. This value may be unevaluated, however, due to the lazy evaluation strategy. Furthermore, note that the Koka type () -> a is not the same as the Haskell type () -> a. The two types describe functions of different arities: the former describes a nullary function, while the latter describes an unary function. Formally,

[[-]] : Haskell type -> Koka type
[[() -> a]] = (unit) -> [[a]]

In Koka the function type (unit) -> a describes a function which accepts a single argument, namely, the unit argument, while the type () -> a describes a function that accepts no arguments.

@Pauan
Copy link

Pauan commented Jul 11, 2017

@zaoqi Haskell programs are lazy, which means values are only evaluated when they are needed. In addition, Haskell programs do not have side effects, so side effects are represented with the IO type. The IO type is only evaluated when needed.

Koka programs are not lazy, so everything is evaluated immediately. In addition, Koka programs do have side effects, and Koka does not have the IO type. Therefore in Koka the only way to trigger side effects is to call a function. And the only way to delay evaluation in Koka is to use a function.

In other words, Haskell programs are automatically delayed, but Koka programs must use functions to delay evaluation.

It is possible to create an IO type for Koka:

alias io'<a> = () -> io a

And then you can create io' types:

val my-print: io'<()> = fun() { println("Hi!") }

val main: io'<()> = my-print

This is the same as this Haskell program:

myPrint :: IO ()
myPrint = putStrLn "Hi!"

main :: IO ()
main = myPrint

As you can see, a () -> io a in Koka is exactly the same as an IO a in Haskell. The only difference is that we have to wrap things in fun() { ... } to delay evaluation.

Koka could wrap everything in fun() { ... }, that would give the same behavior as Haskell, but that is inefficient and unnecessary, which is why Koka doesn't do that.

@zaoqi zaoqi closed this as completed Jul 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants