% $Id: ReducerTest.oz,v 1.1441 2009/01/11 19:50:34 leavens Exp leavens $ % Tests for the Reducer % AUTHOR: Gary T. Leavens \insert 'ReducerPrinting.oz' \insert 'TestingNoStop.oz' %% from the course library {StartTesting 'Reducer'} %%% Initial tests declare Env0 = {InitEnv} Store0 = {EmptyStore} {TestRed config(skipStmt#Env0|nil Store0) 'skip' config(nil Store0)} {TestRed config(seqStmt(skipStmt|skipStmt|nil)#Env0|nil Store0) 'sequence' config(skipStmt#Env0|skipStmt#Env0|nil Store0)} {TestRed config(localStmt("X" assignStmt("X" numExp(3)))#Env0|nil Store0) 'local' local Loc#Store2 = {AllocStore Store0} in config(assignStmt("X" numExp(3))#{ExtendEnv Env0 "X" Loc}|nil Store2) end} %% Below records what I did to debug a failure in the above, %% which ended up localizing it to EqualStore, allowing me to debug that... %% % local % C#RN = {Reduce1 config(localStmt("X" assignStmt("X" numExp(3)))#Env0|nil Store0)} % (S#E)|_ = {StackOfConfig C} % Loc#Store2 = {AllocStore Store0} % in % {Test {EqualStore {StoreOfConfig C} Store2} '==' true} % {Test {EqualEnv E {ExtendEnv Env0 "X" Loc}} '==' true} % end {TestRed config(localStmt("Y" localStmt("X" seqStmt([assignStmt("X" varIdExp("Y")) assignStmt("X" numExp(3))]))) #Env0|nil Store0) 'local' local LocY#StoreY = {AllocStore Store0} in config(localStmt("X" seqStmt([assignStmt("X" varIdExp("Y")) assignStmt("X" numExp(3))])) #{ExtendEnv Env0 "Y" LocY}|nil StoreY) end } local LocY#StoreY = {AllocStore Store0} in {TestRed config(localStmt("X" seqStmt([assignStmt("X" varIdExp("Y")) assignStmt("X" numExp(3))])) #{ExtendEnv Env0 "Y" LocY}|nil StoreY) 'local' local LocX#StoreX = {AllocStore StoreY} in config(seqStmt([assignStmt("X" varIdExp("Y")) assignStmt("X" numExp(3))]) #{ExtendEnv {ExtendEnv Env0 "Y" LocY} "X" LocX}|nil StoreX) end } end local LocY#StoreY = {AllocStore Store0} LocX#StoreX = {AllocStore StoreY} EnvXY = {ExtendEnv {ExtendEnv Env0 "Y" LocY} "X" LocX} in {TestRed config(seqStmt([assignStmt("X" varIdExp("Y")) assignStmt("X" numExp(3))]) #EnvXY|nil StoreX) 'sequence' config((assignStmt("X" varIdExp("Y"))#EnvXY) |(assignStmt("X" numExp(3))#EnvXY)|nil StoreX) } end local LocY#StoreY = {AllocStore Store0} LocX#StoreX = {AllocStore StoreY} EnvXY = {ExtendEnv {ExtendEnv Env0 "Y" LocY} "X" LocX} StoreXY = {UnifyLocs StoreX LocX LocY} in {TestRed config((assignStmt("X" varIdExp("Y"))#EnvXY) |(assignStmt("X" numExp(3))#EnvXY)|nil StoreX) 'var-var binding' config((assignStmt("X" numExp(3))#EnvXY)|nil StoreXY) } end local LocY#StoreY = {AllocStore Store0} LocX#StoreX = {AllocStore StoreY} EnvXY = {ExtendEnv {ExtendEnv Env0 "Y" LocY} "X" LocX} StoreXY = {UnifyLocs StoreX LocX LocY} StoreX3Y3 = {UnifyLocVal StoreXY LocX num(3)} in {TestRed config((assignStmt("X" numExp(3))#EnvXY)|nil StoreXY) 'value-creation' config(nil StoreX3Y3) } end %%%%%%%%%%%%%%% Statement Defs for what follows %%%%%%%%%% InnerStmt01 = assignStmt("Th" numExp(3)) InnerStmt02 = assignStmt("Rec" recordExp(atomExp(foo) [colonFld(val varIdExp("Th"))])) InnerStmt03 = caseStmt(varIdExp("Rec") recordPat(foo [colonFld(val varIdExp("Z"))]) assignStmt("Res" recordExp(atomExp(bar) [colonFld(baz varIdExp("Z"))])) assignStmt("Res" procExp([varIdPat("A") varIdPat("R")] assignStmt("A" varIdExp("R"))))) InnerStmt0 = seqStmt([InnerStmt01 InnerStmt02 InnerStmt03]) {TestRed config(localStmt("Res" localStmt("Rec" localStmt("Th" InnerStmt0))) #Env0|nil Store0) 'local' local LocRes#StoreRes = {AllocStore Store0} in config(localStmt("Rec" localStmt("Th" InnerStmt0)) #{ExtendEnv Env0 "Res" LocRes}|nil StoreRes) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} in config(localStmt("Rec" localStmt("Th" InnerStmt0)) #{ExtendEnv Env0 "Res" LocRes}|nil StoreRes) end 'local' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} in config(localStmt("Th" InnerStmt0) #{ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec}|nil StoreRec) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} in config(localStmt("Th" InnerStmt0) #{ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec}|nil StoreRec) end 'local' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} in config(InnerStmt0#EnvTh|nil StoreTh) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} in config(seqStmt([InnerStmt01 InnerStmt02 InnerStmt03])#EnvTh|nil StoreTh) end 'sequence' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} in config(InnerStmt01#EnvTh|InnerStmt02#EnvTh|InnerStmt03#EnvTh|nil StoreTh) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} in config(assignStmt("Th" numExp(3))#EnvTh|InnerStmt02#EnvTh|InnerStmt03#EnvTh|nil StoreTh) end 'value-creation' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} in config(InnerStmt02#EnvTh|InnerStmt03#EnvTh|nil StoreTh3) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} in config(assignStmt("Rec" recordExp(atomExp(foo) [colonFld(val varIdExp("Th"))])) #EnvTh|InnerStmt03#EnvTh|nil StoreTh3) end 'value-creation' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(foo(val: LocTh))} in config(InnerStmt03#EnvTh|nil Store3Rec) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(foo(val: LocTh))} in config(caseStmt(varIdExp("Rec") recordPat(foo [colonFld(val varIdExp("Z"))]) assignStmt("Res" recordExp(atomExp(bar) [colonFld(baz varIdExp("Z"))])) assignStmt("Res" procExp([varIdPat("A") varIdPat("R")] assignStmt("A" varIdExp("R"))))) #EnvTh|nil Store3Rec) end 'case-match' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(foo(val: LocTh))} EnvMatch = {ExtendEnvList EnvTh ["Z"] [LocTh]} in config(assignStmt("Res" recordExp(atomExp(bar) [colonFld(baz varIdExp("Z"))])) #EnvMatch|nil Store3Rec) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(foo(val: LocTh))} EnvMatch = {ExtendEnvList EnvTh ["Z"] [LocTh]} in config(assignStmt("Res" recordExp(atomExp(bar) [colonFld(baz varIdExp("Z"))])) #EnvMatch|nil Store3Rec) end 'value-creation' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(foo(val: LocTh))} StoreResFinal = {UnifyLocVal Store3Rec LocRes record(bar(baz: LocTh))} in config(nil StoreResFinal) end } %%% An alternative with goes into the else part {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(blech(val: LocTh))} in config(caseStmt(varIdExp("Rec") recordPat(foo [colonFld(val varIdExp("Z"))]) assignStmt("Res" recordExp(atomExp(bar) [colonFld(baz varIdExp("Z"))])) assignStmt("Res" procExp([varIdPat("A") varIdPat("R")] assignStmt("A" varIdExp("R"))))) #EnvTh|nil Store3Rec) end 'case-else' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(blech(val: LocTh))} in config(assignStmt("Res" procExp([varIdPat("A") varIdPat("R")] assignStmt("A" varIdExp("R")))) #EnvTh|nil Store3Rec) end } {TestRed local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(blech(val: LocTh))} in config(assignStmt("Res" procExp([varIdPat("A") varIdPat("R")] assignStmt("A" varIdExp("R")))) #EnvTh|nil Store3Rec) end 'value-creation' local LocRes#StoreRes = {AllocStore Store0} LocRec#StoreRec = {AllocStore StoreRes} LocTh#StoreTh = {AllocStore StoreRec} EnvTh = {ExtendEnv {ExtendEnv {ExtendEnv Env0 "Res" LocRes} "Rec" LocRec} "Th" LocTh} StoreTh3 = {UnifyLocVal StoreTh LocTh num(3)} Store3Rec = {UnifyLocVal StoreTh3 LocRec record(blech(val: LocTh))} StoreClos = {UnifyLocVal Store3Rec LocRes closure(["A" "R"] assignStmt("A" varIdExp("R")) EnvTh)} in config(nil StoreClos) end } %%%%%%%%%%%%%%%%%%%%% Reducer test %%%%%%%%%%%%%%% {ShowReductionsFor 'Same As Above' localStmt("Res" localStmt("Rec" localStmt("Th" InnerStmt0)))} InnerApply0 = assignStmt("Th" numExp(3)) InnerApply1 = assignStmt("Id" procExp([varIdPat("X") varIdPat("R")] assignStmt("R" varIdExp("X")))) InnerApply2 = applyStmt(varIdExp("Id") [varIdExp("Th") varIdExp("Res")]) InnerApply = seqStmt([InnerApply0 InnerApply1 InnerApply2]) {ShowReductionsFor 'An Application' localStmt("Res" localStmt("Id" localStmt("Th" InnerApply)))} InnerIf0 = assignStmt("X" boolExp(true)) InnerIf13 = assignStmt("Res" numExp(3)) InnerIf14 = assignStmt("Res" numExp(4)) InnerIf1 = ifStmt(varIdExp("X") InnerIf13 InnerIf14) InnerIf = seqStmt([InnerIf0 InnerIf1]) {ShowReductionsFor 'With If True' localStmt("Res" localStmt("X" InnerIf))} InnerIfF0 = assignStmt("X" boolExp(false)) InnerIfF13 = assignStmt("Res" numExp(3)) InnerIfF14 = assignStmt("Res" numExp(4)) InnerIfF1 = ifStmt(varIdExp("X") InnerIfF13 InnerIfF14) InnerIfF = seqStmt([InnerIfF0 InnerIfF1]) {ShowReductionsFor 'With If False' localStmt("Res" localStmt("X" InnerIfF))} {ShowReductionsFor 'Locals and Assignments from Notes' localStmt("R" localStmt("X" seqStmt([assignStmt("X" numExp(2)) assignStmt("R" varIdExp("X"))])))} NotesS7 = ifStmt(varIdExp("Z") skipStmt assignStmt("Z" varIdExp("X"))) NotesS6 = localStmt("Z" seqStmt([applyStmt(varIdExp("Y") [varIdExp("Z")]) NotesS7])) NotesS5 = assignStmt("X" boolExp(false)) NotesS4 = localStmt("X" seqStmt([NotesS5 NotesS6])) NotesS3 = assignStmt("X" boolExp(true)) NotesS2 = assignStmt("Y" procExp([varIdPat("R")] assignStmt("R" varIdExp("X")))) NotesS1 = localStmt("Y" seqStmt([NotesS2 NotesS3 NotesS4])) NotesS0 = localStmt("X" NotesS1) {ShowReductionsFor 'Procedure Application with If from Notes' NotesS0} % local Res in % local K in % K = proc {$ X ?R} R=proc {$ Y ?Z} Z=X end end % local F in % local Three in % Three = 3 % {K Three F} % local Four in % Four = 4 % {F Four Res} % end % end % end % end % end CurriedProc = procExp([varIdPat("X") varIdPat("R")] assignStmt("R" procExp([varIdPat("Y") varIdPat("Z")] assignStmt("Z" varIdExp("X"))))) DefiningFour = localStmt("Four" seqStmt([ assignStmt("Four" numExp(4)) applyStmt(varIdExp("F") [varIdExp("Four") varIdExp("Res")]) ])) DefiningF = localStmt("F" localStmt("Three" seqStmt([assignStmt("Three" numExp(3)) applyStmt(varIdExp("K") [varIdExp("Three") varIdExp("F")]) DefiningFour ]))) CurriedAppProg = localStmt("Res" localStmt("K" seqStmt([assignStmt("K" CurriedProc) DefiningF]))) {ShowReductionsFor 'Curried Function Application from Notes' CurriedAppProg } % local Res in % local Length in % Length = proc {$ Lst R} % case Lst of % '|'(1:_ 2:T) then % local Temp in % {Length T Temp} % local One in % One = 1 % {Number.'+' One Temp R} % end % end % else R = 0 % end % end % local Three in % Three = 3 % local Nil in % Nil = nil % local List3 in % List3 = '|'(1: Three 2:Nil) % local MyList in % MyList = '|'(1: Three 2:List3) % {Length MyList Res} % {System.showInfo Res} % end % end % end % end % end % end LengthProc = procExp([varIdPat("Lst") varIdPat("R")] caseStmt(varIdExp("Lst") recordPat('|' [colonFld(1 varIdExp("_")) colonFld(2 varIdExp("T"))]) localStmt("Temp" seqStmt([applyStmt(varIdExp("Length") [varIdExp("T") varIdExp("Temp")]) localStmt("One" seqStmt([assignStmt("One" numExp(1)) applyStmt(varIdExp("Number.+") [varIdExp("One") varIdExp("Temp") varIdExp("R")])])) ])) assignStmt("R" numExp(0)))) List33 = recordExp(atomExp('|') [colonFld(1 varIdExp("Three")) colonFld(2 varIdExp("List3")) ]) MyListLocal = localStmt("MyList" seqStmt([assignStmt("MyList" List33) applyStmt(varIdExp("Length") [varIdExp("MyList") varIdExp("Res")]) ])) List3 = localStmt("List3" seqStmt([assignStmt("List3" recordExp(atomExp('|') [colonFld(1 varIdExp("Three")) colonFld(2 varIdExp("Nil")) ])) MyListLocal])) LengthProcApp = localStmt("Three" seqStmt([assignStmt("Three" numExp(3)) localStmt("Nil" seqStmt([assignStmt("Nil" atomExp(nil)) List3])) ])) ShowStmt = applyStmt(varIdExp("System.showInfo") [varIdExp("Res")]) FullRecurApp = localStmt("Res" localStmt("Length" seqStmt([assignStmt("Length" LengthProc) LengthProcApp ShowStmt ]))) {ShowStdReductionsFor 'Length Procedure, fully Recursive' FullRecurApp } {StartTesting 'done'}