aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/reducer.h8
-rw-r--r--inc/term.h7
-rw-r--r--readme.md2
-rw-r--r--src/main.c1
-rw-r--r--src/parse.c36
-rw-r--r--src/reducer.c6
-rw-r--r--src/term.c4
7 files changed, 57 insertions, 7 deletions
diff --git a/inc/reducer.h b/inc/reducer.h
new file mode 100644
index 0000000..28fe8b1
--- /dev/null
+++ b/inc/reducer.h
@@ -0,0 +1,8 @@
+#ifndef REDUCER_H
+#define REDUCER_H
+
+#include <term.h>
+
+struct term *reduce(struct term *term);
+
+#endif
diff --git a/inc/term.h b/inc/term.h
index e750beb..9c7dbc9 100644
--- a/inc/term.h
+++ b/inc/term.h
@@ -1,20 +1,23 @@
#ifndef TERM_H
#define TERM_H
-typedef int bruijn;
typedef enum { INV, ABS, APP, VAR } term_type;
struct term {
term_type type;
union {
struct {
+ int name;
struct term *term;
} abs;
struct {
struct term *lhs;
struct term *rhs;
} app;
- bruijn var;
+ struct {
+ int name;
+ enum { BRUIJN_INDEX, BARENDREGT_VARIABLE } type;
+ } var;
} u;
};
diff --git a/readme.md b/readme.md
index 6a9d5a1..56d2d99 100644
--- a/readme.md
+++ b/readme.md
@@ -12,7 +12,7 @@
## Research
-In no particular order:
+Interesting/relevant research in no particular order:
0. Biernacka, M., Charatonik, W., & Drab, T. (2022). A simple and
efficient implementation of strong call by need by an abstract
diff --git a/src/main.c b/src/main.c
index 024cc7a..7ff90d2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,6 +7,7 @@ int main(void)
{
struct term *term = parse("([[((0 1) [(1 0)])]] [0])");
print_term(term);
+ printf("\n");
free_term(term);
return 0;
}
diff --git a/src/parse.c b/src/parse.c
index 495f55f..306bf4f 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -6,6 +6,12 @@
#include <parse.h>
#include <term.h>
+static int name_generator(void)
+{
+ static int current = 0x424242; // TODO: idk?
+ return current++;
+}
+
static struct term *rec(const char **term)
{
struct term *res = 0;
@@ -22,7 +28,8 @@ static struct term *rec(const char **term)
res->u.app.rhs = rec(term);
} else if (**term >= '0' && **term <= '9') {
res = new_term(VAR);
- res->u.var = **term - '0';
+ res->u.var.name = **term - '0';
+ res->u.var.type = BRUIJN_INDEX;
(*term)++;
} else {
(*term)++;
@@ -31,7 +38,32 @@ static struct term *rec(const char **term)
return res;
}
+// TODO: WARNING: This might not be 100% correct! Verify!
+static void to_barendregt(struct term *term, int level, int replacement)
+{
+ switch (term->type) {
+ case ABS:
+ term->u.abs.name = name_generator();
+ to_barendregt(term->u.abs.term, level + 1, term->u.abs.name);
+ break;
+ case APP:
+ to_barendregt(term->u.app.lhs, level, replacement);
+ to_barendregt(term->u.app.rhs, level, replacement);
+ break;
+ case VAR:
+ if (term->u.var.type == BRUIJN_INDEX) {
+ term->u.var.name = replacement - term->u.var.name;
+ term->u.var.type = BARENDREGT_VARIABLE;
+ }
+ break;
+ default:
+ fprintf(stderr, "Invalid type %d\n", term->type);
+ }
+}
+
struct term *parse(const char *term)
{
- return rec(&term);
+ struct term *parsed = rec(&term);
+ to_barendregt(parsed, -1, -1);
+ return parsed;
}
diff --git a/src/reducer.c b/src/reducer.c
new file mode 100644
index 0000000..e74946b
--- /dev/null
+++ b/src/reducer.c
@@ -0,0 +1,6 @@
+#include <reducer.h>
+
+struct term *reduce(struct term *term)
+{
+ return term;
+}
diff --git a/src/term.c b/src/term.c
index 746c6f7..d13ec9f 100644
--- a/src/term.c
+++ b/src/term.c
@@ -38,7 +38,7 @@ void print_term(struct term *term)
{
switch (term->type) {
case ABS:
- printf("[");
+ printf("[{%d} ", term->u.abs.name);
print_term(term->u.abs.term);
printf("]");
break;
@@ -50,7 +50,7 @@ void print_term(struct term *term)
printf(")");
break;
case VAR:
- printf("%d", term->u.var);
+ printf("%d", term->u.var.name);
break;
default:
fprintf(stderr, "Invalid type %d\n", term->type);