Skip to content

Instantly share code, notes, and snippets.

@thelissimus
Created April 15, 2024 12:38
Show Gist options
  • Save thelissimus/00304902ac7973e18027f59c49fb7e08 to your computer and use it in GitHub Desktop.
Save thelissimus/00304902ac7973e18027f59c49fb7e08 to your computer and use it in GitHub Desktop.
LInt implemented in Prolog.
exp(E) --> int(E).
exp(E) --> binop(E).
int(I) --> [I], { integer(I) }.
binop(add(A, B)) --> "+", exp(A), exp(B).
binop(sub(A, B)) --> "-", exp(A), exp(B).
binop(mul(A, B)) --> "*", exp(A), exp(B).
binop(div(A, B)) --> "/", exp(A), exp(B).
eval(int(I), I).
eval(add(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal + BVal.
eval(sub(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal - BVal.
eval(mul(A, B), Result) :- eval(A, AVal), eval(B, BVal), Result is AVal * BVal.
eval(div(A, B), Result) :- eval(A, AVal), eval(B, BVal), BVal =\= 0, Result is AVal / BVal.
@priller-dev
Copy link

dot feels more logical than semicolons for ending the line🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment