Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst committed Oct 18, 2023
1 parent 679d43f commit 9b65c1e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 60 deletions.
35 changes: 14 additions & 21 deletions aqua-src/antithesis.aqua
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
aqua M
aqua A

export getObj
export test

service OpNum("op"):
identity(n: u32) -> u32
ability InnerAb:
arrow() -> i8, i8

service OpStr("op"):
identity(n: string) -> string
ability TestAb:
inner: InnerAb

service OpArr("op"):
identity(arr: []string) -> []string
func create() -> TestAb:
arrow = () -> i8, i8:
<- 42, 76
<- TestAb(inner = InnerAb(arrow = arrow))

data InnerObj:
arr: []string
num: u32

data SomeObj:
str: string
num: u64
inner: InnerObj

func getObj() -> SomeObj:
b = SomeObj(str = OpStr.identity("some str"), num = 5, inner = InnerObj(arr = ["a", "b", "c"], num = 6))
c = b.copy(str = "new str", inner = b.inner.copy(num = 3))
<- c
func test() -> i8:
ababab <- create()
res <- ababab.inner.arrow()
<- res
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
abilityType: NamedType,
p: PropertyRaw
): State[S, (VarModel, Inline)] = p match {
case IntoArrowRaw(arrowName, t, arguments) =>
case IntoArrowRaw(arrowName, _, arguments) =>
val arrowType = abilityType.fields
.lookup(arrowName)
.collect { case at @ ArrowType(_, _) =>
Expand Down Expand Up @@ -82,9 +82,9 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
}
}
} yield result
case IntoFieldRaw(fieldName, at @ AbilityType(abName, fields)) =>
case IntoFieldRaw(fieldName, at @ AbilityType(_, _)) =>
(VarModel(AbilityType.fullName(varModel.name, fieldName), at), Inline.empty).pure
case IntoFieldRaw(fieldName, t) =>
case IntoFieldRaw(fieldName, _) =>
for {
abilityField <- Exports[S].getAbilityField(varModel.name, fieldName)
result <- abilityField match {
Expand All @@ -102,6 +102,18 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi

}
} yield result
case icr: IntoCopyRaw =>
internalError(
s"Inlining, cannot use 'copy' ($icr) in ability ($varModel)"
)
case fr: FunctorRaw =>
internalError(
s"Inlining, cannot use functor ($fr) in ability ($varModel)"
)
case iir: IntoIndexRaw =>
internalError(
s"Inlining, cannot use index ($iir) in ability ($varModel)"
)
}

private[inline] def unfoldProperty[S: Mangler: Exports: Arrows](
Expand Down Expand Up @@ -154,6 +166,10 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
mergeMode = SeqMode
)
}
case iar: IntoArrowRaw =>
internalError(
s"Inlining, cannot use arrow ($iar) in non-ability type ($varModel)"
)
}

// Helper for `optimizeProperties`
Expand Down Expand Up @@ -211,7 +227,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
State.pure((vm, prevInline.mergeWith(optimizationInline, SeqMode)))
) { case (state, property) =>
state.flatMap {
case (vm @ Ability(name, at, _), leftInline) =>
case (vm @ Ability(_, at, _), leftInline) =>
unfoldAbilityProperty(vm, at, property.raw).map { case (vm, inl) =>
(
vm,
Expand Down Expand Up @@ -317,8 +333,8 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
raw: ValueRaw,
properties: Chain[PropertyRaw],
propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] = {
((raw, properties.uncons) match {
): State[S, (ValueModel, Inline)] =
(raw, properties.uncons) match {
/**
* To inline
*/
Expand All @@ -338,7 +354,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
propertiesAllowed
)
} yield propsInlined
case l =>
case _ =>
internalError(
s"Unfolded stream ($vr) cannot be a literal"
)
Expand All @@ -362,9 +378,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
}
}
}
})

}
}

/**
* Remove properties from the var and return a new var without them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object CallArrowRawInliner extends RawInliner[CallArrowRaw] with Logging {
private[inline] def unfold[S: Mangler: Exports: Arrows](
value: CallArrowRaw,
exportTo: List[Call.Export]
): State[S, (List[ValueModel], Inline)] = Exports[S].exports.flatMap { exports =>
): State[S, (List[ValueModel], Inline)] = {
logger.trace(s"${exportTo.mkString(" ")} $value")

val call = Call(value.arguments, exportTo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import aqua.model.ValueModel.Ability
import cats.Eval
import cats.data.{Chain, IndexedStateT, NonEmptyMap, State}
import cats.syntax.foldable.*
import cats.syntax.functor.*

object MakeAbilityRawInliner extends RawInliner[AbilityRaw] {

private def updateFields[S: Mangler: Exports: Arrows](
name: String,
fields: NonEmptyMap[String, (ValueModel, Inline)]
): State[S, Unit] = {
for {
res <- fields.toNel.traverse {
case (n, (Ability(abilityName, _, _), _)) =>
val leftName = AbilityType.fullName(name, n)
Exports[S].copyWithAbilityPrefix(abilityName, leftName)
case (n, (vm, _)) =>
Exports[S].resolveAbilityField(name, n, vm)
}
} yield ()
}
): State[S, Unit] =
fields.toNel.traverse {
case (n, (Ability(abilityName, _, _), _)) =>
val leftName = AbilityType.fullName(name, n)
Exports[S].copyWithAbilityPrefix(abilityName, leftName)
case (n, (vm, _)) =>
Exports[S].resolveAbilityField(name, n, vm)
}.as(())

override def apply[S: Mangler: Exports: Arrows](
raw: AbilityRaw,
Expand Down
6 changes: 3 additions & 3 deletions model/raw/src/main/scala/aqua/raw/value/PropertyRaw.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aqua.raw.value

import aqua.types.{StructType, Type}
import aqua.types.{ArrowType, NilType, StructType, Type}
import cats.data.NonEmptyMap

sealed trait PropertyRaw {
Expand All @@ -22,10 +22,10 @@ case class IntoFieldRaw(name: String, `type`: Type) extends PropertyRaw {
override def varNames: Set[String] = Set.empty
}

case class IntoArrowRaw(name: String, arrowType: Type, arguments: List[ValueRaw])
case class IntoArrowRaw(name: String, arrowType: ArrowType, arguments: List[ValueRaw])
extends PropertyRaw {

override def `type`: Type = arrowType
override def `type`: Type = arrowType.codomain.uncons.map(_._1).getOrElse(NilType)

override def map(f: ValueRaw => ValueRaw): PropertyRaw =
copy(arguments = arguments.map(f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import aqua.semantics.Prog
import aqua.semantics.rules.ValuesAlgebra
import aqua.semantics.rules.names.NamesAlgebra
import aqua.semantics.rules.types.TypesAlgebra
import aqua.types.{StreamType, Type}

import aqua.types.{ProductType, StreamType, Type}
import cats.Monad
import cats.syntax.flatMap.*
import cats.syntax.functor.*
Expand All @@ -24,11 +23,11 @@ class CallArrowSem[S[_]](val expr: CallArrowExpr[S]) extends AnyVal {

import expr.*

private def getExports[Alg[_]: Monad](callArrow: CallArrowRaw)(using
private def getExports[Alg[_]: Monad](codomain: ProductType)(using
N: NamesAlgebra[S, Alg],
T: TypesAlgebra[S, Alg]
): Alg[List[Call.Export]] =
(variables zip callArrow.baseType.codomain.toList).traverse { case (v, t) =>
(variables zip codomain.toList).traverse { case (v, t) =>
N.read(v, mustBeDefined = false).flatMap {
case Some(stream @ StreamType(st)) =>
T.ensureTypeMatches(v, st, t).as(Call.Export(v.value, stream))
Expand All @@ -45,7 +44,7 @@ class CallArrowSem[S[_]](val expr: CallArrowExpr[S]) extends AnyVal {
// TODO: Accept other expressions
callArrowRaw <- V.valueToCallArrowRaw(expr.callArrow)
tag <- callArrowRaw.traverse(car =>
getExports(car).map(CallArrowRawTag(_, car)) <*
getExports(car.baseType.codomain).map(CallArrowRawTag(_, car)) <*
T.checkArrowCallResults(callArrow, car.baseType, variables)
)
} yield tag.map(_.funcOpLeaf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,19 @@ class TypesInterpreter[S[_], X](using
s"Arrow `${op.name.value}` not found in type `$name`, available: ${fieldsAndArrows.toNel.toList.map(_._1).mkString(", ")}"
)
.as(None)
) { t =>
val resolvedType = t match {
// TODO: is it a correct way to resolve `IntoArrow` type?
case ArrowType(_, codomain) => codomain.uncons.map(_._1).getOrElse(t)
case _ => t
}
locations
.pointFieldLocation(name, op.name.value, op)
.as(Some(IntoArrowRaw(op.name.value, resolvedType, arguments)))
) {
case at@ArrowType(_, _) =>
locations
.pointFieldLocation(name, op.name.value, op)
.as(Some(IntoArrowRaw(op.name.value, at, arguments)))
case _ =>
// Unexpected
report
.error(
op,
s"`${op.name.value}` must be an arrow."
)
.as(None)
}
case t =>
t.properties
Expand Down

0 comments on commit 9b65c1e

Please sign in to comment.