Skip to content

Commit

Permalink
Massive refactor/reorg of types; removed WithState and HistoricalType…
Browse files Browse the repository at this point in the history
…T among other changes
  • Loading branch information
maxsklar committed Oct 2, 2023
1 parent 45fe9d8 commit 6da1d58
Show file tree
Hide file tree
Showing 23 changed files with 596 additions and 814 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ Build and suggest statistical models based on properties of the dataset.
Because there is strong verification onboard, imagine having code that is constantly finding upgrades to the way it stores and serves data. These upgrades can be found through the internet, or through an algorithm that searches the solution space.

### Universal Concept Map
Once probabilistic programming is a part of newmap.ai, we can generate a collection to model the space of “concepts” known to humans. This is an approach to natural language understanding which
Once probabilistic programming is a part of newmap.ai, we can generate a collection to model the space of “concepts” known to humans. This is an approach to natural language understanding which

## Code Design Principles

### Untagged Objects vs Tagged Objects

### NewMapType
NewMapType is a representation of a type in the type system. Ultimately, the information in NewMapType should specify everything that we need to know about the type of an object.

NewMapType can also be turned into an Untagged Object.
2 changes: 1 addition & 1 deletion src/main/newmap/system/init.nm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data String = Array|Char
typeclass _display (t: (t => String))
update _display: (String: (s: s))

//case CustomT("Array", nType) => Success(UCase(UIndex(0), UArray(Array.empty)))
//case CustomT("Array", nType) => Success(UCase(UIndex(0), UArray()))

