Conversion and IO
Recast:
PUBLIC AC.BinaryOp = {
Args are a StructureElement and a Structure
thisVectorStructure: Object ← secondArg;
thisVectorStructureData: VectorStructureData ← NARROW[thisVectorStructure.data];
thisStructureCoordinateStructure: Object ← thisVectorStructureData.coordinateStructure;
thisStructureDimension: NAT ← thisVectorStructureData.dimension;
canRecastMethod: Method ← AC.LookupMethodInStructure[$canRecast, thisStructureCoordinateStructure];
recastMethod: Method ← AC.LookupMethodInStructure[$recast, thisStructureCoordinateStructure];
flag: BOOL;
IF AC.StructureEqual[firstArg.class, secondArg] THEN RETURN[firstArg]; -- nothing to do
See if can recast firstArg into thisStructureCoordinateStructure
flag ← AC.ApplyPredNoLkpNoRecast[canRecastMethod,LIST[firstArg.class, thisStructureCoordinateStructure] ];
IF flag
THEN {
recastElement: Object ← AC.ApplyNoLkpNoRecastObject[recastMethod, LIST[firstArg, thisStructureCoordinateStructure] ];
RETURN[ ImbedScalar[recastElement, thisVectorStructure] ];
};
If firstArg is a Vector, check if can recast its coordinateStructure into thisStructureCoordinateStructure, and if has right dimension; if so, recast all its elements.
IF
AC.LookupMethodInStructure[$vectorStructure, firstArg.class]#
NIL
THEN {
inputVectorStructure: Object ← firstArg.class;
inputVectorStructureData: VectorStructureData ← NARROW[inputVectorStructure.data];
inputStructureCoordinateStructure: Object ← inputVectorStructureData.coordinateStructure;
inputStructureDimension: NAT ← inputVectorStructureData.dimension;
argData: VectorData ← NARROW[firstArg.data];
resultData: VectorData;
IF thisStructureDimension#inputStructureDimension THEN RETURN[NIL];
flag ← AC.ApplyPredNoLkpNoRecast[canRecastMethod,LIST[inputStructureCoordinateStructure, thisStructureCoordinateStructure] ];
IF NOT flag THEN RETURN[NIL]; -- give up
resultData ← NEW[VectorDataRec[argData.dimensionPlus1 - 1] ];
FOR i:
NAT
IN [1..argData.dimensionPlus1)
DO
resultData[i] ← AC.ApplyNoLkpNoRecastObject[recastMethod, LIST[argData[i], thisStructureCoordinateStructure] ];
ENDLOOP;
RETURN[
NEW[
AC.ObjectRec ← [
flavor: StructureElement,
class: thisVectorStructure,
data: resultData
] ] ];
};
Can't do it
RETURN[NIL];
};
CanRecast:
PUBLIC AC.BinaryPredicate = {
Args are either [Structure, Structure] or [StructureElement, Structure]
thisVectorStructure: Object ← secondArg;
thisVectorStructureData: VectorStructureData ← NARROW[thisVectorStructure.data];
thisStructureCoordinateStructure: Object ← thisVectorStructureData.coordinateStructure;
thisStructureDimension: NAT ← thisVectorStructureData.dimension;
canRecastMethod: Method ← AC.LookupMethodInStructure[$canRecast, thisStructureCoordinateStructure];
flag: BOOL;
firstArgStructure: Object ← IF firstArg.flavor = StructureElement THEN firstArg.class ELSE IF firstArg.flavor = Structure THEN firstArg ELSE ERROR;
IF AC.StructureEqual[firstArgStructure, thisVectorStructure] THEN RETURN[TRUE];
flag ← AC.ApplyPredNoLkpNoRecast[canRecastMethod,LIST[firstArgStructure, thisStructureCoordinateStructure] ];
IF flag THEN RETURN[TRUE];
IF
AC.LookupMethodInStructure[$vectorStructure, firstArgStructure]#
NIL
THEN {
inputVectorStructure: Object ← firstArgStructure;
inputVectorStructureData: VectorStructureData ← NARROW[inputVectorStructure.data];
inputStructureCoordinateStructure: Object ← inputVectorStructureData.coordinateStructure;
inputStructureDimension: NAT ← inputVectorStructureData.dimension;
IF thisStructureDimension#inputStructureDimension THEN RETURN[FALSE];
flag ← AC.ApplyPredNoLkpNoRecast[canRecastMethod,LIST[inputStructureCoordinateStructure, thisStructureCoordinateStructure] ];
RETURN[flag];
};
RETURN[FALSE];
};
ToExpr:
PUBLIC AC.ToExprOp = {
vectorData: VectorData ← NARROW[in.data];
vectorStructureData: VectorStructureData ← NARROW[in.class.data];
size: CARDINAL ← vectorData.dimensionPlus1 - 1;
outColumn: LIST OF MathExpr.EXPR ← NIL;
method: Method ← AC.LookupMethodInStructure[$toExpr, vectorStructureData.coordinateStructure];
FOR j:
NAT
DECREASING
IN [1..size]
DO
outColumn ← CONS[NARROW[AC.ApplyNoLkpNoRecastRef[method, LIST[vectorData[j]] ]] , outColumn];
ENDLOOP;
out ← MathConstructors.MakeVector[size, outColumn, vectorStructureData.row]; -- row (not column) vector
};
LegalFirstChar:
PUBLIC AC.LegalFirstCharOp = {
SELECT char
FROM
'[ => RETURN[TRUE];
ENDCASE;
RETURN[FALSE];
};
Read:
PUBLIC
AC.ReadOp ~ {
structureData: VectorStructureData ← NARROW[structure.data];
coordinateStructure: AC.Object ← structureData.coordinateStructure;
readMethod: AC.Method ← AC.LookupMethodInStructure[$read, coordinateStructure];
puncChar: CHAR;
nextElement: AC.Object;
length: NAT ← 0;
list, listTail: LIST OF AC.Object ← NIL;
outData: VectorData;
[]← in.SkipWhitespace[];
puncChar ← in.GetChar[];
[]← in.SkipWhitespace[];
IF puncChar # '[ THEN ERROR SyntaxError[$LeftParenExpected];
[]← in.SkipWhitespace[];
puncChar ← in.PeekChar[];
IF puncChar = '] THEN puncChar ← in.GetChar[];
WHILE puncChar # ']
DO
nextElement ← AC.ApplyReadMethod[readMethod, in, coordinateStructure];
length ← length + 1;
[]← in.SkipWhitespace[];
IF list=
NIL
THEN list ← listTail ←
LIST[nextElement]
ELSE
{ listTail.rest ← LIST[nextElement]; listTail ← listTail.rest };
puncChar ← in.GetChar[];
[]← in.SkipWhitespace[];
IF puncChar # ']
THEN
IF puncChar # ', THEN ERROR SyntaxError[$CommaExpected];
ENDLOOP;
outData ← NEW[VectorDataRec[length] ];
FOR i:
NAT
IN [1..length]
DO
outData[i] ← list.first;
list ← list.rest;
ENDLOOP;
out ← NEW[AC.ObjectRec ← [flavor: StructureElement, class: structure, data: outData] ];
};
FromRope:
PUBLIC
AC.FromRopeOp ~ {
out ← Read[IO.RIS[in], structure];
};
ToRope:
PUBLIC
AC.ToRopeOp ~ {
vectorStructureData: VectorStructureData ← NARROW[in.class.data];
coordinateStructure: AC.Object ← vectorStructureData.coordinateStructure;
toRopeMethod: AC.Method ← AC.LookupMethodInStructure[$toRope, coordinateStructure];
inData: VectorData ← NARROW[in.data];
out ← "[ ";
FOR i:
NAT
IN [1..inData.dimensionPlus1)
DO
out ← Rope.Concat[ out, NARROW[AC.ApplyNoLkpNoRecastRef[toRopeMethod, LIST[inData[i] ] ] ] ];
IF i < inData.dimensionPlus1-1 THEN out ← Rope.Concat[out,", "];
ENDLOOP;
out ← Rope.Concat[ out, " ]" ];
};
Write:
PUBLIC AC.WriteOp ~ {
stream.PutRope[ ToRope[in] ]
};
Start Code
vectorsClass:
AC.Object ←
AC.MakeClass["VectorsClass",
NIL,
NIL];
vectorsOverGroupClass:
AC.Object ←
AC.MakeClass["VectorsOverGroupClass", vectorsClass,
NIL];
subclass[vectorsClass]
vectorsOverRingClass:
AC.Object ←
AC.MakeClass["VectorsOverRingClass", vectorsOverGroupClass,
NIL];
subclass[vectorsOverGroupClass]
vectorsOverFieldClass:
AC.Object ←
AC.MakeClass["VectorsOverFieldClass", vectorsOverRingClass,
NIL];
subclass[vectorsOverRingClass]
setCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← set], NIL, NIL];
groupCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← group], NIL, NIL];
ringCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← ring], NIL, NIL];
fieldCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← field], NIL, NIL];
vectorSpaceCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← vectorSpace], NIL, NIL];
vectorStructureMethod: Method ← AC.MakeMethod[Value, FALSE, NIL, NIL, "vectorStructure"];
matrixStructureMethod: Method ← AC.MakeMethod[Value, FALSE, NIL, NIL, "matrixStructure"];
shortPrintNameMethod: Method ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← ShortPrintName], NIL, "shortPrintName"];
recastMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Recast], NIL, "recast"];
canRecastMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← CanRecast], NIL, "canRecast"];
toExprMethod: Method ← AC.MakeMethod[ToExprOp, FALSE, NEW[AC.ToExprOp ← ToExpr], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "toExpr"];
legalFirstCharMethod: Method ← AC.MakeMethod[LegalFirstCharOp, FALSE, NEW[AC.LegalFirstCharOp ← LegalFirstChar], NIL, "legalFirstChar"];
readMethod: Method ← AC.MakeMethod[ReadOp, FALSE, NEW[AC.ReadOp ← Read], NIL, "read"];
fromRopeMethod: Method ← AC.MakeMethod[FromRopeOp, TRUE, NEW[AC.FromRopeOp ← FromRope], NIL, "fromRope"];
toRopeMethod: Method ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← ToRope], NIL, "toRope"];
parenMethod: Method ← AC.MakeMethod[UnaryOp, FALSE, NEW[AC.UnaryOp ← AC.Copy], NIL, "paren"];
imbedScalarMethod: Method ← AC.MakeMethod[UnaryImbedOp, FALSE, NEW[AC.UnaryImbedOp ← ImbedScalar], NIL, "imbedScalar"];
vectorMethod: Method ← AC.MakeMethod[ListImbedOp, FALSE, NEW[AC.ListImbedOp ← MakeVector], NIL, "vector"];
coordinateMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Coordinate], NEW[AC.UnaryToListOp ← ObjectAndIntDesired], "coordinate"];
zeroMethod: Method ← AC.MakeMethod[NullaryOp, FALSE, NEW[AC.NullaryOp ← Zero], NIL, "zero"];
oneMethod: Method ← AC.MakeMethod[NullaryOp, FALSE, NEW[AC.NullaryOp ← One], NIL, "one"];
sumMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Add], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "sum"];
negationMethod: Method ← AC.MakeMethod[UnaryOp, TRUE, NEW[AC.UnaryOp ← Negate], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "negation"];
differenceMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Subtract], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "difference"];
scalarProductMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← ScalarMultiply], NEW[AC.UnaryToListOp ← CoordinateAndVectorDesired], "scalarProduct"];
equalMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← Equal], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "equals"];
mapUnaryElementOpMethod: Method ← AC.MakeMethod[BinaryMixedOp, TRUE, NEW[AC.BinaryMixedOp ← MapUnaryElementOp], NIL, "mapUnary"]; -- should nave non-NIL DesiredArgStructures
mapBinaryElementOpMethod: Method ← AC.MakeMethod[TernaryMixedOp, TRUE, NEW[AC.TernaryMixedOp ← MapBinaryElementOp], NIL, "mapBinary"]; -- should nave non-NIL DesiredArgStructures
makeVectorStructureMethod: Method ← AC.MakeMethod[VectorStructureConstructor, FALSE, NEW[AC.VectorStructureConstructor ← MakeVectorStructure], NIL, "makeVectorStructure"];
AC.AddMethodToClass[$category, setCategoryMethod, vectorsClass];
AC.AddMethodToClass[$vectorStructure, vectorStructureMethod, vectorsClass];
AC.AddMethodToClass[$matrixStructure, matrixStructureMethod, vectorsClass];
AC.AddMethodToClass[$shortPrintName, shortPrintNameMethod, vectorsClass];
AC.AddMethodToClass[$recast, recastMethod, vectorsClass];
AC.AddMethodToClass[$canRecast, canRecastMethod, vectorsClass];
AC.AddMethodToClass[$toExpr, toExprMethod, vectorsClass];
AC.AddMethodToClass[$legalFirstChar, legalFirstCharMethod, vectorsClass];
AC.AddMethodToClass[$read, readMethod, vectorsClass];
AC.AddMethodToClass[$fromRope, fromRopeMethod, vectorsClass];
AC.AddMethodToClass[$toRope, toRopeMethod, vectorsClass];
AC.AddMethodToClass[$paren, parenMethod, vectorsClass];
AC.AddMethodToClass[$vector, vectorMethod, vectorsClass];
AC.AddMethodToClass[$coordinate, coordinateMethod, vectorsClass];
AC.AddMethodToClass[$eqFormula, equalMethod, vectorsClass];
AC.AddMethodToClass[$mapUnary, mapUnaryElementOpMethod, vectorsClass];
AC.AddMethodToClass[$mapBinary, mapBinaryElementOpMethod, vectorsClass];
AC.AddMethodToClass[$category, groupCategoryMethod, vectorsOverGroupClass];
AC.AddMethodToClass[$zero, zeroMethod, vectorsOverGroupClass];
AC.AddMethodToClass[$sum, sumMethod, vectorsOverGroupClass];
AC.AddMethodToClass[$negation, negationMethod, vectorsOverGroupClass];
AC.AddMethodToClass[$difference, differenceMethod, vectorsOverGroupClass];
AC.AddMethodToClass[$category, ringCategoryMethod, vectorsOverRingClass];
AC.AddMethodToClass[$category, fieldCategoryMethod, vectorsOverFieldClass];
AC.AddMethodToClass[$category, vectorSpaceCategoryMethod, vectorsOverFieldClass];
AC.AddMethodToClass[$makeVectorStructure, makeVectorStructureMethod, Structures.StructuresClass];