#include get.cam #include ret.cam ;-------------------------------------------------------------------- ; Rewrite functions ; ; 0 + x = x <--> ; S(x) + y = S(x + y) <--> ; 0 * x = 0 <--> ; S(x) * y = x * y + y ; ;-------------------------------------------------------------------- ; ; 0 + x -> x ; FUNC RR1(x:X):X RETURN if xMatch(x,O("+",),'x>)) then RX(x) else x; ; ; S(x) + y -> S(x + y) ; FUNC RR2(x:X):X RETURN if xMatch(x,O("+",),'y>)) then O("S",) else x; ; ; 0 * x -> 0 ; FUNC RR3(x:X):X RETURN if xMatch(x,O("*",),'x>)) then O("0",<>) else x; ; ; S(x) * y -> x * y + y ; FUNC RR4(x:X):X RETURN if xMatch(x,O("*",),'y>)) then O("+",),RX(x)>) else x; FUNC rfPSTARTTRS(x:X,n:INT) PRE is-X(x) ;STATE progn(gpSETTINGS(P(15,30,0,25,60,1,false,3,true,1,39)), ;STATE progn(gpSETTINGS(P(15,50,0,50,60,1,false,2,true,1,39)), STATE progn(gpSETTINGS(P(5,50,3,50,60,0,false,2,true,2,39)), gpSHOWSETTINGS(), gpPSTART({1,2,3,4}), rfEVAL(x), princ("> Best(",0,"): ",rfBESTVAL(),"%\n> thinking..."), foreach(i,inseg(n), gpSELECT(),gpCROSSOVER(),gpMUTATION(),rfEVAL(x), princ(" best (",i,"): ",rfBESTVAL(),"%\n> thinking...")), ; rfSHOWBEST(x)); ; rfSHOWREIF(x)); rfSHOWRESULT(x)); FUNC rfBEST():I->R RETURN [ k -> G[k] | k <- dom(G) : gpSEL(k,2) == rfBESTVAL() ]; FUNC rfBESTVAL():INT PRE not(gpEMPTY()) RETURN max-orio(0,{ gpSEL(x,2) | x <- dom(G) }); FUNC rfEVAL(x:X) STATE G <- rcprob(_reify_(x,G)); _reify_(e,s) = if (s == []) then [] else let (x = choice(dom(s)), cx = gpSEL(x,1), rx = if (GR(GV) == 2) then CONC(cx) else cx, fx = rfEVALTRS(ApplyFirstMatch(e,rx)), sx = gpSEL(x,3)) in [ x -> R(cx,fx,sx,0,0) ] + _reify_(e,s\{x}); FUNC rfEVALTRS(x:X):INT RETURN pdiv(1,xComplex(x)); FUNC rfSHOWRESULT(x:X) RETURN let (b = rfBEST(), k = choice(dom(b)), c = B1(b[k])) ; in xString(erApplyFinalRule(ApplyFirstMatch(x,c))); in if (GR(GV) == 2) then xString(ApplyFirstMatch(x,CONC(c))) else xString(ApplyFirstMatch(x,c)); FUNC rfSHOWREIF(x:X) RETURN let (b = rfBEST(), k = choice(dom(b)), c = B1(b[k])) in if (GR(GV) == 2) then _showreif_(x,CONC(c)) else _showreif_(x,c); _showreif_(x,c) = if (c == <>) then "" else let (h = head(c), t = tail(c), r = FirstMatch(x,h)) in do(if (x != r) then princ("\n> applying ",h,"... ",xString(r)), _showreif_(r,t)); m<-O("+",),O("*",),O("S",)>)>)>),O("*",),O("0",<>)>)>);