Conversion and IO for Elements of Single Set Structures
SingleSetRecast:
PUBLIC
AC.BinaryOp = {
Args are a StructureElement and a Structure
thisSetStructure: Object ← secondArg;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
familyOfSetsStructureUniverse: Object ← familyOfSetsStructureData.universe;
recastObject: Object;
flag: BOOL;
IF AC.StructureEqual[firstArg.class, secondArg] THEN RETURN[firstArg]; -- nothing to do
See if firstArg is (can be recast into) an element of thisStructureUnderlyingSet; if so, do it.
flag ← AC.ApplyPredLkpRecast[$elementOf, familyOfSetsStructure, LIST[firstArg, thisSetStructureUnderlyingSet] ]; -- apply method, instead of call IsElementOf, to get recast
IF flag
THEN {
recastObject ← AC.ApplyLkpNoRecastObject[$recast, familyOfSetsStructureUniverse, LIST[firstArg, familyOfSetsStructureUniverse] ];
recastObject.class ← thisSetStructure; -- reset class field to point to this Structure; no need for copy
RETURN[recastObject]
}
ELSE RETURN[NIL];
};
SingleSetCanRecast:
PUBLIC
AC.BinaryPredicate = {
Args are either [Structure, Structure] or [StructureElement, Structure]
thisSetStructure: Object ← secondArg;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
familyOfSetsStructureUniverse: Object ← familyOfSetsStructureData.universe;
flag: BOOL;
firstArgStructure: Object ← IF firstArg.flavor = StructureElement THEN firstArg.class ELSE IF firstArg.flavor = Structure THEN firstArg ELSE ERROR;
IF AC.StructureEqual[firstArgStructure, thisSetStructure] THEN RETURN[TRUE];
firstArgStructure must be a SingleSetStructure, and its underlying set must be (recastable into) a subset of secondArg's underlying set.
IF IsSingleSetStructure[firstArgStructure]
THEN {
firstArgSetStructureData: SingleSetStructureData ← NARROW[firstArgStructure.data];
firstArgStructureUnderlyingSet: Object ← firstArgSetStructureData.underlyingSet;
flag ← AC.ApplyPredLkpRecast[$subset, familyOfSetsStructure, LIST[firstArgStructureUnderlyingSet, thisSetStructureUnderlyingSet] ]; -- apply method, instead of call IsSubset, to get recast
RETURN[flag];
};
RETURN[FALSE];
};
SingleSetToExpr:
PUBLIC
AC.ToExprOp = {
Use method for UnderlyingSet universe
thisSetStructure: Object ← in.class;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
universe: Object ← familyOfSetsStructureData.universe;
method: Method ← AC.LookupMethodInStructure[$toExpr, universe];
convertIn: Object ← UnderlyingSetUniverseEltFromSSSElt[in]; -- need to have an element of UnderlyingSet universe to apply it to.
RETURN[ NARROW[AC.ApplyNoLkpNoRecastRef[method, LIST[convertIn] ] ] ];
};
SingleSetLegalFirstChar:
PUBLIC
AC.LegalFirstCharOp = {
Use method for UnderlyingSet universe
thisSetStructure: Object ← structure;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
universe: Object ← familyOfSetsStructureData.universe;
method: Method ← AC.LookupMethodInStructure[$legalFirstChar, universe];
RETURN [AC.ApplyLegalFirstCharMethod[method, char, universe] ];
};
SingleSetRead:
PUBLIC
AC.ReadOp ~ {
Use method for UnderlyingSet universe
thisSetStructure: Object ← structure;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
universe: Object ← familyOfSetsStructureData.universe;
method: Method ← AC.LookupMethodInStructure[$read, universe];
universeElt: Object ← AC.ApplyReadMethod[method, in, structure];
recastElt: Object ← SingleSetRecast[universeElt, thisSetStructure]; -- try to imbed in the Single Set Structure
IF recastElt=NIL THEN ERROR;
RETURN [recastElt];
};
SingleSetFromRope:
PUBLIC
AC.FromRopeOp ~ {
out ← SingleSetRead[IO.RIS[in], structure];
};
SingleSetToRope:
PUBLIC
AC.ToRopeOp ~ {
Use method for UnderlyingSet universe
thisSetStructure: Object ← in.class;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
universe: Object ← familyOfSetsStructureData.universe;
method: Method ← AC.LookupMethodInStructure[$toRope, universe];
convertIn: Object ← UnderlyingSetUniverseEltFromSSSElt[in]; -- need to have an element of UnderlyingSet universe to apply it to.
RETURN[ NARROW[AC.ApplyNoLkpNoRecastRef[method, LIST[convertIn] ] ] ];
};
SingleSetWrite:
PUBLIC
AC.WriteOp ~ {
stream.PutRope[ SingleSetToRope[in] ]
};
UnderlyingSetUniverseEltFromSSSElt:
PUBLIC
AC.UnaryOp ~ {
thisSetStructure: Object ← arg.class;
thisSetStructureData: SingleSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet: Object ← thisSetStructureData.underlyingSet;
familyOfSetsStructure: Object ← NARROW[thisSetStructureUnderlyingSet.class];
familyOfSetsStructureData: FamilyOfSetsStructureData ← NARROW[familyOfSetsStructure.data];
universe: Object ← familyOfSetsStructureData.universe;
result ← AC.Copy[arg];
result.class ← universe;
};
SSSEltFromUnderlyingSetUniverseElt:
PUBLIC AC.BinaryOp ~ {
secondArg is a Single Set Structure, firstArg is an element of its underlyingSet represented as an element of the underlyingSet's universe. result is (a copy of) firstArg with its class reset to secondArg
result ← AC.Copy[firstArg];
result.class ← secondArg;
};
IsVariable:
PUBLIC
AC.UnaryPredicate ~ {
check whether arg is a variable represented as an element of a SingleSetStructure
thisSetStructure: Object;
thisSetStructureData: SingleSetStructureData;
thisSetStructureUnderlyingSet: Object;
familyOfSetsStructure: Object;
IF arg.flavor#StructureElement THEN RETURN[FALSE];
thisSetStructure ← arg.class;
IF NOT IsSingleSetStructure[thisSetStructure] THEN RETURN[FALSE];
thisSetStructureData ← NARROW[thisSetStructure.data];
thisSetStructureUnderlyingSet ← thisSetStructureData.underlyingSet;
familyOfSetsStructure ← NARROW[thisSetStructureUnderlyingSet.class];
IF NOT AC.StructureEqual[familyOfSetsStructure, VariableSets] THEN RETURN[FALSE];
RETURN[TRUE];
};
VariableFromRope:
PUBLIC
AC.FromRopeOp ~ {
variable: Object ← VARS.FromRope[in, VARS.Variables];
set: Object ← MakeSet[LIST[variable], VariableSets];
singleSetStructure: Object ← MakeSingleSetStructure[set];
RETURN[SSSEltFromUnderlyingSetUniverseElt[variable, singleSetStructure] ];
};
Start Code
familyOfSetsClass: AC.Object ← AC.MakeClass["FamilyOfSetsClass", NIL, NIL];
singleSetClass: AC.Object ← AC.MakeClass["SingleSetClass", NIL, NIL];
Kludge up special subclass for VariableSets, containing special evaluation for variables
VariableSets: PUBLIC AC.Object ← MakeFamilyOfSetsStructure[VARS.Variables]; -- this is initialization only; n.b. must be done after familyOfSetsClass initialized, so that gets it as non-NIL class, so that class pointer in variableSetsClass points to something non-NIL as superclass.
variableMethod: Method ← AC.MakeMethod[FromRopeOp, FALSE, NEW[AC.FromRopeOp ← VariableFromRope], NIL, "variable"];
variableSetsClass: AC.Object ← AC.MakeClass["variableSetsClass", VariableSets.class, NIL];
setCategoryMethod: Method ← AC.MakeMethod[Value, FALSE, NEW[AC.Category ← set], NIL, NIL];
familyOfSetsStructureMethod: Method ← AC.MakeMethod[Value, FALSE, NIL, NIL, "familyOfSetsStructure"];
singleSetStructureMethod: Method ← AC.MakeMethod[Value, FALSE, NIL, NIL, "singleSetStructure"];
universeMethod: Method ← AC.MakeMethod[UnaryOp, FALSE, NEW[AC.UnaryOp ← Universe], NIL, "universe"];
underlyingSetMethod: Method ← AC.MakeMethod[UnaryOp, FALSE, NEW[AC.UnaryOp ← UnderlyingSet], NIL, "underlyingSet"];
shortPrintNameMethod: Method ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← FamilyOfSetsShortPrintName], NIL, "shortPrintName"];
recastMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← FamilyOfSetsRecast], NIL, "recast"];
canRecastMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← FamilyOfSetsCanRecast], NIL, "canRecast"];
toExprMethod: Method ← AC.MakeMethod[ToExprOp, FALSE, NEW[AC.ToExprOp ← FamilyOfSetsToExpr], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "toExpr"];
legalFirstCharMethod: Method ← AC.MakeMethod[LegalFirstCharOp, FALSE, NEW[AC.LegalFirstCharOp ← FamilyOfSetsLegalFirstChar], NIL, "legalFirstChar"];
readMethod: Method ← AC.MakeMethod[ReadOp, FALSE, NEW[AC.ReadOp ← FamilyOfSetsRead], NIL, "read"];
fromRopeMethod: Method ← AC.MakeMethod[FromRopeOp, TRUE, NEW[AC.FromRopeOp ← FamilyOfSetsFromRope], NIL, "fromRope"];
toRopeMethod: Method ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← FamilyOfSetsToRope], NIL, "toRope"];
isVariableMethod: Method ← AC.MakeMethod[UnaryPredicate, TRUE, NEW[AC.UnaryPredicate ← IsVariable], NIL, "isVarInSSS"];
USUEltFromSSSEltMethod: Method ← AC.MakeMethod[UnaryOp, TRUE, NEW[AC.UnaryOp ← UnderlyingSetUniverseEltFromSSSElt], NIL, "USUEltFromSSSElt"];
SSSEltFromUSUEltMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← SSSEltFromUnderlyingSetUniverseElt], NIL, "SSSEltFromUSUElt"];
parenMethod: Method ← AC.MakeMethod[UnaryOp, FALSE, NEW[AC.UnaryOp ← AC.Copy], NIL, "paren"];
setMethod: Method ← AC.MakeMethod[ListImbedOp, FALSE, NEW[AC.ListImbedOp ← MakeSet], NIL, "set"];
elementOfMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← IsElement], NEW[AC.UnaryToListOp ← IsElDesiredArgStructures], "elementOf"];
subsetMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← IsSubset], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "subset"];
cardinalityMethod: Method ← AC.MakeMethod[ElementRankOp, TRUE, NEW[AC.ElementRankOp ← Cardinality], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "cardinality"];
equalMethod: Method ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← Equal], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "equals"];
unionMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Union], NIL, "union"];
intersectionMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Intersection], NIL, "intersection"];
differenceMethod: Method ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← Difference], NIL, "difference"];
makeSingleSetStructureMethod: Method ← AC.MakeMethod[StructureFromSetConstructor, FALSE, NEW[AC.StructureFromSetConstructor ← MakeSingleSetStructure], NIL, "makeSingleSetStructure"];
makeFamilyOfSetsStructureMethod: Method ← AC.MakeMethod[SequenceStructureConstructor, FALSE, NEW[AC.SequenceStructureConstructor ← MakeFamilyOfSetsStructure], NIL, "makeFamilyOfSetsStructure"];
AC.AddMethodToClass[$category, setCategoryMethod, familyOfSetsClass];
AC.AddMethodToClass[$familyOfSetsStructure, familyOfSetsStructureMethod, familyOfSetsClass];
AC.AddMethodToClass[$universe, universeMethod, familyOfSetsClass];
AC.AddMethodToClass[$shortPrintName, shortPrintNameMethod, familyOfSetsClass];
AC.AddMethodToClass[$recast, recastMethod, familyOfSetsClass];
AC.AddMethodToClass[$canRecast, canRecastMethod, familyOfSetsClass];
AC.AddMethodToClass[$toExpr, toExprMethod, familyOfSetsClass];
AC.AddMethodToClass[$legalFirstChar, legalFirstCharMethod, familyOfSetsClass];
AC.AddMethodToClass[$read, readMethod, familyOfSetsClass];
AC.AddMethodToClass[$fromRope, fromRopeMethod, familyOfSetsClass];
AC.AddMethodToClass[$toRope, toRopeMethod, familyOfSetsClass];
AC.AddMethodToClass[$paren, parenMethod, familyOfSetsClass];
AC.AddMethodToClass[$set, setMethod, familyOfSetsClass];
AC.AddMethodToClass[$elementOf, elementOfMethod, familyOfSetsClass];
AC.AddMethodToClass[$subset, subsetMethod, familyOfSetsClass];
AC.AddMethodToClass[$cardinality, cardinalityMethod, familyOfSetsClass];
AC.AddMethodToClass[$eqFormula, equalMethod, familyOfSetsClass];
AC.AddMethodToClass[$union, unionMethod, familyOfSetsClass];
AC.AddMethodToClass[$intersect, intersectionMethod, familyOfSetsClass];
AC.AddMethodToClass[$difference, differenceMethod, familyOfSetsClass];
AC.AddMethodToClass[$makeFamilyOfSetsStructure, makeFamilyOfSetsStructureMethod, Structures.StructuresClass];
shortPrintNameMethod ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← SingleSetShortPrintName], NIL, "shortPrintName"];
recastMethod ← AC.MakeMethod[BinaryOp, TRUE, NEW[AC.BinaryOp ← SingleSetRecast], NIL, "recast"];
canRecastMethod ← AC.MakeMethod[BinaryPredicate, TRUE, NEW[AC.BinaryPredicate ← SingleSetCanRecast], NIL, "canRecast"];
toExprMethod ← AC.MakeMethod[ToExprOp, FALSE, NEW[AC.ToExprOp ← SingleSetToExpr], NEW[AC.UnaryToListOp ← AC.DefaultDesiredArgStructures], "toExpr"];
legalFirstCharMethod ← AC.MakeMethod[LegalFirstCharOp, FALSE, NEW[AC.LegalFirstCharOp ← SingleSetLegalFirstChar], NIL, "legalFirstChar"];
readMethod ← AC.MakeMethod[ReadOp, FALSE, NEW[AC.ReadOp ← SingleSetRead], NIL, "read"];
fromRopeMethod ← AC.MakeMethod[FromRopeOp, TRUE, NEW[AC.FromRopeOp ← SingleSetFromRope], NIL, "fromRope"];
toRopeMethod ← AC.MakeMethod[ToRopeOp, FALSE, NEW[AC.ToRopeOp ← SingleSetToRope], NIL, "toRope"];
AC.AddMethodToClass[$category, setCategoryMethod, singleSetClass];
AC.AddMethodToClass[$singleSetStructure, singleSetStructureMethod, singleSetClass];
AC.AddMethodToClass[$underlyingSet, underlyingSetMethod, singleSetClass];
AC.AddMethodToClass[$shortPrintName, shortPrintNameMethod, singleSetClass];
AC.AddMethodToClass[$recast, recastMethod, singleSetClass];
AC.AddMethodToClass[$canRecast, canRecastMethod, singleSetClass];
AC.AddMethodToClass[$toExpr, toExprMethod, singleSetClass];
AC.AddMethodToClass[$legalFirstChar, legalFirstCharMethod, singleSetClass];
AC.AddMethodToClass[$read, readMethod, singleSetClass];
AC.AddMethodToClass[$fromRope, fromRopeMethod, singleSetClass];
AC.AddMethodToClass[$toRope, toRopeMethod, singleSetClass];
AC.AddMethodToClass[$isVarInSSS, isVariableMethod, singleSetClass];
AC.AddMethodToClass[$USUEltFromSSSElt, USUEltFromSSSEltMethod, singleSetClass];
AC.AddMethodToClass[$SSSEltFromUSUElt, SSSEltFromUSUEltMethod, singleSetClass];
AC.AddMethodToClass[$paren, parenMethod, singleSetClass];
AC.AddMethodToClass[$makeSingleSetStructure, makeSingleSetStructureMethod, Structures.StructuresClass];