Last active
April 16, 2023 15:19
-
-
Save kassane/53e3974726af96a804d4b78ac072dc0e to your computer and use it in GitHub Desktop.
Adapton programming model & Lazy evaluation - int values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: http://plum-umd.github.io/adapton/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct Adapton_Int { | |
int value; | |
int (*compute)(struct Adapton_Int *, struct Adapton_Int *); | |
struct Adapton_Int *arg1; | |
struct Adapton_Int *arg2; | |
} Adapton_Int; | |
int force(Adapton_Int *adapton_int) { | |
if (adapton_int->compute == NULL) { | |
return adapton_int->value; | |
} else { | |
adapton_int->value = | |
adapton_int->compute(adapton_int->arg1, adapton_int->arg2); | |
adapton_int->compute = NULL; | |
return adapton_int->value; | |
} | |
} | |
int add(Adapton_Int *a, Adapton_Int *b) { return force(a) + force(b); } | |
Adapton_Int *makeAdaptonInt(int value) { | |
Adapton_Int *adapton_int = (Adapton_Int *)malloc(sizeof(Adapton_Int)); | |
adapton_int->value = value; | |
adapton_int->compute = NULL; | |
adapton_int->arg1 = NULL; | |
adapton_int->arg2 = NULL; | |
return adapton_int; | |
} | |
Adapton_Int *adapt(int value) { | |
Adapton_Int *adapton_int = makeAdaptonInt(value); | |
adapton_int->compute = NULL; | |
return adapton_int; | |
} | |
Adapton_Int *makeCompute(Adapton_Int *a, Adapton_Int *b, | |
int (*computeFunc)(Adapton_Int *, Adapton_Int *)) { | |
Adapton_Int *adapton_int = makeAdaptonInt(0); | |
adapton_int->value = 0; | |
adapton_int->compute = computeFunc; | |
adapton_int->arg1 = a; | |
adapton_int->arg2 = b; | |
return adapton_int; | |
} | |
int main() { | |
Adapton_Int *x = adapt(1); | |
Adapton_Int *y = adapt(2); | |
Adapton_Int *z = makeCompute(x, y, &add); | |
printf("%d\n", force(z)); | |
x = adapt(5); | |
z = makeCompute(x, y, &add); | |
printf("%d\n", force(z)); | |
y = adapt(4); | |
z = makeCompute(x, y, &add); | |
printf("%d\n", force(z)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct LazyInt { | |
int value; | |
int (*func)(struct LazyInt *, struct LazyInt *); | |
struct LazyInt *arg1; | |
struct LazyInt *arg2; | |
} LazyInt; | |
int force(LazyInt *lazyInt) { | |
if (lazyInt->func == NULL) { | |
return lazyInt->value; | |
} else { | |
lazyInt->value = lazyInt->func(lazyInt->arg1, lazyInt->arg2); | |
lazyInt->func = NULL; | |
return lazyInt->value; | |
} | |
} | |
int add(LazyInt *a, LazyInt *b) { return force(a) + force(b); } | |
LazyInt lazy(int value) { | |
LazyInt lazyInt; | |
lazyInt.value = value; | |
lazyInt.func = NULL; | |
lazyInt.arg1 = NULL; | |
lazyInt.arg2 = NULL; | |
return lazyInt; | |
} | |
LazyInt compute(LazyInt a, LazyInt b, int (*func)(LazyInt *, LazyInt *)) { | |
LazyInt lazyInt; | |
lazyInt.value = 0; | |
lazyInt.func = func; | |
lazyInt.arg1 = &a; | |
lazyInt.arg2 = &b; | |
return lazyInt; | |
} | |
int main() { | |
LazyInt x = lazy(1); | |
LazyInt y = lazy(2); | |
LazyInt z = compute(x, y, &add); | |
printf("%d\n", force(&z)); | |
x = lazy(5); | |
z = compute(x, y, &add); | |
printf("%d\n", force(&z)); | |
y = lazy(4); | |
z = compute(x, y, &add); | |
printf("%d\n", force(&z)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment