Skip to content

Commit

Permalink
Fix error in .type field definition
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsklar committed Aug 4, 2024
1 parent 894e3a5 commit e4c6394
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 41 deletions.
5 changes: 0 additions & 5 deletions src/main/scala/ai/newmap/interpreter/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ object Evaluator {
}
}
case AccessField(value, uType, field) => {
//println(s"AccessField: $value -- $uType -- $field")
for {
evalValue <- this(value, env)

//_ = println(s"evalValue: $evalValue")

// The field and type class should already be evaluated, so no need to re-evaluate it here
// TODO - HERE IS THE PROBLEM!
// solution: pattern match all, and figure out which one(s) to return
Expand All @@ -41,8 +38,6 @@ object Evaluator {
case _ => Failure("Can't access value: " + result)
}

//_ = println(s"resultValue: $resultValue")

answer <- applyFunction(resultValue, evalValue, env)
} yield answer
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/scala/ai/newmap/interpreter/StatementInterpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object StatementInterpreter {
}
}

_ = println("fieldTypes: " + fieldTypes)
//_ = println("fieldTypes: " + fieldTypes)

// 2) put that into a struct
implementationRequirements: NewMapType = StructT(UMap(fieldTypes), IdentifierT)
Expand Down Expand Up @@ -174,7 +174,7 @@ case class UpdateTypeclassWithFieldCommandParse(
fieldT <- fieldTypeObject.asType

//_ = println(s"fieldT: $fieldT")
_ = println(s"implementations: $implementations -- ${fieldTypeResult.tcParameters}")
//_ = println(s"implementations: $implementations -- ${fieldTypeResult.tcParameters}")

implementationsResult <- TypeChecker.typeCheck(
implementations,
Expand All @@ -184,19 +184,17 @@ case class UpdateTypeclassWithFieldCommandParse(
fieldTypeResult.tcParameters
)

_ = println(s"implementationsResult: $implementationsResult")
//_ = println(s"implementationsResult: $implementationsResult")

mapBindings <- implementationsResult.nExpression.getMapBindings()

_ = println(s"mapBindings: $mapBindings")
} yield {
val implementationsTransformed: Vector[(NewMapType, UntaggedObject)] = mapBindings.flatMap(binding => {
for {
implT <- binding._1.asType.toOption
} yield implT -> binding._2
})

println(s"implementationsTransformed: $implementationsTransformed")
//println(s"implementationsTransformed: $implementationsTransformed")

ReturnValue(
UpdateTypeclassWithFieldCommand(id, typeParameter, fieldT, fieldName, implementationsTransformed, isCommand),
Expand Down
48 changes: 25 additions & 23 deletions src/main/scala/ai/newmap/interpreter/TypeChecker.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ai.newmap.interpreter

import ai.newmap.model._
import ai.newmap.util.{Outcome, Success, Failure}
import ai.newmap.util.{Outcome, Success, Failure, DebugUtils}
import java.util.UUID

object TypeChecker {
Expand Down Expand Up @@ -110,10 +110,6 @@ object TypeChecker {
} else {
env.lookupValue(s) match {
case Some(nObject) => {
if (s == "asdf") {
println("nObject: " + nObject + " -- " + nObject.nType + " -- " + expectedType)
}

responseFromConversion(nObject, expectedType, env, tcParameters)
}
case None if (env.typeSystem.typeToParameterType.get(s).nonEmpty) => {
Expand Down Expand Up @@ -735,26 +731,26 @@ object TypeChecker {
for {
underlyingTypeOfFunction <- getFinalUnderlyingType(function.nType, env)

_ = println("underlyingTypeOfFunction: " + underlyingTypeOfFunction)
//_ = println("underlyingTypeOfFunction: " + underlyingTypeOfFunction)

functionUntaggedObject = Evaluator.stripVersioningU(function.uObject, env)

inputObj <- underlyingTypeOfFunction.inputTypeOpt(Some(functionUntaggedObject)) match {
case Some(inputT) => {
println("in here: " + inputT + " -- " + typeCheck(input, inputT, env, FullFunction, tcParameters))
//println("in here: " + inputT + " -- " + typeCheck(input, inputT, env, FullFunction, tcParameters))
for {
inputTypeChecked <- typeCheck(input, inputT, env, FullFunction, tcParameters)
} yield NewMapObject(inputTypeChecked.nExpression, inputTypeChecked.refinedTypeClass)
}
case None => {
println("in here!!!")
//println("in here!!!")
for {
typeCheckUnknownTypeResult <- typeCheckUnknownType(input, env, tcParameters)
} yield NewMapObject(typeCheckUnknownTypeResult.nExpression, typeCheckUnknownTypeResult.refinedTypeClass)
}
}

_ = println("inputObj: " + inputObj + " -- " + inputObj.nType)
//_ = println("inputObj: " + inputObj + " -- " + inputObj.nType)

functionObj = NewMapObject(functionUntaggedObject, underlyingTypeOfFunction)

Expand Down Expand Up @@ -855,18 +851,18 @@ object TypeChecker {
tcParameters: Map[String, NewMapType]
): Outcome[TypeCheckResponse, String] = nObjects match {
case nObject +: others => {
println("accessFieldTypeParseWithInput: " + nObject + " -- " + field + " -- " + input)
//println("accessFieldTypeParseWithInput: " + nObject + " -- " + field + " -- " + input)

val firstAttempt: Outcome[TypeCheckResponse, String] = for {
result <- accessFieldTypeParseSingleType(nObject, field, WildcardT("_"), env, tcParameters)
_ = println("result: " + result.nExpression + " -- " + result.refinedTypeClass + " -- " + input)
//_ = println("result: " + result.nExpression + " -- " + result.refinedTypeClass + " -- " + input)
functionResult <- typeCheckKnownFunction(NewMapObject(result.nExpression, result.refinedTypeClass), input, env, result.tcParameters)

_ = println("function result: " + functionResult)
//_ = println("function result: " + functionResult)
tcResponse <- verifyFunctionResult(functionResult, WildcardT("_"), env, featureSet, tcParameters)
} yield tcResponse

println("firstAttempt: " + firstAttempt)
//println("firstAttempt: " + firstAttempt)

firstAttempt.rescue(f => {
val newNObjects = for {
Expand Down Expand Up @@ -935,14 +931,14 @@ object TypeChecker {
env
)

_ = println("returnValue: " + returnValue + " -- " + theFieldTC.tcParameters)
//_ = println("returnValue: " + returnValue + " -- " + theFieldTC.tcParameters)

returnObj <- returnValue match {
case UCase(t, o) => t.asType.map(nT => NewMapObject(o, nT))
case _ => Failure("Unknown return value: " + returnValue)
}

_ = println("returnObj: " + returnObj + " -- " + returnObj.nType)
//_ = println("returnObj: " + returnObj + " -- " + returnObj.nType)

response <- TypeConverter.isTypeConvertible(returnObj.nType, expectedType, env)
} yield {
Expand All @@ -966,13 +962,16 @@ object TypeChecker {
}

def tagAndNormalizeObject(uObject: UntaggedObject, nTypeClass: NewMapType, env: Environment): Outcome[NewMapObject, String] = {
uObject match {
case UInit => {
UpdateCommandCalculator.getDefaultValueOfCommandType(nTypeClass, env).map(initValue => NewMapObject(initValue, nTypeClass))
}
case _ => (nTypeClass, normalizeCount(uObject)) match {
case (CountT, Success(i)) => Success(NewMapObject(UIndex(i), nTypeClass))
case _ => Success(NewMapObject(uObject, nTypeClass))
if (uObject == UInit) {
UpdateCommandCalculator.getDefaultValueOfCommandType(nTypeClass, env).map(initValue => NewMapObject(initValue, nTypeClass))
} else {
if (nTypeClass == CountT) {
normalizeCount(uObject) match {
case Success(i) => Success(NewMapObject(UIndex(i), nTypeClass))
case _ => Success(NewMapObject(uObject, nTypeClass))
}
} else {
Success(NewMapObject(uObject, nTypeClass))
}
}
}
Expand All @@ -984,7 +983,10 @@ object TypeChecker {
normalizeCount(internal).map(i => i + 1)
}
case UIndex(i) => Success(i)
case _ => Failure(s"Couldn't normalize count: $uObject")
case _ => {
//DebugUtils.printStackTrace()
Failure(s"Couldn't normalize count: $uObject")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ai/newmap/interpreter/TypeConverter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object TypeConverter {
MapT(TypeTransform(startingInputType, startingOutputType), MapConfig(startingCompleteness, startingFeatureSet, _, _, _)),
MapT(TypeTransform(endingInputType, endingOutputType), MapConfig(endingCompleteness, endingFeatureSet, _, _, _))
) => {
println("We are here: " + startingType + " -- " + endingType)
//println("We are here: " + startingType + " -- " + endingType)

// TODO: This is not entirely true
// I think we can convert these (so long as the feature set is compatible) - but conversion from
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/ai/newmap/model/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ case class Environment(
)
}
case NewVersionedFieldCommand(id, mapT, value, isCommand) => {
println(s"newVersionedFieldCommand: $id -- $mapT -- $value")
//println(s"newVersionedFieldCommand: $id -- $mapT -- $value")

val resultO = for {
typeTransform <- mapT match {
Expand Down Expand Up @@ -325,11 +325,11 @@ case class Environment(
val implT = implementation._1
val impl = implementation._2

println("implT: " + implT)
println("impl: " + impl)
//println("implT: " + implT)
//println("impl: " + impl)

// I THINK SOMETHING IS HERE
println(s"upcoming substitution for $id . $fieldName: " + fieldType + " -- " + Map(typeParameter -> implT.asUntagged))
//println(s"upcoming substitution for $id . $fieldName: " + fieldType + " -- " + Map(typeParameter -> implT.asUntagged))

// TODO(max) - This is a key spot!!
// ANOTHER GUESS: fieldT is ALWAYS a map - and the key needs to be substituted differently than the value
Expand All @@ -349,7 +349,7 @@ case class Environment(
isCommand
)

println("calling newVersionedFieldCommand: " + newVersionedFieldCommand)
//println("calling newVersionedFieldCommand: " + newVersionedFieldCommand)

newEnv = newEnv.newCommand(newVersionedFieldCommand)
}
Expand Down Expand Up @@ -744,7 +744,9 @@ object Environment {
MapConfig(RequireCompleteness, SimpleFunction)
),
"type",
Vector(WildcardT("t") -> ParamId("t")),
Vector(WildcardT("t") -> UMap(
UWildcard("_") -> ParamId("t")
)),
false
),
NewParamTypeCommand(
Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/ai/newmap/model/UntaggedObject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ case class UIdentifier(s: String) extends UntaggedObject
// TODO: change from Vector to ListMap
case class UMap(values: Vector[(UntaggedObject, UntaggedObject)]) extends UntaggedObject

object UMap {
def apply(values: (UntaggedObject, UntaggedObject)*): UMap = {
UMap(values.toVector)
}
}

// This is equivalent to UMap where the keys are indecies
case class UArray(values: Array[UntaggedObject]) extends UntaggedObject

Expand Down
10 changes: 10 additions & 0 deletions src/main/scala/ai/newmap/util/DebugUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ai.newmap.util

object DebugUtils {
def printStackTrace(): Unit = {
val stackTrace = new Throwable().getStackTrace
stackTrace.foreach { traceElement =>
println(traceElement)
}
}
}

0 comments on commit e4c6394

Please sign in to comment.