







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








(^1) If you are using Vista and Browse doesn’t work for you, but you don’t want to reinstall Oz, then instead of using Browse use
Show (or to see strings, System.showInfo). However, this is somewhat problematic because Show (and showInfo) only show you the instantaneous view of their argument. In essence there will be a race condition between other threads and the thread using Show. One way around this is to use the Wait operation in Oz (see Section 10.1 of “The Oz Base Environment” [DKS06]). For example, write {Wait X} {Show X} where the textbook has {Browse X}. In cases where you don’t have a single variable to wait on, you can introduce delays using the Delay procedure (see Section 10.3 of “The Oz Base Environment” [DKS06]). For example, use {Delay 5000} {Show Lst} where code in the textbook shows {Browse Lst}. You may have to adjust the timing argument to delay (which is measured in milliseconds) to fit the problem.
Thread and Dataflow Behavior Semantics
% $Id: FloatPredicates.oz,v 1.3 2007/10/23 02:14:21 leavens Exp leavens $ %% Some functions to do approximate equality of floating point numbers. %% AUTHOR: Gary T. Leavens
declare %% Return true iff the difference between X and Y %% is no larger than Epsilon fun {Within Epsilon X Y} {Abs X-Y} =< Epsilon end
%% Partly curried version of Within fun {WithinMaker Epsilon} fun {$ X Y} {Within Epsilon X Y} end end
%% Return true iff the corresponding lists are %% equal relative to the given predicate fun {CompareLists Pred Xs Ys} case Xs#Ys of nil#nil then true [] (X|Xr)#(Y|Yr) then {Pred X Y} andthen {CompareLists Pred Xr Yr} else false end end
%% Return true iff the lists are equal %% in the sense that the corresponging elements %% are equal to within Epsilon fun {WithinLists Epsilon Xs Ys} {CompareLists {WithinMaker Epsilon} Xs Ys} end
%% Return true iff the ratio of X-Y to Y is within Epsilon fun {Relative Epsilon X Y} {Abs X-Y} =< Epsilon*{Abs Y} end
%% Partly curried version of Relative fun {RelativeMaker Epsilon} fun {$ X Y} {Relative Epsilon X Y} end end
%% Return true iff the lists are equal %% in the sense that the corresponging elements %% are relatively equal to within Epsilon fun {RelativeLists Epsilon Xs Ys} {CompareLists {RelativeMaker Epsilon} Xs Ys} end
%% A useful tolerance for testing StandardTolerance = 1.0e~
%% A convenience for testing, relative equality with a fixed Epsilon ApproxEqual = {RelativeMaker StandardTolerance}
% $Id: FloatTesting.oz,v 1.5 2008/03/24 15:43:04 leavens Exp leavens $ % Testing for floating point numbers. % AUTHOR: Gary T. Leavens
\insert ’FloatPredicates.oz’ \insert ’TestingNoStop.oz’
declare %% TestMaker returns a procedure P such that {P Actual ’=’ Expected} %% is true if {FloatCompare Epsilon Actual Expected} (for Floats) %% or if {FloatListCompare Epsilon Actual Expected} (for lists of Floats) %% If so, print a message, otherwise throw an exception. fun {TestMaker FloatCompare FloatListCompare Epsilon} fun {Compare Actual Expected} if {IsFloat Actual} andthen {IsFloat Expected} then {FloatCompare Epsilon Actual Expected} elseif {IsList Actual} andthen {IsList Expected} then {FloatListCompare Epsilon Actual Expected} else false end end in proc {$ Actual Connective Expected} if {Compare Actual Expected} then {System.showInfo {Value.toVirtualString Actual 5 20}
else {System.showInfo ’TEST FAILURE: ’
} end end end
WithinTest = {TestMaker Within WithinLists StandardTolerance} RelativeTest = {TestMaker Relative RelativeLists StandardTolerance}
% $Id: EasyDiff.oz,v 1.3 2007/10/31 00:44:27 leavens Exp leavens $ declare fun {EasyDiff F X Delta} ({F (X+Delta)} - {F X}) / Delta end
% $Id: ConvergesToTest.oz,v 1.5 2007/10/31 00:45:52 leavens Exp leavens $ \insert ’ConvergesTo.oz’ \insert ’FloatTesting.oz’
fun lazy {Repeat X} X|{Repeat X} end
{StartTesting ’ConvergesTo...’} {WithinTest {ConvergesTo {Append [1.0 3.5 4.5] {Repeat 7.0}} {WithinMaker 1.01} } ’~=~’ 4.5} {WithinTest {ConvergesTo {Append [1.0 32.5 17.2346 10.474 8.29219 8.00515] {Repeat 8.0}} {WithinMaker 0.5} } ’~=~’ 8.00515}
% $Id: SquareRootTest.oz,v 1.2 2007/10/31 00:35:49 leavens Exp leavens $ \insert ’SquareRoot.oz’ \insert ’FloatTesting.oz’ {StartTesting ’SquareRoot...’} Millionth = 0. WithinMillionth = {TestMaker Within WithinLists Millionth} {WithinMillionth {SquareRoot 1.0 Millionth 2.0} ’~=~’ 1.41421356} {WithinMillionth {SquareRoot 1.0 Millionth 4.0} ’~=~’ 2.0} {WithinMillionth {SquareRoot 1.0 Millionth 64.0} ’~=~’ 8.0} {WithinMillionth {SquareRoot 1.0 Millionth 3.14159} ’~=~’ 1.7724531}
% $Id: DifferentiateTest.oz,v 1.2 2007/10/31 00:37:26 leavens Exp leavens $ \insert ’Differentiate.oz’ \insert ’FloatTesting.oz’ {StartTesting ’Differentiate...’} Millionth = 0. WithinMillionth = {TestMaker Within WithinLists Millionth} {WithinMillionth {Differentiate Millionth 500.0 fun {$ X} XX end 20.0} ’~=~’ 40.0} {WithinMillionth {Differentiate Millionth 100.0 fun {$ X} XX*X end 10.0} ’~=~’ 300.0}
Laziness Problems
% $Id: RequestCalcTest.oz,v 1.2 2008/03/23 16:38:27 leavens Exp leavens $ \insert ’TestingNoStop.oz’ \insert ’RequestCalc.oz’
% Testing declare Z fun lazy {SGen Y} {Delay 5000} Y|{SGen Y+1} end Z = {SGen 0} {Browse Z}
{Delay 2000} {RequestCalc Z} {Browse requestin} {Delay 2000} {RequestCalc Z.2} {Browse request2in}
References