-
Notifications
You must be signed in to change notification settings - Fork 18
/
dp.d
1251 lines (1242 loc) · 39.8 KB
/
dp.d
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import std.conv: text;
import std.string: split;
import std.algorithm: map;
import std.array: array;
import std.range: iota;
import std.string: startsWith;
import util.tuple: q=tuple,Q=Tuple;
import util.io:writeln;
import std.exception: enforce;
import backend,options;
import distrib;
import sym.dexpr,util.hashtable,util;
import ast.expression,ast.declaration,ast.type,ast.error;
import ast.semantic_,ast.scope_,context;
import std.random, sample; // (for InferenceMethod.simulate)
class Bruteforce: Backend{
this(string sourceFile){
this.sourceFile=sourceFile;
}
override Distribution analyze(FunctionDef def,ErrorHandler err){
DExpr[string] fields;
foreach(i,a;def.params) fields[a.getName]=dVar(a.getName);
DExpr init=dRecord(fields);
auto interpreter=Interpreter(def,def.body_,init,false);
auto ret=distInit();
interpreter.runFun(ret);
assert(!!def.ftype);
bool isTuple = !!def.ftype.cod.isTupleTy;
return ret.toDistribution(def.params.map!(p=>p.getName).array,def.isTuple,def.retNames,isTuple);
}
private:
string sourceFile;
}
auto distInit(){
return Dist(MapX!(DExpr,DExpr).init,zero,SetX!string.init);
}
struct Dist{
MapX!(DExpr,DExpr) state;
DExpr error;
SetX!string tmpVars;
// @disable this();
this(MapX!(DExpr,DExpr) state,DExpr error,SetX!string tmpVars){
this.state=state;
this.error=error;
this.tmpVars=tmpVars;
}
Dist dup(){ return Dist(state.dup,error,tmpVars.dup); static assert(this.tupleof.length==3); }
void addTmpVar(string var){ tmpVars.insert(var); }
Dist marginalizeTemporaries(){
if(!tmpVars.length) return this;
auto r=distInit;
r.error=error;
foreach(k,v;state){
auto rec=cast(DRecord)k;
assert(!!rec,text(k));
auto val=rec.values.dup;
foreach(x;tmpVars) val.remove(x);
r.add(dRecord(val),v);
}
tmpVars.clear();
return r;
}
void add(DExpr k,DExpr v){
if(k in state) state[k]=(state[k]+v).simplify(one);
else state[k]=v;
if(state[k] == zero) state.remove(k);
}
void opOpAssign(string op:"+")(Dist r){
foreach(k,v;r.state)
add(k,v);
error=(error+r.error).simplify(one);
foreach(t;r.tmpVars) addTmpVar(t);
}
Dist map(DLambda lambda){
if(opt.trace) writeln("particle-size: ",state.length);
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state) r.add(dApply(lambda,k).simplify(one),v);
return r;
}
DExpr expectation(DLambda lambda,bool hasFrame){
if(!hasFrame){
DExpr r1=zero,r2=zero;
foreach(k,v;state){
r1=(r1+v*dApply(lambda,k)).simplify(one);
r2=(r2+v).simplify(one);
}
return (r1/r2).simplify(one);
}
MapX!(DExpr,Q!(Q!(DExpr,DExpr)[],DExpr,DExpr)) byFrame;
foreach(k,v;state){
auto frame=dField(k,"`frame").simplify(one);
if(frame in byFrame) byFrame[frame][0]~=q(k,v);
else byFrame[frame]=q([q(k,v)],zero,zero);
}
foreach(k,ref v;byFrame){
foreach(x;v[0]){
v[1]=(v[1]+x[1]*dApply(lambda,x[0])).simplify(one);
v[2]=(v[2]+x[1]).simplify(one);
}
}
auto r=distInit;
r.copyNonState(this);
static int unique=0;
string tmp="`expectation"~lowNum(++unique);
addTmpVar(tmp);
foreach(k,v;byFrame){
auto e=(v[1]/v[2]).simplify(one);
foreach(x;v[0])
r.add(dRUpdate(x[0],tmp,e).simplify(one),x[1]);
}
this=r;
return dField(db1,tmp);
}
Dist pushFrame(){
return map(dLambda(dRecord(["`frame":db1])));
}
Dist popFrame(string tmpVar){
addTmpVar(tmpVar);
return map(dLambda(dRUpdate(dField(db1,"`frame"),tmpVar,dField(db1,"`value"))));
}
Dist flatMap(DLambda[] lambdas){
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state){
foreach(lambda;lambdas){
auto app=dApply(lambda,k).simplify(one);
auto val=app[0.dℚ].simplify(one),scale=app[1.dℚ].simplify(one);
r.add(val,(v*scale).simplify(one));
}
}
return r;
}
Dist assertTrue(DLambda lambda){
if(opt.noCheck) return this;
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state){
auto cond=dApply(lambda,k).simplify(one);
if(cond == one || cond == zero){
if(cond == zero){
r.error = (r.error + v).simplify(one);
}else{
r.add(k,v);
}
}else{
r.error = (r.error + v*dEqZ(cond)).simplify(one);
r.add(k,(v*cond).simplify(one));
}
}
return r;
}
Dist eraseErrors(){
auto r=dup();
r.error=zero;
return r;
}
Dist observe(DLambda lambda){
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state){
auto cond=dApply(lambda,k).simplify(one);
if(cond == one || cond == zero){
if(cond == one) r.add(k,v);
}else{
r.add(k,(v*cond).simplify(one));
}
}
return r;
}
private Distribution toDistributionImpl(Distribution r,string[] names,bool isTuple){
auto vars=names.map!(name=>r.declareVar(name)).array;
r.distribution=zero;
foreach(k,v;state){
auto cur = v;
foreach(i,var;vars){
assert(!!var);
auto val = dField(k,"`value");
cur = cur*dDiscDelta(isTuple?val[i.dℚ]:val,var);
}
r.distribution=r.distribution+cur;
}
r.error=error;
r.simplify();
r.orderFreeVars(vars,isTuple);
return r;
}
Distribution toDistribution(string[] names,bool isTuple){
auto r=new Distribution();
return toDistributionImpl(r,names,isTuple);
}
Distribution toDistribution(string[] paramNames,bool argsIsTuple,string[] names,bool isTuple){
auto r=new Distribution();
auto args=paramNames.map!(name=>r.declareVar(name)).array;
r.addArgs(args,argsIsTuple,null);
return toDistributionImpl(r,names,isTuple);
}
/*DExpr flip(DExpr p){
// TODO: check that p is in [0,1]
static int unique=0;
auto tmp="`flip"~lowNum(++unique);
addTmpVar(tmp);
this=flatMap(
[dLambda(dTuple([dRUpdate(db1,tmp,zero),(one-p).simplify(one)])),
dLambda(dTuple([dRUpdate(db1,tmp,one),p]))]);
if(opt.backend==InferenceMethod.simulate) pickOne();
return dField(db1,tmp);
}*/
DExpr uniformInt(DExpr arg){
// TODO: check constraints on arguments
auto r=distInit;
r.copyNonState(this);
auto lambda=dLambda(arg);
static int unique=0;
auto tmp="`uniformInt"~lowNum(++unique);
r.addTmpVar(tmp);
if(opt.backend==InferenceMethod.simulate){
long low,up;
DExpr result=null;
foreach(k,v;state){
auto ab=dApply(lambda,k).simplify(one);
auto a=ab[0.dℚ].simplify(one), b=ab[1.dℚ].simplify(one);
static ℤ toNum(string what)(DExpr e){
if(auto q=cast(Dℚ)e) return mixin(what~"div")(q.c.num,q.c.den);
import std.format: format;
import std.math: floor, ceil;
if(auto f=cast(DFloat)e) return ℤ(format("%.0s",mixin(what)(f.c)));
enforce(0,text("non-constant condition ",e));
assert(0);
}
ℤ lowCand=toNum!"ceil"(a),upCand=toNum!"floor"(b);
if(!result){
import std.random;
enforce(lowCand<=upCand);
assert(long.min<=lowCand && upCand<=long.max);
low=lowCand.toLong(), up=upCand.toLong();
result=dℚ(uniform!"[]"(low,up));
}else enforce(low==lowCand && up==upCand);
r.add(dRUpdate(k,tmp,result).simplify(one),v);
}
}else{
foreach(k,v;state){
auto ab=dApply(lambda,k).simplify(one);
auto a=ab[0.dℚ].simplify(one), b=ab[1.dℚ].simplify(one);
auto az=a.isInteger(), bz=b.isInteger();
assert(az&&bz,text("TODO: ",a," ",b));
auto num=dℚ(bz.c.num-az.c.num+1);
if(num.c>0){
auto nv=(v/num).simplify(one);
for(ℤ i=az.c.num;i<=bz.c.num;++i) r.add(dRUpdate(k,tmp,i.dℚ).simplify(one),nv);
}else r.error = (r.error + v).simplify(one);
}
}
this=r;
//if(opt.backend==InferenceMethod.simulate) pickOne();
return dField(db1,tmp);
}
DExpr categorical(DExpr arg){
auto r=distInit;
r.copyNonState(this);
auto lambda=dLambda(arg);
static int unique=0;
auto tmp="`categorical"~lowNum(++unique);
r.addTmpVar(tmp);
if(opt.backend==InferenceMethod.simulate){
DExpr[] p;
DExpr result=null;
foreach(k,v;state){
auto arr=dApply(lambda,k).simplify(one);
auto len=dField(arr,"length").simplify(one);
auto qlen=cast(Dℚ)len;
enforce(!!qlen && qlen.c.den==1);
DExpr[] pCand;
DExpr sum=zero;
foreach(i;0..qlen.c.num.toLong()){
auto cur=arr[i.dℚ].simplify(one);
pCand~=cur;
sum=(sum+cur).simplify(one);
}
enforce(cast(DFloat)sum||sum==one,"category probabilities must sum up to 1");
if(!result){
import std.random;
p=pCand;
real f=uniform(0.0L,1.0L);
DExpr cur=zero;
foreach(i,val;p){
cur=(cur+val).simplify(one);
auto r=dLe(dFloat(f),cur).simplify(one);
enforce(r == zero || r == one);
if(r==one){
result = dℚ(i);
break;
}
}
}else enforce(p==pCand);
r.add(dRUpdate(k,tmp,result).simplify(one),v);
}
}else{
foreach(k,v;state){
auto p=dApply(lambda,k).simplify(one);
auto l=dField(p,"length").simplify(one);
auto lz=l.isInteger();
enforce(!!lz,"TODO");
if(lz!=zero){
// TODO: check other preconditions
for(ℤ i=0.ℤ;i<lz.c.num;++i)
r.add(dRUpdate(k,tmp,i.dℚ).simplify(one),(v*p[i.dℚ]).simplify(one));
}else r.error = (r.error + v).simplify(one);
}
}
this=r;
//if(opt.backend==InferenceMethod.simulate) pickOne();
return dField(db1,tmp);
}
void assignTo(DExpr lhs,DExpr rhs){
void assignToImpl(DExpr lhs,DExpr rhs){
if(auto id=cast(DNVar)lhs){
auto lambda=dLambda(dRUpdate(db1,id.name,rhs));
this=map(lambda);
}else if(auto idx=cast(DIndex)lhs){
assignTo(idx.e,dIUpdate(idx.e,idx.i,rhs));
}else if(auto fe=cast(DField)lhs){
if(fe.e == db1){
assignTo(dVar(fe.f),rhs);
return;
}
assignTo(fe.e,dRUpdate(fe.e,fe.f,rhs));
}else if(auto tpl=cast(DTuple)lhs){
if(!cast(DField)rhs){
static int unique=0;
auto tmp="`tpl"~lowNum(++unique);
addTmpVar(tmp);
assignTo(dField(db1,tmp),rhs);
assignTo(lhs,dField(db1,tmp));
}else{
foreach(i;0..tpl.values.length)
assignTo(tpl[i],rhs[i.dℚ].simplify(one));
}
}else if(cast(DPlus)lhs||cast(DMult)lhs){
// TODO: this could be the case (if cond { a } else { b }) = c;
// (this is also not handled in the symbolic backend at the moment)
}else{
assert(0,text("TODO: ",lhs," = ",rhs));
}
}
assignToImpl(lhs,rhs);
}
DExpr call(FunctionDef fun,DExpr thisExp,DExpr arg,Scope sc){
auto ncur=pushFrame();
if(fun.isConstructor) ncur=ncur.map(dLambda(dRUpdate(db1,fun.thisVar.getName,dRecord())));
if(thisExp) ncur=ncur.map(dLambda(dRUpdate(db1,fun.contextName,inFrame(thisExp))));
else if(fun.isNested) ncur=ncur.map(dLambda(dRUpdate(db1,fun.contextName,inFrame(buildContextFor!readLocal(fun,sc)))));
if(fun.isNested&&fun.isConstructor) ncur=ncur.map(dLambda(dRUpdate(db1,fun.thisVar.getName,dRecord([fun.contextName:dField(db1,fun.contextName)]))));
if(fun.isTuple){
DExpr updates=db1;
foreach(i,prm;fun.params){
updates=dRUpdate(updates,prm.getName,inFrame(arg[i.dℚ]));
}
if(updates != db1) ncur=ncur.map(dLambda(updates));
}else{
assert(fun.params.length==1);
ncur=ncur.map(dLambda(dRUpdate(db1,fun.params[0].getName,inFrame(arg))));
}
auto intp=Interpreter(fun,fun.body_,ncur,true);
auto nndist = distInit();
intp.runFun(nndist);
static uniq=0;
string tmp="`call"~lowNum(++uniq);
this=nndist.popFrame(tmp);
if(thisExp&&!fun.isConstructor){
assignTo(thisExp,dField(db1,tmp)[1.dℚ]);
assignTo(dVar(tmp),dField(db1,tmp)[0.dℚ]);
}
return dField(db1,tmp);
}
private Dist callImpl(DExpr fun,DExpr arg){
this=pushFrame();
auto f=dLambda(inFrame(fun)), a=dLambda(inFrame(arg));
auto r=distInit;
r.copyNonState(this);
MapX!(FunctionDef,MapX!(DExpr,DExpr)) byFun;
foreach(k,v;state){
auto cf=dApply(f,k).simplify(one);
auto ca=dApply(a,k).simplify(one);
FunctionDef fdc;
DExpr ctx;
if(auto bcf=cast(DDPContextFun)cf){
fdc=bcf.def;
ctx=bcf.ctx;
}else if(auto bff=cast(DDPFun)cf) fdc=bff.def;
else assert(0,text(cf," ",typeid(cf)));
DExpr nk=k;
if(fdc.isTuple){
foreach(i,prm;fdc.params)
nk=dRUpdate(nk,prm.getName,ca[i.dℚ]).simplify(one);
}else{
assert(fdc.params.length==1);
nk=dRUpdate(nk,fdc.params[0].getName,ca).simplify(one);
}
assert(!!fdc.context==!!ctx,text(fdc.context," ",ctx," ",cf));
if(ctx) nk=dRUpdate(nk,fdc.contextName,ctx).simplify(one);
if(fdc !in byFun) byFun[fdc]=typeof(byFun[fdc]).init;
byFun[fdc][nk]=v;
}
foreach(fdc,kv;byFun){
auto ncur=distInit;
ncur.state=kv;
ncur.copyNonState(this);
auto intp=Interpreter(fdc,fdc.body_,ncur,true);
auto nndist = distInit();
intp.runFun(nndist);
r+=nndist;
}
return r;
}
DExpr call(DExpr fun,DExpr arg){
auto r=callImpl(fun,arg);
static uniq=0;
string tmp="`call'"~lowNum(++uniq);
this=r.popFrame(tmp);
return dField(db1,tmp);
}
Dist normalize(){
auto r=distInit();
r.copyNonState(this);
DExpr tot=r.error;
foreach(k,v;state) tot=(tot+v).simplify(one);
if(!opt.noCheck && tot == zero) r.error=one;
else if(cast(Dℚ)tot) foreach(k,v;state) r.add(k,(v/tot).simplify(one));
else{
if(!opt.noCheck) r.error=(dNeqZ(tot)*r.error+dEqZ(tot)).simplify(one);
foreach(k,v;state) r.add(k,((v/tot)*dNeqZ(tot)).simplify(one));
}
return r;
}
DExpr infer(DExpr fun){
assert(opt.backend != InferenceMethod.simulate,"TODO: higher-order inference in simulate backend");
auto r=distInit;
r.copyNonState(this);
static uniq=0;
string tmp="`infer"~lowNum(++uniq);
addTmpVar(tmp);
foreach(k,v;state){
auto cur=distInit;
cur.add(k,v);
cur=cur.callImpl(fun,dTuple([])); // TODO: group calls that depend on identical information
cur=cur.map(dLambda(dRecord(["`value":dField(db1,"`value")])));
cur.tmpVars.clear();
cur=cur.normalize();
r.add(dRUpdate(k,tmp,dDPDist(cur)),v);
}
this=r;
return dField(db1,tmp);
}
DExpr distSample(DExpr dist){
auto r=distInit;
r.copyNonState(this);
auto d=dLambda(dist);
static uniq=0;
string tmp="`sample"~lowNum(++uniq);
r.addTmpVar(tmp);
if(opt.backend==InferenceMethod.simulate){
DExpr theDist=null;
DExpr result=null;
foreach(k,v;state){
auto dbf=cast(DDPDist)dApply(d,k).simplify(one);
assert(!!dbf,text(dbf," ",d));
if(!result){
theDist=dbf;
real f=uniform(0.0L,1.0L);
DExpr cur=zero;
DExpr sum=zero;
foreach(dk,dv;dbf.dist.state)
sum=(sum+dv).simplify(one);
enforce(sum==one,"probabilities must sum up to 1");
foreach(dk,dv;dbf.dist.state){
cur=(cur+dv).simplify(one);
auto r=dLe(dFloat(f),cur).simplify(one);
enforce(r == zero || r == one);
if(r==one){
result = dField(dk,"`value").simplify(one);
break;
}
}
}else enforce(dbf==theDist);
r.add(dRUpdate(k,tmp,result).simplify(one),v);
}
}else{
foreach(k,v;state){
auto dbf=cast(DDPDist)dApply(d,k).simplify(one);
assert(!!dbf,text(dbf," ",d));
foreach(dk,dv;dbf.dist.state) r.add(dRUpdate(k,tmp,dField(dk,"`value")).simplify(one),(v*dv).simplify(one));
if(!opt.noCheck) r.error=(r.error+v*dbf.dist.error).simplify(one);
}
}
this=r;
// if(opt.backend==InferenceMethod.simulate) pickOne();
return dField(db1,tmp);
}
DExpr distExpectation(DExpr dist){
auto r=distInit;
r.copyNonState(this);
auto d=dLambda(dist);
static uniq=0;
string tmp="`expectation'"~lowNum(++uniq);
r.addTmpVar(tmp);
foreach(k,v;state){
auto dbf=cast(DDPDist)dApply(d,k).simplify(one);
assert(!!dbf,text(dbf," ",d));
DExpr val1=zero,val2=zero;
foreach(dk,dv;dbf.dist.state){
val1=(val1+dv*dField(dk,"`value")).simplify(one);
val2=(val2+dv).simplify(one);
}
if(!opt.noCheck&&val2==zero) r.error=(r.error+v).simplify(one);
else if(cast(Dℚ)val2) r.add(dRUpdate(k,tmp,val1/val2).simplify(one),v);
else{
if(!opt.noCheck) r.error=(r.error+v*dEqZ(val2)).simplify(one);
r.add(dRUpdate(k,tmp,val1/val2).simplify(one),(v*dNeqZ(val2)).simplify(one));
}
}
this=r;
return dField(db1,tmp);
}
DExpr distError(DExpr dist){
if(opt.noCheck) return zero;
auto r=distInit;
r.copyNonState(this);
auto d=dLambda(dist);
static uniq=0;
string tmp="`error'"~lowNum(++uniq);
r.addTmpVar(tmp);
foreach(k,v;state){
auto dbf=cast(DDPDist)dApply(d,k).simplify(one);
assert(!!dbf,text(dbf," ",d));
DExpr error=dbf.dist.error;
r.add(dRUpdate(k,tmp,error).simplify(one),v);
}
this=r;
return dField(db1,tmp);
}
void copyNonState(ref Dist rhs){
this.tupleof[1..$]=rhs.tupleof[1..$];
}
void pickOne()in{assert(opt.backend==InferenceMethod.simulate);}do{
real f = uniform(0.0L,1.0L);
DExpr cur=zero;
foreach(k,v;state){
cur=(cur+v).simplify(one);
auto r=dLe(dFloat(f),cur).simplify(one);
assert(r == zero || r == one);
if(r == one){
error = zero;
state.clear();
state[k]=one;
return;
}
}
cur = (cur+error).simplify(one);
assert(cur == one);
state.clear();
error = one;
}
Dist simplify(DExpr facts){
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state)
r.add(k.simplify(facts),v.simplify(facts));
return r;
}
Dist substitute(DVar var,DExpr expr){
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state)
r.add(k.substitute(var,expr),v.substitute(var,expr));
return r;
}
Dist incDeBruijnVar(int di, int free){
auto r=distInit;
r.copyNonState(this);
foreach(k,v;state)
r.add(k.incDeBruijnVar(di,free),v.incDeBruijnVar(di,free));
return r;
}
int freeVarsImpl(scope int delegate(DVar) dg,ref DExprSet visited){
foreach(k,v;state){
if(auto r=k.freeVarsImpl(dg,visited)) return r;
if(auto r=v.freeVarsImpl(dg,visited)) return r;
}
return 0;
}
}
DExpr inFrame(DExpr arg){
return arg.substitute(db1,dField(db1,"`frame"));
}
class DDPFun: DExpr{
FunctionDef def;
alias subExprs=Seq!def;
override string toStringImpl(Format formatting,Precedence prec,int binders){
return def.name.name;
}
mixin Constant;
}
mixin FactoryFunction!DDPFun;
class DDPContextFun: DExpr{
FunctionDef def;
DExpr ctx;
alias subExprs=Seq!(def,ctx);
override string toStringImpl(Format formatting,Precedence prec,int binders){
return text(def.name?def.name.name:text("(",def,")"),"@(",ctx.toStringImpl(formatting,Precedence.none,binders),")");
}
mixin Visitors;
override DExpr simplifyImpl(DExpr facts){ return dDPContextFun(def,ctx.simplify(facts)); }
}
mixin FactoryFunction!DDPContextFun;
class DDPDist: DExpr{
Dist dist;
alias subExprs=Seq!dist;
this(Dist dist)in{assert(!dist.tmpVars.length);}do{ this.dist=dist; }
override string toStringImpl(Format formatting,Precedence prec,int binders){
auto d=dist.toDistribution(["r"],false);
d.addArgs([],true,null);
return dApply(d.toDExpr(),dTuple([])).simplify(one).toStringImpl(formatting,prec,binders);
}
override DExpr simplifyImpl(DExpr facts){ return dDPDist(dist.simplify(facts)); }
mixin Visitors;
}
DDPDist dDPDist(Dist dist)in{assert(!dist.tmpVars.length);}do{
static MapX!(Q!(MapX!(DExpr,DExpr),DExpr),DDPDist) uniq;
auto t=q(dist.state,dist.error);
if(t in uniq) return uniq[t];
auto r=new DDPDist(dist);
uniq[t]=r;
return r;
}
import ast.lexer: Tok;
alias ODefExp=BinaryExp!(Tok!":=");
DExpr readLocal(string name){ return dField(db1,name); }
DExpr readFunction(Identifier id)in{ assert(id && id.scope_ && cast(FunctionDef)id.meaning); }do{
auto fd=cast(FunctionDef)id.meaning;
assert(!!fd);
if(!fd.isNested) return dDPFun(fd);
return dDPContextFun(fd,buildContextFor!readLocal(fd,id.scope_));
}
struct Interpreter{
FunctionDef functionDef;
CompoundExp statements;
Dist cur;
bool hasFrame=false;
this(FunctionDef functionDef,CompoundExp statements,DExpr init,bool hasFrame){
auto cur=distInit;
cur.state[init]=one;
this(functionDef,statements,cur,hasFrame);
}
this(FunctionDef functionDef,CompoundExp statements,Dist cur,bool hasFrame){
this.functionDef=functionDef;
this.statements=statements;
this.cur=cur;
this.hasFrame=hasFrame;
}
DExpr runExp(Expression e){
if(!cur.state.length) return zero;
// TODO: get rid of code duplication
DExpr doIt(Expression e){
if(e.type == typeTy) return dTuple([]); // TODO: get rid of this
if(auto pl=cast(PlaceholderExp)e) return dVar(pl.ident.name);
if(auto id=cast(Identifier)e){
if(!id.meaning&&id.name=="π") return dΠ;
if(auto init=id.getInitializer())
return doIt(init);
if(auto r=lookupMeaning!(readLocal,readFunction)(id)) return r;
assert(0,"unsupported");
}
if(auto fe=cast(FieldExp)e){
if(isBuiltIn(fe)){
if(fe.e.type.isTupleTy||cast(ArrayTy)fe.e.type||cast(VectorTy)fe.e.type){
assert(fe.f.name=="length");
}
}
return dField(doIt(fe.e),fe.f.name);
}
if(auto ae=cast(AddExp)e) return doIt(ae.e1)+doIt(ae.e2);
if(auto me=cast(SubExp)e) return doIt(me.e1)-doIt(me.e2);
if(auto me=cast(NSubExp)e){
auto r = doIt(me.e1)-doIt(me.e2);
cur.assertTrue(dLambda(dGeZ(r)));
return r;
}
if(auto me=cast(MulExp)e) return doIt(me.e1)*doIt(me.e2);
if(cast(DivExp)e||cast(IDivExp)e){
auto de=cast(ABinaryExp)e;
auto e1=doIt(de.e1);
auto e2=doIt(de.e2);
cur=cur.assertTrue(dLambda(dNeqZ(e2)));
return cast(IDivExp)e?dFloor(e1/e2):e1/e2;
}
if(auto me=cast(ModExp)e) return doIt(me.e1)%doIt(me.e2);
if(auto pe=cast(PowExp)e) return doIt(pe.e1)^^doIt(pe.e2);
if(auto ce=cast(CatExp)e) return doIt(ce.e1)~doIt(ce.e2);
if(auto ce=cast(BitOrExp)e) return dBitOr(doIt(ce.e1),doIt(ce.e2));
if(auto ce=cast(BitXorExp)e) return dBitXor(doIt(ce.e1),doIt(ce.e2));
if(auto ce=cast(BitAndExp)e) return dBitAnd(doIt(ce.e1),doIt(ce.e2));
if(auto ume=cast(UMinusExp)e) return -doIt(ume.e);
if(auto ume=cast(UNotExp)e) return dEqZ(doIt(ume.e));
if(auto ume=cast(UBitNotExp)e) return -doIt(ume.e)-1;
if(auto le=cast(LambdaExp)e){
if(le.fd.isNested){
return dDPContextFun(le.fd,buildContextFor!readLocal(le.fd,le.fd.scope_));
}
return dDPFun(le.fd);
}
if(auto ce=cast(CallExp)e){
auto id=cast(Identifier)ce.e;
auto fe=cast(FieldExp)ce.e;
DExpr thisExp=null;
if(fe){
id=fe.f;
thisExp=doIt(fe.e);
}
if(id){
if(auto fun=cast(FunctionDef)id.meaning){
auto arg=doIt(ce.arg); // TODO: allow temporaries within arguments
return cur.call(fun,thisExp,arg,id.scope_);
}
if(!fe && isBuiltIn(id)){
switch(id.name){
case "Marginal":
enforce(0,"TODO");
assert(0);
case "sampleFrom":
Expression[] args;
if(auto tpl=cast(TupleExp)ce.arg) args=tpl.e;
else args=[ce.arg];
assert(args.length);
auto getArg(){
return args[1..$].length==1?doIt(args[1]):dTuple(args[1..$].map!doIt.array);
}
auto literal=cast(LiteralExp)args[0];
assert(literal&&literal.lit.type==Tok!"``");
auto str=literal.lit.str;
import samplefrom;
static real toFloat(DExpr e){
if(auto q=cast(Dℚ)e) return toReal(q.c);
if(auto f=cast(DFloat)e) return f.c;
enforce(0,"parameters to sampling procedure must be constant");
assert(0);
}
final switch(getToken(str)) with(Token){
case exp:
assert(args[1..$].length==1);
return dE^^doIt(args[1]);
case log:
assert(args[1..$].length==1);
return dLog(doIt(args[1]));
case sin:
assert(args[1..$].length==1);
return dSin(doIt(args[1]));
case cos:
assert(args[1..$].length==1);
return dCos(doIt(args[1]));
case abs:
assert(args[1..$].length==1);
return dAbs(doIt(args[1]));
case floor:
assert(args[1..$].length==1);
return dFloor(doIt(args[1]));
case ceil:
assert(args[1..$].length==1);
return dCeil(doIt(args[1]));
case array:
assert(args[1..$].length==2);
return dArray(doIt(args[1]),dLambda(doIt(args[2]).incDeBruijnVar(1,0)));
case inferPrecondition:
return one; // TODO
case infer:
return cur.infer(getArg());
case errorPr:
return cur.distError(getArg());
case samplePrecondition:
return one; // TODO
case sample:
if(opt.noCheck){
assert(args[1..$].length==1);
return cur.distSample(getArg());
}else{
assert(args[1..$].length==2);
return cur.distSample(getArg()[0.dℚ].simplify(one));
}
case expectation:
if(opt.noCheck){
assert(args[1..$].length==1);
return cur.distExpectation(getArg());
}else{
assert(args[1..$].length==2);
return cur.distExpectation(getArg()[0.dℚ].simplify(one));
}
case uniformIntPrecondition: // (TODO: remove)
return one; // TODO
case uniformInt: // uniformInt
return cur.uniformInt(getArg());
case categoricalPrecondition:
return one; // TODO?
case categorical:
return cur.categorical(getArg());
case binomial:
assert(args[1..$].length==2);
DExpr arg=getArg();
auto n_=arg[0.dℚ], p=arg[1.dℚ].incDeBruijnVar(1,0);
auto n=n_.incDeBruijnVar(1,0);
return cur.categorical(dArray(n_+1,dLambda(dNChooseK(n,db1)*p^^db1*(1-p)^^(n-db1))).simplify(one));
case gauss:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Gaussian with --dp");
auto arg=dLambda(getArg()).simplify(one);
real μ,ν;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto gArgs=dApply(arg,k);
auto μCand=toFloat(gArgs[0.dℚ].simplify(one)), νCand=toFloat(gArgs[1.dℚ].simplify(one));
if(!hasResult){
μ=μCand, ν=νCand;
import std.math, std.mathspecial, std.random;
result=dFloat(sampleGauss(μ,ν));
hasResult=true;
}else enforce(μ==μCand && ν==νCand);
}
assert(hasResult);
return result;
case uniform:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Uniform with --dp");
auto arg=dLambda(getArg()).simplify(one);
real a,b;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto uArgs=dApply(arg,k);
auto aCand=toFloat(uArgs[0.dℚ].simplify(one)), bCand=toFloat(uArgs[1.dℚ].simplify(one));
if(!hasResult){
a=aCand, b=bCand;
result=dFloat(sampleUniform(a,b));
hasResult=true;
}else enforce(a==aCand && b==bCand);
}
assert(hasResult);
return result;
case laplace:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Laplace with --dp");
auto arg=dLambda(getArg()).simplify(one);
real μ,b;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto lArgs=dApply(arg,k);
auto μCand=toFloat(lArgs[0.dℚ].simplify(one)),bCand=toFloat(lArgs[1.dℚ].simplify(one));
if(!hasResult){
μ=μCand, b=bCand;
import std.math: log;
result=dFloat(sampleLaplace(μ,b));
hasResult=true;
}else enforce(μ==μCand && b==bCand);
}
assert(hasResult);
return result;
case rayleigh:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Rayleigh with --dp");
auto arg=dLambda(getArg()).simplify(one);
real ν;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto νCand=toFloat(dApply(arg,k).simplify(one));
if(!hasResult){
ν=νCand;
import std.math: log, sqrt;
result=dFloat(sampleRayleigh(ν));
hasResult=true;
}else enforce(ν==νCand);
}
assert(hasResult);
return result;
case pareto:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Pareto with --dp");
auto arg=dLambda(getArg()).simplify(one);
real a,b;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto pArgs=dApply(arg,k);
auto aCand=toFloat(pArgs[0.dℚ].simplify(one)),bCand=toFloat(pArgs[1.dℚ].simplify(one));
if(!hasResult){
a=aCand, b=bCand;
import std.math: log;
result=dFloat(samplePareto(a,b));
hasResult=true;
}else enforce(a==aCand && b==bCand);
}
assert(hasResult);
return result;
case gamma:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Gamma with --dp");
auto arg=dLambda(getArg()).simplify(one);
real α,β;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto gArgs=dApply(arg,k);
auto αCand=toFloat(gArgs[0.dℚ].simplify(one)),βCand=toFloat(gArgs[1.dℚ].simplify(one));
if(!hasResult){
α=αCand, β=βCand;
import std.math: log;
result=dFloat(sampleGamma(α,β));
hasResult=true;
}else enforce(α==αCand && β==βCand);
}
assert(hasResult);
return result;
case exponential:
enforce(opt.backend==InferenceMethod.simulate,"cannot sample from Exponential with --dp");
auto arg=dLambda(getArg()).simplify(one);
real λ;
bool hasResult;
DExpr result;
foreach(k,v;cur.state){
auto λCand=toFloat(dApply(arg,k).simplify(one));
if(!hasResult){
λ=λCand;
import std.math: log, sqrt;
result=dFloat(sampleExponential(λ));
hasResult=true;
}else enforce(λ==λCand);
}
assert(hasResult);
return result;
case flip, none:
static DDPDist[const(char)*] dists; // NOTE: it is actually important that identical strings with different addresses get different entries (parameters are substituted)
if(str.ptr !in dists){
auto dist=new Distribution();
auto info=analyzeSampleFrom(ce,new SimpleErrorHandler(),dist);
if(info.error) enforce(0,"TODO");
auto retVars=info.retVars,paramVars=info.paramVars,newDist=info.newDist;
foreach(i,pvar;paramVars){
auto expr=doIt(args[1+i]);
newDist=newDist.substitute(pvar,expr); // TODO: use substituteAll
}
dist.distribute(newDist);
auto tmp=dist.declareVar("`tmp");
dist.initialize(tmp,dTuple(cast(DExpr[])retVars.map!(v=>v.tmp).array),contextTy);
foreach(v;info.retVars) dist.marginalize(v.tmp);
dist.simplify();
auto smpl=distInit();
void gather(DExpr e,DExpr factor){
enforce(!cast(DInt)e,text("TODO: ",ce.e));
foreach(s;e.summands){
foreach(f;s.factors){
if(auto dd=cast(DDiscDelta)f){
assert(dd.var == tmp);
smpl.add(dRecord(["`value":retVars.length==1?dd.e[0.dℚ].simplify(one):dd.e]),(factor*s.withoutFactor(f)).substitute(tmp,dd.e).simplify(one));
}else if(auto sm=cast(DPlus)f){
gather(sm,factor*s.withoutFactor(f));
}
}
}
}
gather(dist.distribution,one);
dists[str.ptr]=dDPDist(smpl);
}
return cur.distSample(dists[str.ptr]);
}
case "Expectation":
auto arg=doIt(ce.arg);
return cur.expectation(dLambda(arg),hasFrame);
default:
assert(0, text("unsupported: ",id.name));
}
}
}
auto fun=doIt(ce.e), arg=doIt(ce.arg);
return cur.call(fun,arg);
}
if(auto idx=cast(IndexExp)e) return dIndex(doIt(idx.e),doIt(idx.a)); // TODO: bounds checking
if(auto sl=cast(SliceExp)e) return dSlice(doIt(sl.e),doIt(sl.l),doIt(sl.r)); // TODO: bounds checking
if(auto le=cast(LiteralExp)e){
if(le.lit.type==Tok!"0"){
auto n=le.lit.str.split(".");
if(n.length==1) n~="";
return (dℚ((n[0]~n[1]).ℤ)/(ℤ(10)^^n[1].length)).simplify(one);
}else if(le.lit.type==Tok!".0"){
import std.conv: to;
return dFloat(to!real(le.lit.str));
}
}