-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpartial_functions.rb
More file actions
353 lines (312 loc) · 8.76 KB
/
Copy pathpartial_functions.rb
File metadata and controls
353 lines (312 loc) · 8.76 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
$mm_limit = 0
def mm_limit
$mm_limit += 1
fail if $mm_limit == 5
result = yield
$mm_limit -= 1
result
end
$values = {}
class Object
def is?(cls)
is_a? cls
end
def value
self
end
def as_value
A_value.equ_to(self)
end
def unify(other)
self == other or other.is? A_value and other.unify(self)
end
end
class Numeric
def ^(other)
self**other
end
end
class Fixnum
def ^(other)
self**other
end
end
Scope = []
class << Scope
def enclose(b = {})
push(b)
result = yield
pop
result
end
def current
last || {}
end
end
class Symbol
def method_missing(*args)
mm_limit { (@variable ||= ((self.to_s =~ /^_/) ? A_gobbler : A_variable).new(self)).send(*args) }
end
def unify(other)
method_missing(:unify,other)
end
end
class Hash
def unify(other)
print " #{inspect}.unify #{other.class}:#{other.inspect}\n"
all? { |k,t| print " #{k.class}:#{k}.unify #{other.inspect} --> #{k.unify(other)}\n"; other.is_a? t and k.unify(other) }
end
end
class A_value
attr :forms
def self.equ_to(x)
$values[x] || new(x)
end
def initialize(*forms)
@forms = [forms].flatten
raise "What, _no_ forms from the start?!?" unless @forms
end
def +(other)
A_value.new A_sum.new(self,other)
end
def *(other)
A_value.new A_product.new(self,other)
end
def -(other)
self + -1*other
end
def ^(other)
self
end
def coerce(other)
[other.as_value,self]
end
def as_value
self
end
def inspect
if @forms.length == 1
@forms.first.inspect
else
"{\n#{@forms.collect {|r| r.inspect }.join(",\n")}\n}"
end
end
def pairings(patterns,reps = @forms,&block)
if patterns.empty?
yield
else
reps.each { |v|
print "Pairing #{v.inspect} with #{patterns.first.inspect}\n"
Scope.enclose(Scope.current.dup) {
pairings(patterns[1..-1],reps-[v],&block) if patterns.first.unify(v)
}
}
end
end
def apply_rules
$rules.each { |rule|
pairings(rule.preconditions) { @forms |= [rule.action.call].compact }
}
self
end
def method_missing(*args)
raise "What, _no_ forms?!?" unless @forms
if @forms.length == 1
print "#{@forms.inspect} sending #{args.inspect} to #{@forms.first}\n"
@forms.first.send(*args)
else
print "#{@forms.inspect} sending #{args.inspect} to ...and that's the problem.\n"
super
end
end
def unify(other)
true #self == other or @forms.include? other #false #@forms.find { |r| r.unify other }
end
end
class An_expression < Numeric
attr_reader :name,:parameters,:value
def initialize(name,*args,&block)
@name = name
@value = block
@parameters = args
end
def unify(other)
(@name == other.name) and @parameters.zip(other.parameters).all? { |p,ap| result = p.unify(ap); print " #{p.class}:#{p.inspect}.unify(#{ap.class}:#{ap.inspect}) = #{result}\n"; result }
end
def eval(actual)
Scope.enclose { (p ['unified',Scope]; @value.call) if unify(actual) }
end
def inspect
if parameters.empty?
name
elsif [:+,:*,:/,:-].include? name
"(#{parameters.collect { |t| t.inspect}.join(" #{name} ")})"
else
"#{name}(#{parameters.collect { |p| p.inspect }.join(",")})"
end
end
def coerce(other)
[other.as_value,self.as_value]
end
def method_missing(meth,*args)
p [self,meth,args]
mm_limit { as_value.send(meth,*args) }
end
end
class A_variable < A_value
def initialize(name)
@name = name
end
def as_value
Scope.current[self] || self
end
def value
as_value
end
def unify(other)
(Scope.current[self] ||= other).unify(other)
end
def inspect
@name
end
end
class Gobbler < A_variable
def unify(other)
true
end
end
class A_sum < An_expression
def initialize(*terms)
super(:+,*terms)
end
end
class A_product < An_expression
def initialize(*factors)
super(:*,*factors)
end
end
class A_method < An_expression
def as_value
mm_limit { A_value.new self.class.new(name,*@parameters.collect { |a| a.as_value }) }
end
end
_ = __ = ___ = ____ = :_ #.as_value
Infinity = 1.0/0.0
def partial_function(sym)
eval %Q{
def #{sym}(*args,&block)
A_method.new(#{sym.inspect},*args,&block)
end
}
end
$templates = Hash.new { |h,k| h[k] = [] }
def def_partial(template)
$templates[template.name] << template
end
partial_function :F
def_partial F(1) do
1
end
def_partial F(:n) do
:n * (:n - 1)
end
p F(7).value
%q{
#---------------------------------------------------------------------------------------------------
def apply(template)
$templates[template.name].collect { |t| t.eval(template) }.compact
end
class A_rule
attr_reader :preconditions,:action
def initialize(*args,&block)
@preconditions = args
@action = block
end
def applies_to(v)
@test.call(v) && (nv = @apply.call(v)) && nv.not_equivalent_to(v) && v.join(nv)
end
end
$rules = []
def define_rule(*args,&block)
$rules << A_rule.new(*args,&block)
end
define_rule( {:a=>A_method} ) {
p ['called with ',:a,:a.name,:a.as_value,Scope]
$templates[:a.name].find { |t|
print "Trying #{t.inspect}\n"
result = t.eval(:a.as_value)
result
}
}
define_rule( :a + (:b + :c) ) { (:a + :b) + :c }
define_rule( :a + :b ) { :b + :a }
define_rule( :a * (:b * :c) ) { (:a * :b) * :c }
define_rule( :a * :b ) { :b * :a }
define_rule( :a * (:b + :c) ) { :a*:b + :a*:c }
define_rule( :a,:v1 + :v2*:a ) { :v1/(1-:v2) }
def saturate(tree)
changed = true
while changed
changed = false
rules.each { |r|
nodes.each { |n|
changed ||= r.applies_at n
}
}
end
print tree.simpilest_representation
end
#---------------------------------------------------------------------------------------------------
def Best_T(*args,&block)
A_method.new(:Best_T,*args,&block)
end
def Odds(*args,&block)
A_method.new(:Odds,*args,&block)
end
#---------------------------------------------------------------------------------------------------
#
# Odds(X,N,T,P1,P2) -- Odds of X winning in N turns when it's T's turn and the score is P1,P2
#
def_partial Odds( 1,__,__,100,___) {1.0} # Player 1 wins if he get 100
def_partial Odds( _,__,__,100,___) {0.0}
def_partial Odds( 2,__,__,___,100) {1.0} # Player 2 wins if she gets 100
def_partial Odds( _,__,__,___,100) {0.0}
def_partial Odds( 0, 0,__,___,___) {1.0} # There's no other way to win immediately
def_partial Odds( _, 0,__,___,___) {0.0}
def_partial Odds( 1,__, 2,___,___) {0.0} # Player 1 can't win on 2's turn
def_partial Odds( 2,__, 1,___,___) {0.0} # Player 2 can't win on 1's turn
def_partial Odds( 1, 1, 1, 99,___) {0.5} # If player 1 has 99 and it's his turn, he has a 50% chance
def_partial Odds( 0, 1, 1, 99,___) {0.5} # to win in one turn, or it could be left undecided.
def_partial Odds( 2, 1, 2,___, 99) {0.5} # Ditto player 2 for player 2 (Best_T(___, 99) --> 1)
def_partial Odds( 0, 1, 2,___, 99) {0.5} #
def_partial Odds( 2, 1, 2,:p1,:p2) {}
def_partial Odds( 0, 1, 2,:p1,:p2) {}
def_partial Odds( 1,:n, 1,:p1,:p2) {
Odds( 1, 1, 1, :p1, :p2) +
Odds( 0, 1, 1, :p1, :p2) * (
0.5*Odds( 1, :n-1, 2, :p1+1,:p2) +
0.5*Odds( 1, :n-1, 2, :p1, :p2)
)
}
def_partial Odds( 2,:n, 2,:p1,:p2) {
Odds( 2, 1, 2, :p1, :p2) +
Odds( 0, 1, 2, :p1, :p2) * (
( 0.5^Best_T(:p1,:p2))*Odds( 1, :n-1, 2, :p1,:p2+2^Best_T(:p1,:p2)) +
(1.0-0.5^Best_T(:p1,:p2))*Odds( 1, :n-1, 2, :p1,:p2 )
)
}
def index_for_maximum(range)
range.collect { |i| yield i }
end
#
#
#
def_partial Best_T(___, 99) {1}
def_partial Best_T(:p1,:p2) {
# i which maximizes
# ( 0.5^i)*Odds( 1,Infinity, 2, :p1,:p2+2^i) +
# (1.0-0.5^i)*Odds( 1,Infinity, 2, :p1,:p2 )
# for all i from 1 to ln2(100-:p2)
}
# if_then_else(c,t,e)
}