Frege / frege-interpreter

Frege Interpreter
15 stars 7 forks source link

infix operators #1

Closed Ingo60 closed 11 years ago

Ingo60 commented 11 years ago

It looks like infix declarations are not honoured, nor imports of packages that have infix operators defined:

frege> import frege.control.Monoid
frege> :t `<>`
12: syntax error, the next definition cannot
start with token "`<>`"
frege> :t (<>)
12: syntax error on end of file
12: unexpected '}' while trying to parse an
annotated item
frege> Sum 0 <> Sum 1
12: syntax error on end of file
12: unexpected '}' while trying to parse
left hand side of a function or pattern
binding
frege>  
Ingo60 commented 11 years ago

The following works, however:

frege> :t (+)
Num α => α -> α -> α
frege> :t (<)
Ord α => α -> α -> Bool
frege>  
Ingo60 commented 11 years ago

As well as this:

frege> :t (`<>`)
Semigroup α => α -> α -> α
frege> (`<>`) (Sum 0) (Sum 1)
res4 = Sum 1
frege> Sum 0 `<>` Sum 1
res5 = Sum 1

This is, of course, not exactly a nice workaround, but it shows at least that the symbol <> is well known, just the scanner doesn't know.

mmhelloworld commented 11 years ago

Considering the following: 1) Sum 0 <> Sum 1 is not working but foo = Sum 0 <> Sum 1 is working 2) To parse <>, the imports must be in scope

I thought that instead of trying to directly parse an expression from the grammar, we could use the following template to check whether the script is an expression:

module SomeName where
<previous scripts>
someName = (<current script>)

On this template after applying the scripts, we will just invoke the lexer and parser and if both succeed, it is an expression otherwise it is either an invalid script or a set of declarations. It turned out to be working well:

frege> :t <>
8: syntax error, the next definition cannot
start with token "<"
frege> import frege.control.Monoid

frege> :t <>
Semigroup α => α -> α -> α
frege> :t +
Num α => α -> α -> α
frege> Sum 1 <> Sum 0
res5 = Sum 1

This fix is available in App Engine as well as frege-scripting-1.0.jar in the downloads page.

With this approach, the grammar changes which I made for parsing just expressions are obsolete.

Do you see any issues with this approach? If this is good, I will push the changes later today.

Ingo60 commented 11 years ago

Sounds amazingly simple, indeed. Is it necessary to write the current script in (...)? If not, this would allow inputs like 2+2 where 2+2=5 which would be quite cool.

mmhelloworld commented 11 years ago

Is it necessary to write the current script in (...)?

It is not necessary. I initially thought that the scripts like + can be compiled if we wrap it in parenthesis by default but it doesn't make sense since it is not actually an expression since foo = + is not valid. I have fixed this as well. Now inputs like 2+2 where 2+2=5 will be working.

Ingo60 commented 11 years ago

You're right, a lexical operator on it's own does not make a syntactically valid expression. Hence, I was in error when I tried:

:t `<>`

since only

:t (<>)

needs to work if the syntax is strictly :t expression