Skip to content

Commit

Permalink
Make StructT updates not change the type, replace tests with Table of…
Browse files Browse the repository at this point in the history
… Objects
  • Loading branch information
maxsklar committed Nov 5, 2023
1 parent 72d3ea7 commit a276041
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
45 changes: 6 additions & 39 deletions src/main/scala/ai/newmap/interpreter/UpdateCommandCalculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,10 @@ object UpdateCommandCalculator {
pairT
))
}
case StructT(_, parentFieldType, RequireCompleteness, _) => {
// We may have the option to wanting to expand this struct!
// TODO: This is one of the cases where the type CHANGES when you update the object
// - should this be allowed? This may be a problem.

// Expand the number of fields in this struct like so!
val fieldExpansionCommandT = parentFieldType
// We are freely adding to an object and changing the type of it's fields
// This means that we need to give
// A) A field expansion command
// B) The tagged object that goes in there (so both type and object)
Success(StructT(
UArray(fieldExpansionCommandT.asUntagged, NewMapO.taggedObjectT.asUntagged),
pairT
))
}
case StructT(parameterList, parentFieldType, CommandOutput, featureSet) => {
case StructT(parameterList, parentFieldType, _, featureSet) => {
// Change to CaseT because we are adding a single parameter!
// Are we allowed to change an old parameter? Let's say sure.
// TODO: rethink this, maybe the input should actually be a struct of the same type to overlay the old struct
// - or not the same type, but the command type of each field!
Success(CaseT(parameterList, parentFieldType, featureSet))
}
case ArrayT(nType) => Success(nType)
Expand Down Expand Up @@ -319,38 +304,20 @@ object UpdateCommandCalculator {
mapValues <- current.uObject.getMapBindings()

nameOfField <- Evaluator.applyFunction(command, UIndex(0), env)
newValueAsNewMapObject <- Evaluator.applyFunction(command, UIndex(1), env)

uCaseValue <- newValueAsNewMapObject match {
case u@UCase(_, _) => Success(u)
case _ => Failure(s"Wrong update for complete struct: $newValueAsNewMapObject")
}

parameterListValues <- parameterList.getMapBindings()

valueOfField <- Evaluator.applyFunction(command, UIndex(1), env)
} yield {
val typeOfField = uCaseValue.constructor
val valueOfField = uCaseValue.input

val newMapValues = (nameOfField -> valueOfField) +: mapValues.filter(x => x._1 != nameOfField)

val newParams = (nameOfField -> typeOfField) +: parameterListValues.filter(x => x._1 != nameOfField)

NewMapObject(UMap(newMapValues), StructT(UMap(newParams), parentFieldType, RequireCompleteness, featureSet))
NewMapObject(UMap(newMapValues), current.nType)
}
}
case StructT(params, parentFieldType, CommandOutput, _) => {
command match {
case UCase(constructor, input) => {
for {
mapValues <- current.uObject.getMapBindings()

// TODO - this part is currently only written for Initializable
// Params need to be updated!!
newParams = params
} yield {
val newMapValues = (constructor -> input) +: mapValues.filter(x => x._1 != constructor)
NewMapObject(UMap(newMapValues), StructT(newParams, parentFieldType))
NewMapObject(UMap(newMapValues), current.nType)
}
}
case _ => {
Expand Down
4 changes: 0 additions & 4 deletions src/main/scala/ai/newmap/model/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,6 @@ object Environment {
UpdateTypeclassWithFieldCommand("AnyType", "_", TypeT, "typeOf", Vector(
WildcardT("t") -> ParamId("t")
), false),
/*ApplyIndividualCommand(
"_typeOf",
UMap(Vector(UWildcard("t") -> UMap(Vector(UWildcard("_") -> ParamId("t")))))
),*/
NewParamTypeCommand(
"Array",
Vector("T" -> TypeT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,29 @@ class TestFullEnvironmentInterpreter extends FlatSpec {
))
}

it should " be creatable as a versioned object" in {
it should " be recognized as complete when in a pattern " in {
testCodeScript(Vector(
CodeExpectation("ver x = new ()", GeneralSuccessCheck),
CodeExpectation("update x: (myField, Count|3)", GeneralSuccessCheck),
CodeExpectation("val myStructSimple: Type = (first: 1, second: 1)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStructSimple: Identifier) = ((0, 0): x)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap((first: 1, second: 1): Identifier) = ((0, 0): x)", GeneralSuccessCheck),
CodeExpectation("val myStruct: Type = (first: 2, second: 3)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStruct: Identifier) = ((0, 0): a, (0, 1): b, (0, 2): c, (1, 0): d, (1, 1): e, (1, 2): f)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStruct: Identifier) = ((0, 0): a, (0, 1): b, (1, 0): d, (1, 1): e, (1, 2): f)", FailureCheck),
))
}

it should " be updatable with a pre-defined tagged object" in {
"A partial map of Objects" should " be possible" in {
testCodeScript(Vector(
CodeExpectation("ver x = new ()", GeneralSuccessCheck),
CodeExpectation("val o: Object = Count|3", GeneralSuccessCheck),
CodeExpectation("update x: (myField, o)", GeneralSuccessCheck),
CodeExpectation("ver x = new Table(Identifier: Object)", GeneralSuccessCheck),
CodeExpectation("update x: (myField, Count|3)", GeneralSuccessCheck),
))
}

it should " be recognized as complete when in a pattern " in {
it should " be updatable with a pre-defined tagged object" in {
testCodeScript(Vector(
CodeExpectation("val myStructSimple: Type = (first: 1, second: 1)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStructSimple: Identifier) = ((0, 0): x)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap((first: 1, second: 1): Identifier) = ((0, 0): x)", GeneralSuccessCheck),
CodeExpectation("val myStruct: Type = (first: 2, second: 3)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStruct: Identifier) = ((0, 0): a, (0, 1): b, (0, 2): c, (1, 0): d, (1, 1): e, (1, 2): f)", GeneralSuccessCheck),
CodeExpectation("val myMap: ReqMap(myStruct: Identifier) = ((0, 0): a, (0, 1): b, (1, 0): d, (1, 1): e, (1, 2): f)", FailureCheck),
CodeExpectation("ver x = new Table(Identifier: Object)", GeneralSuccessCheck),
CodeExpectation("val o: Object = Count|3", GeneralSuccessCheck),
CodeExpectation("update x: (myField, o)", GeneralSuccessCheck),
))
}

Expand Down

0 comments on commit a276041

Please sign in to comment.