Skip to content

Commit

Permalink
Get the Displayable typeclass working
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsklar committed Nov 5, 2023
1 parent 7f9cbd7 commit 72d3ea7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/main/newmap/system/init.nm
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ update class Initializable with type Type: (init : UndefinedType)

data String = Array|Char

typeclass Displayable (t: (t => String))
typeclass Displayable
update class Displayable t with field (t => String) as display
//update class Displayable with type String: (display : (s: s))
update class Displayable with type String: (display : (s: s))

//data Option (T: Type)
//update Option: (None, ())
//update Option: (Some, T)

typeclass addable (t: (t => t => t))
//typeclass Addable
//typeclass addable (t: (t => t => t))
29 changes: 25 additions & 4 deletions src/main/scala/ai/newmap/interpreter/StatementInterpreter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ai.newmap.interpreter

import ai.newmap.interpreter.TypeChecker.{typeCheck, typeCheckGenericMap}
import ai.newmap.interpreter.TypeChecker.{typeCheck, typeCheckMap}
import ai.newmap.model._
import ai.newmap.util.{Failure, Outcome, Success}

Expand Down Expand Up @@ -89,7 +89,7 @@ object StatementInterpreter {
val typeTransform = TypeTransform(IdentifierT, TypeT)

for {
mapValues <- typeCheckGenericMap(values, typeTransform, false, env, BasicMap, Map.empty)
mapValues <- typeCheckMap(values, typeTransform, false, env, BasicMap, Map.empty)
paramList <- convertMapValuesToParamList(mapValues, env)
} yield {
ReturnValue(
Expand Down Expand Up @@ -120,10 +120,31 @@ object StatementInterpreter {
nTypeObj <- Evaluator(nTypeResult.nExpression, env)
nType <- nTypeObj.asType

// What is the type for the implementations?
// 1) get the implementations required
typeClassFields: Map[String, TypeClassFieldInfo] = {
env.typeclassToFieldMapping.get(id.s).getOrElse(Map.empty)
}

fieldTypes: Vector[(UntaggedObject, UntaggedObject)] = {
for {
(fieldName, fieldInfo) <- typeClassFields.toVector
} yield {
val fieldType = MakeSubstitution(
fieldInfo.nType.asUntagged,
Map(fieldInfo.wildcardParam -> nTypeObj)
)

(UIdentifier(fieldName) -> fieldType)
}
}

// 2) put that into a struct
implementationRequirements: NewMapType = StructT(UMap(fieldTypes), IdentifierT)

implementationsResult <- TypeChecker.typeCheck(
implementations,
// THE PROBLEM IS HERE!!
MapT(TypeTransform(IdentifierT, nType), MapConfig(PartialMap, BasicMap)),
implementationRequirements,
env,
FullFunction,
tcParameters
Expand Down
27 changes: 12 additions & 15 deletions src/main/scala/ai/newmap/interpreter/TypeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ object TypeChecker {
}

for {
mapValues <- typeCheckGenericMap(correctedValues, typeTransform, config.featureSet != BasicMap, env, config.featureSet, tcParameters)
mapValues <- typeCheckMap(correctedValues, typeTransform, config.featureSet != BasicMap, env, config.featureSet, tcParameters)

isCovered <- {
if (config.completeness != RequireCompleteness) Success(true)
Expand Down Expand Up @@ -321,7 +321,7 @@ object TypeChecker {
{
val typeTransform = TypeTransform(IdentifierT, typeT)
for {
mapValues <- typeCheckGenericMap(values, typeTransform, false, env, featureSet, tcParameters)
mapValues <- typeCheckMap(values, typeTransform, false, env, featureSet, tcParameters)
} yield {
TypeCheckResponse(
StructT(UMap(mapValues), IdentifierT, RequireCompleteness, BasicMap).asUntagged,
Expand Down Expand Up @@ -357,10 +357,9 @@ object TypeChecker {
for {
_ <- Outcome.failWhen(values.length > 1, "Type transform cannot have multiple values")
value <- Outcome(values.headOption, "Type transform must contain a key and a value")
mapValue <- typeCheckGenericMap(
mapValue <- typeCheckMap(
Vector(value),
TypeTransform(TypeT, TypeT),
//if (allowGenerics) PatternMap else BasicMap,
allowGenerics,
env,
featureSet,
Expand Down Expand Up @@ -389,19 +388,17 @@ object TypeChecker {

inputT <- evalInputType.asType
outputT <- outputType.nExpression.asType
} yield {
val typeTransform = TypeTransform(inputT, outputT)

typeTransform = TypeTransform(inputT, outputT)

// TODO - how do we make the mapConfig customizable in the LambdaParse Symbol?
// - Is there anything we can do with more equals signs, or -> or maybe ~> (I don't know)
val mapConfig = MapConfig(RequireCompleteness, FullFunction)
mapConfig = MapConfig(RequireCompleteness, FullFunction)

TypeCheckResponse(
MapT(typeTransform, mapConfig).asUntagged,
TypeT,
tcParameters
)
}
nObject = NewMapObject(MapT(typeTransform, mapConfig).asUntagged, TypeT)

response <- responseFromConversion(nObject, expectedType, env, tcParameters)
} yield response
}
case ConstructCaseParse(first, second) => {
expectedTypeOutcome match {
Expand Down Expand Up @@ -511,7 +508,7 @@ object TypeChecker {
}
}

def typeCheckGenericMap(
def typeCheckMap(
values: Vector[ParseTree],
typeTransform: TypeTransform,
patternMatchingAllowed: Boolean,
Expand Down Expand Up @@ -552,7 +549,7 @@ object TypeChecker {
resultKey.tcParameters
)

restOfMap <- typeCheckGenericMap(restOfValues, typeTransform, patternMatchingAllowed, env, featureSet, tcParameters)
restOfMap <- typeCheckMap(restOfValues, typeTransform, patternMatchingAllowed, env, featureSet, tcParameters)
} yield {
(foundKeyPattern -> objectFoundValue.nExpression) +: restOfMap
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/ai/newmap/model/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ case class Environment(
))
}
}

newEnv.copy(
newEnv = newEnv.copy(
commands = newCommands,
typeSystem = newTypeSystem
)
Expand Down
11 changes: 5 additions & 6 deletions src/main/scala/ai/newmap/model/PrintNewMapObject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ object PrintNewMapObject {
env: Environment
): Outcome[String, String] = {
for {
displayUnderlyingType <- env.typeSystem.currentUnderlyingType("_display")
displayUnderlyingType <- env.typeSystem.currentUnderlyingType("Displayable")

dislayUnderlyingExpT = displayUnderlyingType._2

// TODO - given the new TypeClassT, the rest of this is nonsense
Expand All @@ -35,12 +36,10 @@ object PrintNewMapObject {
case _ => Failure(s"Not a type class: ${dislayUnderlyingExpT.displayString(env)}")
}

result <- Evaluator.applyFunction(mapValues, nType, env, TypeMatcher) match {
case Success(s) => Success(s)
case Failure(f) => Failure(f.toString)
}
membershipCheck <- Evaluator.applyFunction(mapValues, nType, env, TypeMatcher)
_ <- Outcome.failWhen(membershipCheck == UInit, s"Not member of Displayable typeclass: $nType")

evalResult <- Evaluator(ApplyFunction(result, uObject, StandardMatcher), env)
evalResult <- Evaluator(AccessField(uObject, nType, UIdentifier("display")), env)

finalString <- evalResult match {
case UCase(_, UArray(chars)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,14 +1095,14 @@ class TestFullEnvironmentInterpreter extends FlatSpec {
))
}

it should " be printed nicely" ignore {
it should " be printed nicely" in {
testCodeScript(Vector(
CodeExpectation("val s: String = \"asdf\"", GeneralSuccessCheck),
CodeExpectation("s", SuccessCheckStr("asdf"))
))
}

"The stdout channel " should " work with strings" ignore {
"The stdout channel " should " work with strings" in {
testCodeScript(Vector(
CodeExpectation("ver stdoutRecorder = new String", GeneralSuccessCheck),
CodeExpectation("connectChannel stdout stdoutRecorder", GeneralSuccessCheck),
Expand All @@ -1115,7 +1115,7 @@ class TestFullEnvironmentInterpreter extends FlatSpec {
))
}

"HelloWorld.nm script " should " behave as expected" ignore {
"HelloWorld.nm script " should " behave as expected" in {
testCodeScript(Vector(
CodeExpectation("ver stdoutRecorder = new String", GeneralSuccessCheck),
CodeExpectation("connectChannel stdout stdoutRecorder", GeneralSuccessCheck),
Expand Down

0 comments on commit 72d3ea7

Please sign in to comment.