summaryrefslogtreecommitdiff
path: root/examples/nq/nqueens.sin
blob: 9d6917b87b08558195d48f0b24d147c444453544 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
[type closure_ [func arity arg_c args]]
[type list_ [kind head tail]]
[type nil_ [kind]]
[type left_  [kind val]]
[type right_ [kind val]]

[dec + [x y]]
[dec - [x y]]
[dec run_func [f arity args]]

[def head [xs]
    [list_.head xs]]

[def tail [xs]
    [list_.tail xs]]

[def cons [h t]
    [list_ 0 h t]]

[def nil []
    [nil_ 1]]

[def is_nil [xs]
    [if [nil_.kind xs]
        1
        0]]

[def left [val]
    [left_ 0 val]]

[def right [val]
    [right_ 1 val]]

[def left.val [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 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]]]

[def inc [x]
    [+ x 1]]

[def append [xs x]
    [if [is_nil xs]
        [cons x [nil]]
        [cons [head xs] [append [tail xs] x]]]]

[def add_arg [c a]
    [closure_ [closure_.func c]
          [closure_.arity c]
          [inc [closure_.arg_c c]]
          [append [closure_.args c] a]]]

[def wake [h]
    [run_func [closure_.func h] [closure_.arity h] [closure_.args h]]]

[def closure [f v]
    [closure_ f v 0 [nil]]]

[def _length [xs n]
    [if [is_nil xs]
        n
        [_length [tail xs] [inc n]]]]

[def length [xs]
    [_length xs 0]]

[def concat [as bs]
    [if [is_nil as]
        bs
        [cons [head as] [concat [tail as] bs]]]]

[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 pure [x]
    [cons x [nil]]]

[def empty []
    [nil]]

[def return [x]
    [cons x [nil]]]

[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 foldl [c acc xs]
    [if [is_nil xs]
        acc
        [foldl c [wake [add_arg [add_arg c acc] [head xs]]] [tail xs]]]]

[def concatMapClosure [c acc x]
    [concat acc [wake [add_arg c x]]]]

[def concatMap [c xs]
    [foldl [add_arg [closure concatMapClosure 3] c] [nil] 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]]
         [add_arg [add_arg [closure gen_1_1_1 3] q] b]]]

[def gen_1 [nq b]
    [>>= [.. 1 nq]
         [add_arg [closure gen_1_1 2] b]]]

[def gen [n nq]
    [if n
        [>>= [gen [- n 1] nq]
              [add_arg [closure gen_1 2] nq]]
        [cons [nil] [nil]]]]