Skip to content

Commit

Permalink
Merge pull request #56 from billhails/arithmetic
Browse files Browse the repository at this point in the history
Arithmetic
  • Loading branch information
billhails authored Apr 7, 2024
2 parents e9ea364 + 0f62148 commit 9e0e1d9
Show file tree
Hide file tree
Showing 37 changed files with 984 additions and 515 deletions.
31 changes: 31 additions & 0 deletions docs/ArithmeticTypes.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<mxfile host="Electron" modified="2024-04-05T17:29:34.512Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/21.6.5 Chrome/114.0.5735.243 Electron/25.3.1 Safari/537.36" etag="euueRJ_i7iDJl8OJ7Vv5" version="21.6.5" type="device">
<diagram name="Page-1" id="BLDVzQ1TnQkECbLpREf5">
<mxGraphModel dx="1866" dy="1173" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="0" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="yBlL6VTiBweEXrjM-FRY-26" value="quadratic" style="rounded=0;whiteSpace=wrap;html=1;verticalAlign=top;" vertex="1" parent="1">
<mxGeometry x="200" width="520" height="320" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-7" value="complex" style="rounded=0;whiteSpace=wrap;html=1;verticalAlign=top;" vertex="1" parent="1">
<mxGeometry x="220" y="40" width="340" height="260" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-17" value="rational" style="rounded=0;whiteSpace=wrap;html=1;verticalAlign=top;" vertex="1" parent="1">
<mxGeometry x="240" y="80" width="160" height="200" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-4" value="irrational" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="420" y="140" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-14" value="bigInt" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="260" y="120" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-15" value="smallInt" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="260" y="200" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="yBlL6VTiBweEXrjM-FRY-27" value="symbol" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="580" y="140" width="120" height="60" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
2 changes: 2 additions & 0 deletions docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* NaN.
* Improve arithmetic.
* General overhaul.
* move to [libgmp](https://gmplib.org/)
* Pre-compute constant values at compile time.
* allow numeric (not symbolic) arithmetic expressions in patterns.
* allow unpacking of numerator and denominator variables in patterns.
Expand All @@ -31,3 +32,4 @@
* User definable infix operators.
* With precedence and associativity.
* Curried binary operators `(2+)` etc.
* Allow trailing semicolons ffs.
16 changes: 16 additions & 0 deletions fn/arithmetic.fn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
print(2 + 2);
print(6 - 2);
print(8 / 2);
print(2 * 2);
print(16 % 12);
print(2 ** 2);
print(1 + 4294967295);
print(4294967295 + 4294967295);
print(1234567890123456789 * 2);
print(1234567890123456789 ** 200);
print(4294967295555 % 4294967295555);
print(4294967295555 - 429496729555);
print(-429496729555);
print(-429496729555 / 2);
print(4294967295555 % 429);
print(10 - 1);
1 change: 1 addition & 0 deletions fn/googol.fn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print(10 ** 100)
1 change: 1 addition & 0 deletions fn/irrational.fn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print(1.5)
20 changes: 12 additions & 8 deletions fn/rational.fn
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ print(#( "1 * 2", 1 * 2 ));
print(#( "1 + 2", 1 + 2 ));
print(#( "2 / 3 + 4", 2 / 3 + 4 ));
print(#( "4 + 2 / 3 + 4", 4 + 2 / 3 + 4 ));
print(#( "4 + 2 / -3 + 4", 4 + 2 / -3 + 4 ));
print(#( "4 + 4 / -6 + 4", 4 + 4 / - 6 + 4 ));
print(#( "2 / 3 + 4 / 5", 2 / 3 + 4 / 5 ));
print(#( "(2 / 3) * (4 / 5)", (2 / 3) * (4 / 5) ));
print(#( "(6 / 5)", (6 / 5) ));
print(#( "(6 / 5)", (6 / 5) ));
print(#( "(2 / 3) / (6 / 5)", (2 / 3) / (6 / 5) ));
print(#( "(2 / 3) % (4 / 5)", (2 / 3) % (4 / 5) ));
print(#( "1914882942 ** 10", 1914882942 ** 10 ));
print(#( "1/3 % 8", 1/3 % 8 ));
print(#( "-9 % 8", -9 % 8 ));
print(#( "(1/2) ** 2", (1/2) ** 2 ));
print(#( "(1914882942 ** 5 / 5) % (2 / 3)", (1914882942 ** 5 / 5) % (2 / 3) ))

print(#( "1/3 % 8", 1/3 % 8 ));
print(#( "-9 % 8", -9 % 8 ));
print(#( "(2/3) ** 2", (2/3) ** 2 ));
print(#( "2 / 3 ** 2", 2 / 3 ** 2 ));
print(#( "1 / 2 < 1 / 3", 1/2 < 1/3 ));
print(#( "1 / 2 > 1 / 3", 1/2 > 1/3 ));
print(#( "1 / 2 < 1", 1/2 < 1 ));
print(#( "1 / 2 > 1", 1/2 > 1 ));
print(#( "1 / 2 <=> 1 / 3", 1/2 <=> 1/3 ));
print(#( "1 / 2 <=> 1", 1/2 <=> 1 ));
4 changes: 2 additions & 2 deletions src/anf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ structs:
cases: CexpCondCases

CexpIntCondCases:
option: BigInt
option: MaybeBigInt
body: Exp
next: CexpIntCondCases

Expand Down Expand Up @@ -141,7 +141,7 @@ unions:
lam: AexpLam
var: HashSymbol
annotatedVar: AexpAnnotatedVar
biginteger: BigInt
biginteger: MaybeBigInt
littleinteger: int
character: char
prim: AexpPrimApp
Expand Down
29 changes: 15 additions & 14 deletions src/anf_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# include <unistd.h>
# include "debug.h"
# include "lambda_pp.h"
# include "anf_debug.h"
# include "debugging_on.h"
#else
# include "debugging_off.h"
Expand All @@ -37,7 +38,7 @@
static Exp *normalize(LamExp *lamExp, Exp *tail);
static Exp *normalizeLam(LamLam *lamLam, Exp *tail);
static Exp *normalizeVar(HashSymbol *var, Exp *tail);
static Exp *normalizeBigInteger(BigInt *integer, Exp *tail);
static Exp *normalizeMaybeBigInteger(MaybeBigInt *integer, Exp *tail);
static Exp *normalizeStdInteger(int integer, Exp *tail);
static Exp *normalizeCharacter(char character, Exp *tail);
static Exp *normalizeUnary(LamUnaryApp *app, Exp *tail);
Expand All @@ -56,7 +57,7 @@ static AexpUnaryOp mapUnaryOp(LamUnaryOp op);
static Exp *letBind(Exp *body, LamExpTable *replacements);
static AexpPrimOp mapPrimOp(LamPrimOp op);
static Aexp *aexpNormalizeVar(HashSymbol *var);
static Aexp *aexpNormalizeBigInteger(BigInt *integer);
static Aexp *aexpNormalizeMaybeBigInteger(MaybeBigInt *integer);
static Aexp *aexpNormalizeStdInteger(int integer);
static Aexp *aexpNormalizeCharacter(char character);
static Aexp *aexpNormalizeLam(LamLam *lamLam);
Expand Down Expand Up @@ -107,7 +108,7 @@ static Exp *normalize(LamExp *lamExp, Exp *tail) {
case LAMEXP_TYPE_STDINT:
return normalizeStdInteger(lamExp->val.stdint, tail);
case LAMEXP_TYPE_BIGINTEGER:
return normalizeBigInteger(lamExp->val.biginteger, tail);
return normalizeMaybeBigInteger(lamExp->val.biginteger, tail);
case LAMEXP_TYPE_PRIM:
return normalizePrim(lamExp->val.prim, tail);
case LAMEXP_TYPE_UNARY:
Expand Down Expand Up @@ -651,17 +652,17 @@ static Exp *normalizeCharacter(char character, Exp *tail) {
return exp;
}

static Exp *normalizeBigInteger(BigInt *integer, Exp *tail) {
ENTER(normalizeBigInteger);
static Exp *normalizeMaybeBigInteger(MaybeBigInt *integer, Exp *tail) {
ENTER(normalizeMaybeBigInteger);
if (tail != NULL) {
LEAVE(normalizeBigInteger);
LEAVE(normalizeMaybeBigInteger);
return tail;
}
Aexp *aexp = aexpNormalizeBigInteger(integer);
Aexp *aexp = aexpNormalizeMaybeBigInteger(integer);
int save = PROTECT(aexp);
Exp *exp = wrapAexp(aexp);
UNPROTECT(save);
LEAVE(normalizeBigInteger);
LEAVE(normalizeMaybeBigInteger);
return exp;
}

Expand Down Expand Up @@ -750,7 +751,7 @@ static Exp *normalizeApply(LamApply *lamApply, Exp *tail) {
AexpList *args = replaceLamList(lamApply->args, replacements);
PROTECT(args);
DEBUG("back from replaceLamList");
IFDEBUG(printHashTable(replacements, 0));
IFDEBUG(printLamExpTable(replacements, 0));
CexpApply *cexpApply = newCexpApply(function, countAexpList(args), args);
UNPROTECT(save2);
save2 = PROTECT(cexpApply);
Expand All @@ -770,8 +771,8 @@ static Exp *normalizeApply(LamApply *lamApply, Exp *tail) {
static Exp *letBind(Exp *body, LamExpTable *replacements) {
ENTER(letBind);
// DEBUG("sleep %d", sleep(1));
IFDEBUG(printExp(body));
IFDEBUG(printHashTable(replacements, 0));
IFDEBUG(printExp(body, 0));
IFDEBUG(printLamExpTable(replacements, 0));
if (countLamExpTable(replacements) == 0) {
LEAVE(letBind);
return body;
Expand Down Expand Up @@ -800,7 +801,7 @@ static Aexp *aexpNormalizeVar(HashSymbol *var) {
return newAexp(AEXP_TYPE_VAR, AEXP_VAL_VAR(var));
}

static Aexp *aexpNormalizeBigInteger(BigInt *integer) {
static Aexp *aexpNormalizeMaybeBigInteger(MaybeBigInt *integer) {
return newAexp(AEXP_TYPE_BIGINTEGER, AEXP_VAL_BIGINTEGER(integer));
}

Expand Down Expand Up @@ -901,7 +902,7 @@ static Aexp *replaceLamExp(LamExp *lamExp, LamExpTable *replacements) {
res = aexpNormalizeVar(lamExp->val.var);
break;
case LAMEXP_TYPE_BIGINTEGER:
res = aexpNormalizeBigInteger(lamExp->val.biginteger);
res = aexpNormalizeMaybeBigInteger(lamExp->val.biginteger);
break;
case LAMEXP_TYPE_STDINT:
res = aexpNormalizeStdInteger(lamExp->val.stdint);
Expand Down Expand Up @@ -1181,7 +1182,7 @@ static Aexp *replaceLamCexp(LamExp *apply, LamExpTable *replacements) {
}
HashSymbol *subst = freshSymbol();
setLamExpTable(replacements, subst, apply);
IFDEBUG(printHashTable(replacements, 0));
IFDEBUG(printLamExpTable(replacements, 0));
LEAVE(replaceLamCexp);
return newAexp(AEXP_TYPE_VAR, AEXP_VAL_VAR(subst));
}
10 changes: 8 additions & 2 deletions src/anf_pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ void ppAexpPrimApp(AexpPrimApp *x) {
case AEXPPRIMOP_TYPE_CMP:
eprintf("cmp ");
break;
case AEXPPRIMOP_TYPE_POW:
eprintf("pow ");
break;
default:
cant_happen("unrecognized op %s", aexpPrimOpName(x->type));
}
Expand All @@ -132,6 +135,9 @@ void ppAexpPrimApp(AexpPrimApp *x) {
void ppAexpUnaryApp(AexpUnaryApp *x) {
eprintf("(");
switch (x->type) {
case AEXPUNARYOP_TYPE_NEG:
eprintf("neg ");
break;
case AEXPUNARYOP_TYPE_NOT:
eprintf("not ");
break;
Expand Down Expand Up @@ -234,7 +240,7 @@ void ppCexpCond(CexpCond *x) {
void ppCexpIntCondCases(CexpIntCondCases *x) {
while (x != NULL) {
eprintf("(");
fprintBigInt(errout, x->option);
fprintMaybeBigInt(errout, x->option);
eprintf(" ");
ppExp(x->body);
eprintf(")");
Expand Down Expand Up @@ -372,7 +378,7 @@ void ppAexp(Aexp *x) {
eprintf("nil");
break;
case AEXP_TYPE_BIGINTEGER:
fprintBigInt(errout, x->val.biginteger);
fprintMaybeBigInt(errout, x->val.biginteger);
break;
case AEXP_TYPE_LITTLEINTEGER:
eprintf("%d", x->val.littleinteger);
Expand Down
Loading

0 comments on commit 9e0e1d9

Please sign in to comment.