diff options
| author | Tom Harley | 2020-01-08 14:14:39 +0000 |
|---|---|---|
| committer | Tom Harley | 2020-01-08 14:14:39 +0000 |
| commit | 4cdc57ce8041aecabd3175f2dff176b115f17724 (patch) | |
| tree | da9a25e2d18ce5f4415d4c61441962e9e06ee727 /examples/nq | |
| parent | checkpoint (diff) | |
| download | sinter-4cdc57ce8041aecabd3175f2dff176b115f17724.tar.gz sinter-4cdc57ce8041aecabd3175f2dff176b115f17724.zip | |
I should commit more
Diffstat (limited to 'examples/nq')
| -rw-r--r-- | examples/nq/Makefile | 27 | ||||
| -rw-r--r-- | examples/nq/closures.sin | 47 | ||||
| -rw-r--r-- | examples/nq/either.sin | 37 | ||||
| -rw-r--r-- | examples/nq/glue.c | 44 | ||||
| -rw-r--r-- | examples/nq/list.sin | 94 | ||||
| -rw-r--r-- | examples/nq/nqueens.sin | 83 | ||||
| -rw-r--r-- | examples/nq/repeat.h | 70 | ||||
| -rw-r--r-- | examples/nq/run_func.c | 79 | ||||
| -rw-r--r-- | examples/nq/util.sin | 15 |
9 files changed, 496 insertions, 0 deletions
diff --git a/examples/nq/Makefile b/examples/nq/Makefile new file mode 100644 index 0000000..a2ad100 --- /dev/null +++ b/examples/nq/Makefile @@ -0,0 +1,27 @@ +CC = clang +CXX = clang++ +SINC = ../../sinter + +DEBUG = -g -O3 + +CFLAGS = $(DEBUG) -Werror -Wpedantic -Wall -I$(HOME)/usr/include +LDFLAGS = $(DEBUG) -L$(HOME)/usr/lib +LDLIBS = -lgc + +LLCFLAGS = $(DEBUG) -Werror -Wpedantic -Wall +SINCFLAGS = -v + +TRGT = nqueens + +include ../../common.mak + +.PHONY: default all clean + +default: all + +all: $(TRGT) + +$(TRGT): nqueens.o util.o either.o list.o closures.o run_func.o glue.o + +clean: + $(RM) *.ll *.dot *.svg *.o $(TRGT) diff --git a/examples/nq/closures.sin b/examples/nq/closures.sin new file mode 100644 index 0000000..2e23daf --- /dev/null +++ b/examples/nq/closures.sin @@ -0,0 +1,47 @@ +[type closure_ [func arity arg_c args]] + +[dec = [x y]] + +[dec left [x]] +[dec right [x]] +[dec is_right [x]] +[dec right.val [x]] + +[dec run_func [f a as]] + +[dec cons [x xs]] +[dec append [xs x]] +[dec nil []] +[dec head [xs]] +[dec tail [xs]] +[dec is_nil [xs]] + +[def add_arg [c a] + [closure_ [closure_.func c] + [closure_.arity c] + [+ [closure_.arg_c c] 1] + [append [closure_.args c] a]]] + +[def run_closure [h] + [run_func [closure_.func h] [closure_.arity h] [closure_.args h]]] + +[def maybe_run_closure [h] + [if [= [closure_.arity h] [closure_.arg_c h]] + [left [run_func [closure_.func h] [closure_.arity h] [closure_.args h]]] + [right h]]] + +[def closure [f v] + [if v + [right [closure_ f v 0 [nil]]] + [left [run_func f 0 [nil]]]]] + +[def !> [c a] + [if [is_right c] + [maybe_run_closure [add_arg [right.val c] a]] + c]] + +[def !>> [c as] + [if [is_nil as] + c + [!>> [!> c [head as]] + [tail as]]]] diff --git a/examples/nq/either.sin b/examples/nq/either.sin new file mode 100644 index 0000000..75dc1a0 --- /dev/null +++ b/examples/nq/either.sin @@ -0,0 +1,37 @@ +[type left_ [kind val]] +[type right_ [kind val]] + +[dec inter_assert [a b]] + +[def left [val] + [left_ 0 val]] + +[def right [val] + [right_ 1 val]] + +[def _left.val [x] + [left_.val x]] + +[dec is_left [x]] + +[def left.val [x] + [inter_assert [is_left x] [left_.val x]]] + +[def right.val [x] + [right_.val x]] + +[def is_right [x] + [right_.kind x]] + +[def is_left [x] + [if [is_right x] + 0 + 1]] + +[def Either.>>= [a f] + [if [is_right a] + [f [right.val a]] + a]] + +[def Either.return [x] + [right x]] diff --git a/examples/nq/glue.c b/examples/nq/glue.c new file mode 100644 index 0000000..5d8c6f0 --- /dev/null +++ b/examples/nq/glue.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include <gc.h> + +void *count_solns(void *n); + +void *gc_alloc(int size) { + void *p = GC_MALLOC(size); + + /* + * To get info about the GC discarding objects, use + * GC_REGISTER_FINALIZER(p, fn, arg, 0, 0); + * + * This runs fn(p, arg) when the object at p is finalised. + * + * More info in gc.h of Boehm garbage collector. + */ + + assert(p); + return p; +} + +int main(int argc, char **argv) { + + /* + * Initialising the GC is not necessary on all systems but may be required + * on some. Better safe than sorry! See gc.h for more info. + */ + GC_INIT(); + + if (argc != 2) { + printf("usage: %s n\n", argv[0]); + + } else { + int n = atoi(argv[1]); + int n_solns = *((int *) count_solns(&n)); + printf("%i solutions found\n", n_solns); + + } + + return 0; +} diff --git a/examples/nq/list.sin b/examples/nq/list.sin new file mode 100644 index 0000000..87c2509 --- /dev/null +++ b/examples/nq/list.sin @@ -0,0 +1,94 @@ +[type list_ [kind head tail]] +[type nil_ [kind]] + +[dec = [x y]] + +[dec !> [c a]] + +[def cons [head tail] + [list_ 0 head tail]] + +[def nil [] + [nil_ 1]] + +[def head [xs] + [list_.head xs]] + +[def tail [xs] + [list_.tail xs]] + +[def has_tail [xs] + [nil_.kind xs]] + +[def is_nil [xs] + [if [nil_.kind xs] + 1 + 0]] + +[def _length [xs n] + [if [is_nil xs] + n + [_length [tail xs] [+ n 1]]]] + +[def length [xs] + [_length xs 0]] + +[def concat [as bs] + [if [is_nil as] + bs + [cons [head as] [concat [tail as] bs]]]] + +[def alt_concat [as bs] + [cons [head as] + [if [is_nil [tail as]] + bs + [concat [tail as] bs]]]] + +[def foldr [f acc xs] + [if [is_nil xs] + acc + [f [head xs] [foldr f acc [tail xs]]]]] + +[def foldl [f acc xs] + [if [is_nil xs] + acc + [foldl f [f acc [head xs]] [tail xs]]]] + +[def . [g f x] + [g [f x]]] + +[def flatten [xs] + [if [is_nil xs] + [nil] + [concat [head xs] [flatten [tail xs]]]]] + +[def .. [x y] + [if [= x y] + [cons x [nil]] + [cons x [.. [+ x 1] y]]]] + +[def append [xs x] + [if [is_nil xs] + [cons x [nil]] + [cons [head xs] [append [tail xs] x]]]] + +[def map [f xs] + [if [is_nil xs] + [nil] + [cons [f [head xs]] + [map f [tail xs]]]]] + +[def pure [x] + [cons x [nil]]] + +[def empty [] + [nil]] + +[def return [x] + [cons x [nil]]] + +[def !map [c xs] + [if [is_nil xs] + xs + [cons [!> c [head xs]] + [!map c [tail xs]]]]] diff --git a/examples/nq/nqueens.sin b/examples/nq/nqueens.sin new file mode 100644 index 0000000..6839dc0 --- /dev/null +++ b/examples/nq/nqueens.sin @@ -0,0 +1,83 @@ +[dec != [x y]] +[dec and [x y]] + +[dec cons [x xs]] +[dec head [xs]] +[dec tail [xs]] +[dec nil []] +[dec is_nil [xs]] +[dec flatten [xs]] +[dec .. [x y]] +[dec length [xs]] +[dec map [f xs]] +[dec foldl [f acc xs]] +[dec concat [xs ys]] + +[dec left.val [x]] + +[dec pure [x]] +[dec empty []] +[dec return [x]] + +[dec !map [c as]] +[dec !> [c a]] +[dec closure [f a]] + +[def safe [x d qs] + [if [is_nil qs] + 1 + [and [and [!= x [head qs]] + [!= x [+ [head qs] d]]] + [and [!= x [- [head qs] d]] + [safe x [+ d 1] [tail qs]]]]]] + +[def guard [v] + [if v + [pure 0] + [empty]]] + +[def !. [g f x] + [!> g [f x]]] + +[def .! [g f x] + [g [left.val [!> f x]]]] + +[def !.! [g f x] + [!> g [!> f x]]] + +[def !foldl [c acc xs] + [if [is_nil xs] + acc + [!foldl c [left.val [!> [!> c acc] [head xs]]] [tail xs]]]] + +[def !concatMapClosure [c acc x] + [concat acc [left.val [!> c x]]]] + +[def !concatMap [c xs] + [!foldl [!> [closure !concatMapClosure 3] c] [nil] xs]] + +[def _!>>= [xs c] + [flatten [map left.val [!map c xs]]]] + +[def !>>= [xs c] + [!concatMap c xs]] + +[def gen_1_1_1 [q b _] + [return [cons q b]]] + +[def gen_1_1 [b q] + [!>>= [guard [safe q 1 b]] + [!> [!> [closure gen_1_1_1 3] q] b]]] + +[def gen_1 [nq b] + [!>>= [.. 1 nq] + [!> [closure gen_1_1 2] b]]] + +[def gen [n nq] + [if n + [!>>= [gen [- n 1] nq] + [!> [closure gen_1 2] nq]] + [cons [nil] [nil]]]] + +[def count_solns [n] + [length [gen n n]]] diff --git a/examples/nq/repeat.h b/examples/nq/repeat.h new file mode 100644 index 0000000..8564abf --- /dev/null +++ b/examples/nq/repeat.h @@ -0,0 +1,70 @@ +#ifndef REPEAT_H +#define REPEAT_H + +#define repeat_1(x) x +#define repeat_2(x) x, repeat_1(x) +#define repeat_3(x) x, repeat_2(x) +#define repeat_4(x) x, repeat_3(x) +#define repeat_5(x) x, repeat_4(x) +#define repeat_6(x) x, repeat_5(x) +#define repeat_7(x) x, repeat_6(x) +#define repeat_8(x) x, repeat_7(x) +#define repeat_9(x) x, repeat_8(x) +#define repeat_10(x) x, repeat_9(x) +#define repeat_11(x) x, repeat_10(x) +#define repeat_12(x) x, repeat_11(x) +#define repeat_13(x) x, repeat_12(x) +#define repeat_14(x) x, repeat_13(x) +#define repeat_15(x) x, repeat_14(x) +#define repeat_16(x) x, repeat_15(x) +#define repeat_17(x) x, repeat_16(x) +#define repeat_18(x) x, repeat_17(x) +#define repeat_19(x) x, repeat_18(x) +#define repeat_20(x) x, repeat_19(x) +#define repeat_21(x) x, repeat_20(x) +#define repeat_22(x) x, repeat_21(x) +#define repeat_23(x) x, repeat_22(x) +#define repeat_24(x) x, repeat_23(x) +#define repeat_25(x) x, repeat_24(x) +#define repeat_26(x) x, repeat_25(x) +#define repeat_27(x) x, repeat_26(x) +#define repeat_28(x) x, repeat_27(x) +#define repeat_29(x) x, repeat_28(x) +#define repeat_30(x) x, repeat_29(x) +#define repeat_31(x) x, repeat_30(x) +#define repeat_32(x) x, repeat_31(x) + +#define repeat_n_1(x) concat(x, 1) +#define repeat_n_2(x) repeat_n_1(x), concat(x, 2) +#define repeat_n_3(x) repeat_n_2(x), concat(x, 3) +#define repeat_n_4(x) repeat_n_3(x), concat(x, 4) +#define repeat_n_5(x) repeat_n_4(x), concat(x, 5) +#define repeat_n_6(x) repeat_n_5(x), concat(x, 6) +#define repeat_n_7(x) repeat_n_6(x), concat(x, 7) +#define repeat_n_8(x) repeat_n_7(x), concat(x, 8) +#define repeat_n_9(x) repeat_n_8(x), concat(x, 9) +#define repeat_n_10(x) repeat_n_9(x), concat(x, 10) +#define repeat_n_11(x) repeat_n_10(x), concat(x, 11) +#define repeat_n_12(x) repeat_n_11(x), concat(x, 12) +#define repeat_n_13(x) repeat_n_12(x), concat(x, 13) +#define repeat_n_14(x) repeat_n_13(x), concat(x, 14) +#define repeat_n_15(x) repeat_n_14(x), concat(x, 15) +#define repeat_n_16(x) repeat_n_15(x), concat(x, 16) +#define repeat_n_17(x) repeat_n_16(x), concat(x, 17) +#define repeat_n_18(x) repeat_n_17(x), concat(x, 18) +#define repeat_n_19(x) repeat_n_18(x), concat(x, 19) +#define repeat_n_20(x) repeat_n_19(x), concat(x, 20) +#define repeat_n_21(x) repeat_n_20(x), concat(x, 21) +#define repeat_n_22(x) repeat_n_21(x), concat(x, 22) +#define repeat_n_23(x) repeat_n_22(x), concat(x, 23) +#define repeat_n_24(x) repeat_n_23(x), concat(x, 24) +#define repeat_n_25(x) repeat_n_24(x), concat(x, 25) +#define repeat_n_26(x) repeat_n_25(x), concat(x, 26) +#define repeat_n_27(x) repeat_n_26(x), concat(x, 27) +#define repeat_n_28(x) repeat_n_27(x), concat(x, 28) +#define repeat_n_29(x) repeat_n_28(x), concat(x, 29) +#define repeat_n_30(x) repeat_n_29(x), concat(x, 30) +#define repeat_n_31(x) repeat_n_30(x), concat(x, 31) +#define repeat_n_32(x) repeat_n_31(x), concat(x, 32) + +#endif /* REPEAT_H */ diff --git a/examples/nq/run_func.c b/examples/nq/run_func.c new file mode 100644 index 0000000..7ee0652 --- /dev/null +++ b/examples/nq/run_func.c @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include "repeat.h" + +typedef struct list { + void *kind; + void *contents; + struct list *next; +} list; + +/* FIXME: double fprintf is ugly */ +#define error(v, ...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + exit(v); \ +} while (0) + +#define _concat(a, b) a ## b +#define concat(a, b) _concat(a, b) + +#define assign_arg(n) \ + concat(arg_, n) = l->contents; \ + l = l->next + +#define cast_apply_f(n) return ((void *(*)(concat(repeat_, n)(void *))) f)\ +(concat(repeat_n_, n)(arg_)) + +#define new_case(n) \ + assign_arg(n); \ + if (arity == n) cast_apply_f(n) + +void *run_func(void *f, void *v, void *as) { + int arity = *((int *) v); + list *l = (list *) as; + + void repeat_n_32(*arg_); + + if (arity == 0) return ((void *(*)()) f)(); + new_case(1); + new_case(2); + new_case(3); + new_case(4); + new_case(5); + new_case(6); + new_case(7); + new_case(8); + new_case(9); + new_case(10); + new_case(11); + new_case(12); + new_case(13); + new_case(14); + new_case(15); + new_case(16); + new_case(17); + new_case(18); + new_case(19); + new_case(20); + new_case(21); + new_case(22); + new_case(23); + new_case(24); + new_case(25); + new_case(26); + new_case(27); + new_case(28); + new_case(29); + new_case(30); + new_case(31); + new_case(32); + error(1, "arity %i not handled", arity); +} + +void *inter_assert(void *a, void *b) { + assert(*((int *) a)); + return b; +} diff --git a/examples/nq/util.sin b/examples/nq/util.sin new file mode 100644 index 0000000..20cf3c4 --- /dev/null +++ b/examples/nq/util.sin @@ -0,0 +1,15 @@ +[def not [x] + [if x + 0 + 1]] + +[def and [x y] + [if x + y + 0]] + +[def != [x y] + [- x y]] + +[def = [x y] + [not [!= x y]]] |
