RealsImpl.mesa
Last Edited by: Arnon, July 19, 1985 2:54:46 pm PDT
DIRECTORY
Rope,
IO,
Basics,
Atom,
Convert,
AlgebraClasses,
Points,
Reals;
RealsImpl: CEDAR PROGRAM
IMPORTS IO, Convert
EXPORTS Reals
= BEGIN OPEN Reals, Convert, AC: AlgebraClasses, PTS: Points;
Types and Constants
RealsError: PUBLIC SIGNAL [reason: ATOM ← $Unspecified] = CODE;
bitsPerWord: CARDINAL = Basics.bitsPerWord;
CARD: TYPE = LONG CARDINAL;
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
RealOne: Real ← FromREAL[1.0];
RealZero: Real ← FromREAL[0.0];
Variables
ClassPrintName: AC.PrintNameProc = {
RETURN["Reals"];
};
ClassLegalFirstChar: AC.LegalFirstCharOp = {
SELECT char FROM
= '., IN ['0..'9] => RETURN[TRUE];
ENDCASE;
RETURN[FALSE];
};
ClassRead: AC.ReadOp = {
RETURN[Read[in]];
};
ClassFromRope: AC.FromRopeOp = {
stream: IO.STREAMIO.RIS[in];
RETURN[ ClassRead[stream] ];
};
ClassToRope: AC.ToRopeOp = {
RETURN[ ToRope[NARROW[in, Real] ] ]
};
ClassWrite: AC.WriteOp = {
Write[stream, NARROW[in, Real] ]
};
ClassCharacteristic: AC.StructureRankOp = {
RETURN[ 0 ]
};
ClassAdd: AC.BinaryOp = {
RETURN[ Add[NARROW[firstArg, Real], NARROW[secondArg, Real] ] ]
};
ClassNegate: AC.UnaryOp = {
RETURN[ Negate[ NARROW[arg, Real] ] ];
};
ClassSubtract: AC.BinaryOp = {
RETURN[ Subtract[NARROW[firstArg, Real], NARROW[secondArg, Real] ] ]
};
ClassZero: AC.NullaryOp = {
RETURN[ RealZero ]
};
ClassMultiply: AC.BinaryOp = {
RETURN[ Multiply[NARROW[firstArg, Real], NARROW[secondArg, Real] ] ]
};
ClassInvert: AC.UnaryOp = {
RETURN[ Invert[NARROW[arg, Real] ] ]
};
ClassDivide: AC.BinaryOp = {
RETURN[ Divide[NARROW[firstArg, Real], NARROW[secondArg, Real] ] ]
};
ClassOne: AC.NullaryOp = {
RETURN[ RealOne ]
};
ClassEqual: AC.EqualityOp = {
RETURN[ Compare[NARROW[firstArg, Real], NARROW[secondArg, Real] ] = equal ]
};
ClassSign: AC.CompareToZeroOp = {
RETURN[ Sign[NARROW[arg, Real] ] ]
};
ClassAbs: AC.UnaryOp = {
RETURN[ Abs[NARROW[arg, Real] ] ]
};
ClassCompare: AC.BinaryCompareOp = {
RETURN[ Compare[NARROW[firstArg, Real], NARROW[secondArg, Real] ] ]
};
RealClass: AC.StructureClass ← NEW[AC.StructureClassRec ← [
flavor: field,
printName: ClassPrintName,
characteristic: ClassCharacteristic,
legalFirstChar: ClassLegalFirstChar,
read: ClassRead,
fromRope: ClassFromRope,
toRope: ClassToRope,
write: ClassWrite,
add: ClassAdd,
negate: ClassNegate,
subtract: ClassSubtract,
zero: ClassZero,
multiply: ClassMultiply,
commutative: TRUE,
invert: ClassInvert,
divide: ClassDivide,
one: ClassOne,
equal: ClassEqual,
ordered: TRUE,
sign: ClassSign,
abs: ClassAbs,
compare: ClassCompare,
completeField: TRUE,
realField: TRUE,
realClosedField: TRUE,
algebraicallyClosedField: FALSE,
propList: NIL
] ];
Reals: PUBLIC AC.Structure ← NEW[AC.StructureRec ← [
class: RealClass,
instanceData: NIL
] ];
I/O and Conversion
Read: PUBLIC PROC [in: IO.STREAM] RETURNS [out: Real] ~ {
RETURN[NEW[RealRep ← [IO.GetReal[in] ] ] ];
};
FromRope: PUBLIC PROC [rope: ROPE] RETURNS [Real] = {
RETURN[NEW[RealRep ← [Convert.RealFromRope[rope] ] ] ];
};
ToRope: PUBLIC PROC [real: Real] RETURNS [out: ROPE] = {
RETURN[ Convert.RopeFromReal[real.val] ];
};
Write: PUBLIC PROC [stream: IO.STREAM, in: Real] = {
IO.PutRope[ stream, ToRope[in] ]
};
FromREAL: PUBLIC PROC [real: REAL] RETURNS [Real] = {
RETURN[NEW[RealRep ← [real] ] ];
};
ToREAL: PUBLIC PROC [real: Real] RETURNS [REAL] = {
RETURN[real.val];
};
Arithmetic
Add: PUBLIC PROC [firstArg, secondArg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [firstArg.val + secondArg.val]]];
};
Negate: PUBLIC PROC [arg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [ - arg.val]]];
};
Subtract: PUBLIC PROC [firstArg, secondArg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [firstArg.val - secondArg.val]]];
};
Multiply: PUBLIC PROC [firstArg, secondArg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [firstArg.val * secondArg.val]]];
};
Invert: PUBLIC PROC [arg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [ 1.0 / arg.val]]];
};
Divide: PUBLIC PROC [firstArg, secondArg: Real] RETURNS [result: Real]~ {
RETURN [NEW[RealRep ← [firstArg.val / secondArg.val]]];
};
Comparison
Sign: PUBLIC PROC [arg: Real] RETURNS [Basics.Comparison] = {
SELECT arg.val FROM
< 0.0 => RETURN[less];
= 0.0 => RETURN[equal];
ENDCASE => RETURN[greater];
};
Abs: PUBLIC PROC [arg: Real] RETURNS [result: Real] ~ {
RETURN [NEW[RealRep ← [ ABS[arg.val] ]]];
};
Compare: PUBLIC PROC [firstArg, secondArg: Real] RETURNS [Basics.Comparison] ~ {
SELECT firstArg.val FROM
< secondArg.val => RETURN[less];
= secondArg.val => RETURN[equal];
ENDCASE => RETURN[greater];
};
END.