summaryrefslogtreecommitdiffstats
path: root/ps04_combinators_amb/bnewbold_ps04_work.scm
blob: 5bcaf99aca52600ceb56094c59ebbe575d20cd2f (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
;;; 6.945 Problem Set #4 Source Code
;;; 03/04/2009
;;; Bryan Newbold <bnewbold@mit.edu>

; Note: this file is usually loaded from the load-*.scm files into 
; the-generic-evaluation-environment

;---------------------------
; Problem 4.1

#| ; This conflicts with the infix code below, so leave it commented in
   ; when testing that code.

(defhandler execute-application
  (lambda (proc args)
    ((procedure-body proc)
     (if (list? (procedure-parameters proc))
	 (extend-environment 
	  (procedure-parameters proc)
	  args
	  (procedure-environment proc))
	 (extend-environment 
	  (list (procedure-parameters proc))
	  (list args)
	  (procedure-environment proc)))))
     compound-procedure?)
|#
#| Testing:

eval> (define (ss . x) (map square x))
ok
eval> (ss 1 2 3)
(1 4 9)

; and the regular syntax still works:

eval> (define (s2 x y) (list (square x) (square y)))
ok
eval> (s2 1 2 3 4 5)
;Too many arguments supplied (x y) (1 2 3 4 5)
eval> (s2 1 2)
(1 4)
|#


;---------------------------
; Problem 4.2

; first we gotta listify the infix string or life will suck
(define (infix-operator? x)
  (member x (string->list "+-*^/")))
(define (infix-open-paren? x)
  (member x (string->list "(")))
(define (infix-close-paren? x)
  (member x (string->list ")")))
(define (infix-var? x)
  (member x (string->list "abcdefghijklmnopqrstuvwxyz")))
(define (infix-number? x)
  (member x (string->list "0123456789")))
(define (infix-same-type? a b)
  (cond ((infix-operator? a) (infix-operator? b))
	((infix-open-paren? a) (infix-open-paren? b))
	((infix-close-paren? a) (infix-close-paren? b))
	((infix-number? a) (infix-number? b))
	((infix-var? a) (infix-var? b))
	(else (error "Not a valid infix char type: " a))))
(define (infix-tag-type x)
  (let ((tag (cond ((infix-operator? (car x)) 'oper)
		   ((infix-open-paren? (car x)) 'open-paren)
		   ((infix-close-paren? (car x)) 'close-paren)
		   ((infix-var? (car x)) 'variable)
		   ((infix-number? (car x)) 'number)
		   (else (error "Something wrong, not a type: " x)))))
    (list tag (list->string (reverse x)))))

(define (listify in)
  (define out '())
  (define prog '())
  (define (work chunk)
    (cond ((null? chunk)
	   (cons (infix-tag-type prog) out))
	  ((equal? (car chunk) #\space)
	   (work (cdr chunk)))
	  ((null? prog) 
	   (begin
	     (set! prog (cons (car chunk) prog))
	     (work (cdr chunk))))
	  ((and (infix-same-type? (car prog) (car chunk))
		(not (or (infix-open-paren? (car prog))
			 (infix-close-paren? (car prog)))))
	   (begin 
	     (set! prog (cons (car chunk) prog))
	     (work (cdr chunk))))
	  (else 
	   (begin
	     (set! out (cons (infix-tag-type prog) out))
	     (set! prog (list (car chunk)))
	     (work (cdr chunk))))))
  (reverse (work (string->list in))))

#| Test:
(listify "4+34^4-(a*a+b^2)")
;Value: ((number "4") (oper "+") (number "34") (oper "^") (number "4") (oper "-") (open-paren "(") (var "a") (oper "*") (var "a") (oper "+") (var "b") (oper "^") (number "2") (close-paren ")"))

(listify "4+34^4 - ( a*a + b^2)")
;Value: ((number "4") (oper "+") (number "34") (oper "^") (number "4") (oper "-") (open-paren "(") (var "a") (oper "*") (var "a") (oper "+") (var "b") (oper "^") (number "2") (close-paren ")"))
|#

; alright, now we can do fun NLP mojo


#| These definitions should be pasted into an amb-evaluator:

(define (require p)
  (if (not p) (amb)))

(define *unparsed* '())

(define (parse-type t)
  (require (not (null? *unparsed*)))
  (require (equal? (caar *unparsed*) t))
  (let ((found-type (car *unparsed*)))
    (set! *unparsed* (cdr *unparsed*))
    found-type))

(define (parse-binary)
  (list 'binary
	(parse-simple-expression)
	(parse-type 'oper)
	(parse-expression)))

(define (parse-chunk)
  (list 'chunk
	(parse-type 'open-paren)
	(parse-expression)
	(parse-type 'close-paren)))

(define (parse-function)
  (list 'function
	(parse-type 'variable)
	(parse-chunk)))

(define (parse-simple-expression)
  (amb (parse-type 'number)
       (parse-type 'variable)
       (parse-chunk)
       (parse-function)))

(define (parse-expression)
  (amb (parse-type 'number)
       (parse-type 'variable)
       (parse-binary)
       (parse-chunk)
       (parse-function)))

(define (parse-infix in)
  (set! *unparsed* (listify in))
  (let ((sent (parse-expression)))
    (require (null? *unparsed*))
    sent))

#| Test:
;;; Amb-Eval input:
(parse-infix "sqrt(5+square(4*4))")

;;; Starting a new problem 
;;; Amb-Eval value:
Number of alts considered: 99
(function
 (variable "sqrt")
 (chunk
  (open-paren "(")
  (binary
   (number "5")
   (oper "+")
   (function
    (variable "square")
    (chunk (open-paren "(")
           (binary (number "4") (oper "*") (number "4"))
           (close-paren ")"))))
  (close-paren ")")))

;;; Amb-Eval input:
(parse-infix "1+2+4+5")

;;; Starting a new problem 
;;; Amb-Eval value:
Number of alts considered: 13
(binary
 (number "1")
 (oper "+")
 (binary (number "2")
         (oper "+")
         (binary (number "4") (oper "+") (number "5"))))

|#

(define (lispify parsed)
  (cond ((eq? (car parsed) 'function)
	 (list (lispify (car (cdr parsed)))
	       (lispify (car (cdr (cdr parsed))))))
	((eq? (car parsed) 'variable)
	 (string->symbol (car (cdr parsed))))
	((eq? (car parsed) 'number)
	 (string->number (car (cdr parsed))))
	((eq? (car parsed) 'binary)
	 (list (lispify (car (cdr (cdr parsed))))
	       (lispify (car (cdr parsed)))
	       (lispify (car (cdr (cdr (cdr parsed)))))))
	((eq? (car parsed) 'chunk)
	 (lispify (car (cdr (cdr parsed)))))
	((eq? (car parsed) 'oper)
	 (string->symbol (car (cdr parsed))))
	(else (error "Unhandled type: " (car parsed)))))

; note, this requres eval-in-amb to be defined in the top level
(define (infix in)
  (eval-in-amb (lispify (parse-infix in))))

#|
; x needs to be defined in the top level; here it's 5

;;; Amb-Eval input:
(infix "sqrt(4+3/x*square(56 - 83))")

;;; Starting a new problem 
;;; Amb-Eval value:
Number of alts considered: 146
2.0002057507335316

|#