A categorical programming language
修訂 | fc5364cb07370fb5aa5fdc2d03980c4c22d8de1e (tree) |
---|---|
時間 | 2023-02-16 07:44:47 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Hammer out the core of an interpreter for CCCs with NNO.
The only thing I don't really like is the encoding of curries, but I'm
willing to trade readability for speed here.
@@ -2,10 +2,27 @@ | ||
2 | 2 | (import scheme |
3 | 3 | matchable) |
4 | 4 | |
5 | - (define cammy-compile | |
5 | + (define cammy-compile-aux | |
6 | 6 | (match-lambda |
7 | 7 | ['id (lambda (x) x)] |
8 | + [`(comp ,f ,g) (lambda (x) (g (f x)))] | |
9 | + ['ignore (lambda (_) '*)] | |
10 | + [`(pair ,f ,g) (lambda (x) (cons (f x) (g x)))] | |
11 | + [`(curry ,f) (lambda (x) (lambda (y) (f (cons x y))))] | |
12 | + [`(uncurry ,f) (lambda (x) ((f (car x)) (cdr x)))] | |
13 | + ['fst car] ['snd cdr] | |
14 | + ['t (lambda (_) #t)] ['f (lambda (_) #f)] | |
15 | + ['not not] | |
8 | 16 | ['zero (lambda (_) 0)] |
9 | 17 | ['succ (lambda (n) (+ n 1))] |
18 | + [`(pr ,f ,g) | |
19 | + (lambda (x) | |
20 | + (let loop ((n x) (acc (f '*))) | |
21 | + (if (= n 0) acc (loop (- n 1) (g acc)))))] | |
10 | 22 | )) |
23 | + | |
24 | + (define (cammy-compile expr) | |
25 | + (if (list? expr) | |
26 | + (cammy-compile-aux (cons (car expr) (map cammy-compile (cdr expr)))) | |
27 | + (cammy-compile-aux expr))) | |
11 | 28 | ) |