kyledewey / typed-prolog

A basic type system on top of Prolog, along with higher-order clauses. Translates to normal Prolog.
26 stars 1 forks source link

Comparison predicates don't support arithmetic expressions #12

Closed maemre closed 8 years ago

maemre commented 8 years ago

Comparison predicates like >=/2 don't support arithmetic expressions, they only support numbers and variables. They should be treated like is/2, this can be done with infrastructure we have for arithmetic expressions. As an example, the following code fails with a syntax error:

module(test, [], []).

clausedef(test1, [], [int, int, int]).
test1(X, Y, Z) :-
    X >= Y + Z.

although Prolog parses it correctly. The error given is:

Type error at term public_2_term_constructor(+,[public_2_term_var(_G509),public_2_term_var(_G510)])
        Found: UNKNOWN
        Expected: public_2_intType

Type error at body public_2_firstOrderCall(>=,[public_2_term_var(_G508),public_2_term_constructor(+,[public_2_term_var(_G509),public_2_term_var(_G510)])])

Type error at clause public_2_clauseclause(private_0_test1,[public_2_term_var(_G508),public_2_term_var(_G509),public_2_term_var(_G510)],public_2_firstOrderCall(>=,[public_2_term_var(_G508),public_2_term_constructor(+,[public_2_term_var(_G509),public_2_term_var(_G510)])]))

On the other hand, following code compiles correctly, and this can be used as a workaround for now:

module(test, [], []).

clausedef(test1, [], [int, int, int]).
test1(X, Y, Z) :-
    T is Y + Z,
    X >= T.
kyledewey commented 8 years ago

This is relatively easy to fix, but it will be time-consuming because it involves changing our AST definition which is used throughout the entire compiler. I'm putting this on the back-burner for now because the workaround is acceptable, but this should be addressed in the future.

kyledewey commented 8 years ago

Added support with commit 3e9e6e1020b75cd0778bc2ae77976136cfdcb7d2.