//data Option (T: Type)
//update Option: (None, ())
Expand Down
148 changes: 36 additions & 112 deletions src/main/scala/ai/newmap/interpreter/CommandMaps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ object CommandMaps {
*/
def getDefaultValueOfCommandTypeFromEnv(nType: UntaggedObject, env: Environment): Outcome[UntaggedObject, String] = {
for {
defaultTypeId <- Outcome(env.typeSystem.currentMapping.get("_default"), "Couldn't find _default typeClass")
defaultUnderlyingType <- env.typeSystem.currentUnderlyingType("_default")

defaultUnderlyingType <- Outcome(env.typeSystem.typeToUnderlyingType.get(defaultTypeId), "Couldn't find _default underlying typeclass")

defaultParameterPattern = defaultUnderlyingType._1
defaultUnderlyingExp = defaultUnderlyingType._2

defaultUnderlyingExpT <- defaultUnderlyingExp.asType
defaultUnderlyingExpT = defaultUnderlyingType._2

mapValuesU <- defaultUnderlyingExpT match {
case TypeClassT(_, values) => Success(values)
Expand Down Expand Up @@ -77,11 +72,8 @@ object CommandMaps {
Success(defaultUMap)
}*/
case CharacterT => Success(UCharacter('\u0000'))
case CustomT("Array", _) => Success(UCase(UIndex(0), UArray(Array.empty)))
case CustomT("String", _) => Success(UCase(UIndex(0), UArray(Array.empty))) // Replace this line with a conversion!
case WithStateT(_, underlying) => {
getDefaultValueOfCommandTypeHardcoded(underlying, env)
}
case CustomT("Array", _, _) => Success(UCase(UIndex(0), UArray()))
case CustomT("String", _, _) => Success(UCase(UIndex(0), UArray())) // Replace this line with a conversion!
case _ => Failure(s"Type ${nType.displayString(env)} has no default value")
}
}
Expand All @@ -103,19 +95,13 @@ object CommandMaps {
case IndexT(_) => Success(NewMapO.emptyStruct) // Where to insert the new value?
case CaseT(_, parentType, _) => {
Success(StructT(
UMap(Vector(
UIndex(0) -> parentType.asUntagged,
UIndex(1) -> HistoricalTypeT(typeSystem.currentState).asUntagged
)),
UArray(parentType.asUntagged, TypeT.asUntagged),
IndexTN(2)
))
}
case StructT(_, parentType, _, _) => {
Success(StructT(
UMap(Vector(
UIndex(0) -> parentType.asUntagged,
UIndex(1) -> SubtypeT(IsCommandFunc, HistoricalTypeT(typeSystem.currentState)).asUntagged
)),
UArray(parentType.asUntagged, SubtypeT(IsCommandFunc, TypeT).asUntagged),
IndexTN(2)
))
}
Expand All @@ -126,25 +112,18 @@ object CommandMaps {
Success(StructT(typeTransform, TypeT, CommandOutput, BasicMap))
}
//case MapT(keyType, valueType, config) => getTypeExpansionCommandInput(valueType, typeSystem)
case CustomT(name, UArray(_)) => {
val currentState = typeSystem.currentState

case CustomT(name, _, _) => {
for {
currentMapping <- Outcome(typeSystem.historicalMapping.get(currentState), s"Current type mapping $currentState not found")
currentTypeId <- Outcome(currentMapping.get(name), s"$name must be defined")
currentUnderlyingType <- Outcome(typeSystem.typeToUnderlyingType.get(currentTypeId), s"Couldn't find underlying type for $name")
currentUnderlyingType <- typeSystem.currentUnderlyingType(name)

currentParameterPattern = currentUnderlyingType._1
underlyingT <- currentUnderlyingType._2.asType

commandInput <- getTypeExpansionCommandInput(underlyingT, typeSystem)
commandInput <- getTypeExpansionCommandInput(currentUnderlyingType._2, typeSystem)
} yield commandInput
}
case _ => Failure(s"Unable to expand key: $nType")
}
}

val untaggedIdentity: UntaggedObject = UMap(Vector(UWildcardPattern("_") -> ParamId("_")))
val untaggedIdentity: UntaggedObject = UMap(Vector(UWildcard("_") -> ParamId("_")))

case class ExpandKeyResponse(
newType: NewMapType,
Expand Down Expand Up @@ -184,7 +163,7 @@ object CommandMaps {
} yield {
ExpandKeyResponse(
CaseT(newCaseMap.uObject, parentType, featureSet),
Vector(UCase(newCaseName, UWildcardPattern("_"))),
Vector(UCase(newCaseName, UWildcard("_"))),
untaggedIdentity
)
}
Expand All @@ -195,10 +174,7 @@ object CommandMaps {
MapConfig(CommandOutput, BasicMap)
))

val adjustedCommand = UMap(Vector(
UIndex(0) -> command,
UIndex(1) -> UIndex(1)
))
val adjustedCommand = UArray(command, UIndex(1))

for {
newMembersMap <- updateVersionedObject(isMemberMap, adjustedCommand, env)
Expand Down Expand Up @@ -232,20 +208,12 @@ object CommandMaps {
}
}
}
case CustomT(name, _) => {
case CustomT(name, _, _) => {
// This only occurs if we have a custom type within a custom type - so this won't be called for a while.
// strategy: get underlying type from the type system, turn it into a NewMapType, and then call this on it!
val typeSystem = env.typeSystem
val currentState = typeSystem.currentState
for {
currentMapping <- Outcome(typeSystem.historicalMapping.get(currentState), s"Current type mapping $currentState not found")
currentTypeId <- Outcome(currentMapping.get(name), s"$name must be defined")
currentUnderlyingTypeInfo <- Outcome(typeSystem.typeToUnderlyingType.get(currentTypeId), s"Couldn't find underlying type for $name")

currentParameterPattern = currentUnderlyingTypeInfo._1
currentUnderlyingType = currentUnderlyingTypeInfo._2
currentUnderlyingT <- currentUnderlyingType.asType
response <- expandType(currentUnderlyingT, command, env)
currentUnderlyingTypeInfo <- env.typeSystem.currentUnderlyingType(name)
response <- expandType(currentUnderlyingTypeInfo._2, command, env)
} yield response
}
case _ => Failure(s"Unable to expand key: $nType -- with command $command")
Expand All @@ -254,8 +222,7 @@ object CommandMaps {

def getCommandInputOfCommandType(
nType: NewMapType,
env: Environment,
typeSystemIdOpt: Option[UUID] = None
env: Environment
): Outcome[NewMapType, String] = {
nType match {
case CountT => Success(NewMapO.emptyStruct)
Expand All @@ -272,10 +239,7 @@ object CommandMaps {
outputCommandT <- getCommandInputOfCommandType(typeTransform.valueType, env)
} yield {
StructT(
UMap(Vector(
UIndex(0) -> typeTransform.keyType.asUntagged,
UIndex(1) -> outputCommandT.asUntagged
)),
UArray(typeTransform.keyType.asUntagged, outputCommandT.asUntagged),
IndexTN(2)
)
}
Expand All @@ -301,10 +265,7 @@ object CommandMaps {
}
case _ => {
StructT(
UMap(Vector(
UIndex(0) -> keyExpansionCommandT.asUntagged,
UIndex(1) -> requiredValuesT.asUntagged
)),
UArray(keyExpansionCommandT.asUntagged, requiredValuesT.asUntagged),
IndexTN(2)
)
}
Expand All @@ -313,10 +274,7 @@ object CommandMaps {
}
case FunctionalSystemT(_) => {
Success(StructT(
UMap(Vector(
UIndex(0) -> IdentifierT.asUntagged,
UIndex(1) -> NewMapO.taggedObjectT.asUntagged
)),
UArray(IdentifierT.asUntagged, NewMapO.taggedObjectT.asUntagged),
IndexTN(2)
))
}
Expand All @@ -332,10 +290,7 @@ object CommandMaps {
// A) A field expansion command
// B) The tagged object that goes in there (so both type and object)
Success(StructT(
UMap(Vector(
UIndex(0) -> fieldExpansionCommandT.asUntagged,
UIndex(1) -> NewMapO.taggedObjectT.asUntagged
)),
UArray(fieldExpansionCommandT.asUntagged, NewMapO.taggedObjectT.asUntagged),
IndexTN(2)
))
}
Expand All @@ -345,35 +300,27 @@ object CommandMaps {
Success(CaseT(parameterList, parentFieldType, featureSet))
}
// This should be custom defined
case CustomT("Array", nType) => nType.asType
case CustomT(typeName, params) => {
val typeSystemId = typeSystemIdOpt.getOrElse(env.typeSystem.currentState)
val typeSystemMapping = env.typeSystem.historicalMapping.get(typeSystemId).getOrElse(Map.empty)

case CustomT("Array", nType, _) => nType.asType
case CustomT(typeName, params, typeSystemId) => {
for {
typeId <- Outcome(typeSystemMapping.get(typeName), s"Couldn't find type: $typeName")
underlyingTypeInfo <- Outcome(env.typeSystem.typeToUnderlyingType.get(typeId), s"Couldn't find type: $typeName -- $typeId")
underlyingTypeInfo <- env.typeSystem.historicalUnderlyingType(typeName, typeSystemId)

(underlyingPattern, underlyingExp) = underlyingTypeInfo

patternMatchSubstitutions <- Evaluator.patternMatch(underlyingPattern, params, StandardMatcher, env)

underlyingType = MakeSubstitution(underlyingExp, patternMatchSubstitutions)
underlyingType = MakeSubstitution(underlyingExp.asUntagged, patternMatchSubstitutions)

underlyingT <- underlyingType.asType
result <- getCommandInputOfCommandType(underlyingT, env)
} yield {
result
}
}
case WithStateT(typeSystemId, nType) => {
getCommandInputOfCommandType(nType, env, Some(typeSystemId))
}
/*case CaseT(cases, _, _) => {
Success(UndefinedT)
}*/
case _ => {
println(s"Got undefined for ${nType.displayString(env)}")
Success(UndefinedT)
}
}
Expand All @@ -382,8 +329,7 @@ object CommandMaps {
def updateVersionedObject(
current: NewMapObject,
command: UntaggedObject,
env: Environment,
typeSystemIdOpt: Option[UUID] = None
env: Environment
): Outcome[NewMapObject, String] = {
current.nType match {
case nType@CountT => {
Expand Down Expand Up @@ -431,10 +377,7 @@ object CommandMaps {
env
)

mapValues <- current.uObject match {
case UMap(values) => Success(values)
case _ => Failure(s"Couldn't get map values from ${current.uObject}")
}
mapValues <- current.uObject.getMapBindings()

newMapValues = (input -> newResultForInput.uObject) +: mapValues.filter(x => x._1 != input)
} yield {
Expand All @@ -457,16 +400,12 @@ object CommandMaps {
case _ => {

for {
commandPatterns <- command match {
case UMap(patterns) => Success(patterns)
case _ => Failure(s"Unexpected command shape $command")
}

commandPatterns <- command.getMapBindings()
keyField <- Evaluator.applyFunction(command, UIndex(0), env)

valueExpression <- Evaluator.patternMatchInOrder(commandPatterns, UIndex(1), env)
valueExpressionEval<- Evaluator(valueExpression, env)
} yield {
(NewMapObject(keyField, keyT), valueExpression)
(NewMapObject(keyField, keyT), valueExpressionEval)
}
}
}
Expand All @@ -483,10 +422,7 @@ object CommandMaps {
MapConfig(style, features)
)

mapValues <- current.uObject match {
case UMap(values) => Success(values)
case _ => Failure(s"Couldn't get map values from $current")
}
mapValues <- current.uObject.getMapBindings()

_ <- Outcome.failWhen(updateKeyTypeResponse.newValues.length > 1, "A Unimplemented")

Expand All @@ -500,9 +436,7 @@ object CommandMaps {
} yield (value._1 -> value._2)

newMapValues = (newPattern -> valueExpansionExpression) +: prepNewValues
} yield {
NewMapObject(UMap(newMapValues), newTableType)
}
} yield NewMapObject(UMap(newMapValues), newTableType)
}
case FunctionalSystemT(functionTypes) => {
for {
Expand Down Expand Up @@ -600,7 +534,7 @@ object CommandMaps {
}
}
}
case nType@CustomT("Array", _) => {
case nType@CustomT("Array", _, _) => {

for {
untaggedResult <- current.uObject match {
Expand All @@ -616,11 +550,12 @@ object CommandMaps {
result <- TypeChecker.tagAndNormalizeObject(untaggedResult, nType, env)
} yield result
}
case nType@CustomT(typeName, params) => {
case nType@CustomT(typeName, params, typeSystemId) => {
val customResultOutcome = {
command match {
case UCase(name, value) => {
for {
// TODO - take into account the typeSystemId
// TODO - what if this intercepts a field that's not a command?
func <- Evaluator(AccessField(current.uObject, current.nType.asUntagged, name), env)
afterCommand <- Evaluator.applyFunction(func, value, env, StandardMatcher)
Expand All @@ -632,18 +567,14 @@ object CommandMaps {
}

customResultOutcome.rescue(f => {
val typeSystemId = typeSystemIdOpt.getOrElse(env.typeSystem.currentState)
val typeSystemMapping = env.typeSystem.historicalMapping.get(typeSystemId).getOrElse(Map.empty)

for {
typeId <- Outcome(typeSystemMapping.get(typeName), s"Couldn't find type: $typeName")
underlyingTypeInfo <- Outcome(env.typeSystem.typeToUnderlyingType.get(typeId), s"Couldn't find type: $typeName -- $typeId")
underlyingTypeInfo <- env.typeSystem.historicalUnderlyingType(typeName, typeSystemId)

(underlyingPattern, underlyingExp) = underlyingTypeInfo

patternMatchSubstitutions <- Evaluator.patternMatch(underlyingPattern, params, StandardMatcher, env)

underlyingType = MakeSubstitution(underlyingExp, patternMatchSubstitutions)
underlyingType = MakeSubstitution(underlyingExp.asUntagged, patternMatchSubstitutions)

underlyingT <- underlyingType.asType
currentResolved <- TypeChecker.tagAndNormalizeObject(current.uObject, underlyingT, env)
Expand All @@ -653,13 +584,6 @@ object CommandMaps {
} yield resultResolved
})
}
case WithStateT(typeSystemId, nType) => {
for {
retaggedCurrent <- TypeChecker.tagAndNormalizeObject(current.uObject, nType, env)

result <- updateVersionedObject(retaggedCurrent, command, env, Some(typeSystemId))
} yield result
}
case _ => {
Failure(s"C) ${current.displayString(env)} is not a command type, error in type checker")
}
Expand Down
Loading

0 comments on commit 6da1d58

Please sign in to comment.