-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai-symbolic.rkt
63 lines (54 loc) · 1.7 KB
/
ai-symbolic.rkt
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
#lang racket/base
(require "stream-syntax.rkt"
"peval.rkt"
"prim.rkt"
"tools.rkt"
"ai-eval.rkt"
)
(provide ai-symbolic)
;; Transform abstract syntax to tree formula, i.e. no node sharing.
;; This is for debugging, as the absence of sharing makes it less useful for code.
;; FIXME: It might be useful to add support a `let' form whenever an
;; expression is used more than once, otherwise inline in the
;; expression tree.
(define (pretty-symbolic form)
(define (pretty-tag x)
(case x
((p_add) '+)
((p_sub) '-)
((p_mul) '*)
((p_div) '/)
((p_pow) '^)
(else x)))
(match form
((list-rest tag args)
(cons (pretty-tag tag)
(map pretty-symbolic args)))
(else form)))
(define (ai-symbolic program)
(lambda args
(define (literal sem x) x)
(define (op sym)
(lambda (sem . args) (cons sym args)))
(define ((ni op) . _)
(error 'ai-symbolic-not-inplemented (format "~a" op)))
(define (prim sym fn)
(lambda (sem . expr)
(->datum (peval ai-eval-semantics fn sym expr))))
(define ai-symbolic-semantics
(make-ai #:default
(lambda (sym fn)
(lambda (sem . args)
(cons sym args)))
#:add (prim 'p_add p_add)
#:sub (prim 'p_sub p_sub)
#:mul (prim 'p_mul p_mul)
#:div (prim 'p_div p_div)
#:pow (prim 'p_pow p_pow)
#:literal literal
#:for/n (ni 'reduce/n)
#:feedback/n (ni 'feedback/n)))
(pretty-symbolic
(apply (ai-function-proc program)
ai-symbolic-semantics
args))))