gluon icon indicating copy to clipboard operation
gluon copied to clipboard

`type` in REPL error?

Open fduxiao opened this issue 7 years ago • 6 comments

> type List a = | Cons a (List a) | Nil
<line>:Line: 1, Column: 38: Unexpected token: CloseBlock
Expected one of  (, [, \\\\, block open, byte literal, char literal, do, documentation comment, float literal, identifier, if, int literal, let, match, string literal, type or {
type List a = | Cons a (List a) | Nil
                                     ^
<line>:Line: 1, Column: 38: Unexpected token: CloseBlock
type List a = | Cons a (List a) | Nil
                                     ^

while in haskell

ghci> data List a = Cons a (List a) | Nil deriving (Show)
ghci> Cons 0 $ Cons 1 $ Cons 2 $ Nil  -- Will you also introduce the `$` so there won't be too many `)`s
Cons 0 (Cons 1 (Cons 2 Nil))

gluon version:

$ gluon --version
gluon 0.7.1
commit: c3316fbe89fea8f1958bc88fd714eb360b9ecc1e

fduxiao avatar Mar 28 '18 16:03 fduxiao

/me would vote for <| instead of $

OvermindDL1 avatar Mar 28 '18 17:03 OvermindDL1

@fduxiao This will require some refactoring in the repl and vm as let bindings are handled as a special case right now. That will need to be extended to work with ``type (and perhaps do bindings) as well. I definitely want it though!

@OvermindDL1

https://github.com/gluon-lang/gluon/blob/c3316fbe89fea8f1958bc88fd714eb360b9ecc1e/std/function.glu#L44

:)

Marwes avatar Mar 28 '18 20:03 Marwes

:)

Hehe, as long as the precedence is correct. ^.^

Speaking of, when defining an operator why not define it with a precedence inline like you can in some languages, that way when people define an operator they also define the precedence?

OvermindDL1 avatar Mar 28 '18 20:03 OvermindDL1

Currently they are just hardcoded https://github.com/gluon-lang/gluon/blob/014b180cce1c7581079e05a5227ec90f197d0578/parser/src/infix.rs#L113 but that will be needed at some point (It is a really old issue actually https://github.com/gluon-lang/gluon/issues/3 ...).

I should probably take on implementing it as an attribute (/// @infix left N) pretty soon, shouldn't take very long I think.

Marwes avatar Mar 28 '18 20:03 Marwes

I'm interested in working on this (making type expressions work in the repl). Can someone give me some pointers on how to do it? I've done a bit of digging, and it looks like let is handled here, so I guess that's where type should be handled too. But I'm not familiar with the codebase, so any tips would help.

mikeyhew avatar Jun 19 '18 17:06 mikeyhew

@mikeyhew If you have a stomach for some hacks then, awesome!

It is probably best to start by getting let { Type } = expr working as that already works in the parser and typechecker. In the set_globals function you should be able to use register_type_as to register the type globally and it should be available from the repl.

If that works, then you can do the same thing for type by hacking the the parser in the same way as for let so you can get a TypeBinding out. https://github.com/gluon-lang/gluon/blob/eff8bc287623449e5cbeeed49d054a4b7a55b7bb/parser/src/grammar.lalrpop#L674-L676 .

Since only expressions can be typechecked you can construct an expression as Expr::TypeBinding(vec![binding], Expr::Tuple { elems: vec![] }) and feed that to the typechecker. Then you unpack it to recover the checked alias from the finalized_alias field of TypeBinding

https://github.com/gluon-lang/gluon/blob/eff8bc287623449e5cbeeed49d054a4b7a55b7bb/base/src/ast.rs#L365

Then you should again be able to use register_type_as to make it available.

Marwes avatar Jun 19 '18 20:06 Marwes