Skip to content

Commit

Permalink
Minor Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Mar 29, 2024
1 parent ae16336 commit cec5ca7
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 189 deletions.
32 changes: 8 additions & 24 deletions modules/scala/shared/src/main/scala/dc10/scala/Symbol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ object Symbol:
case class IntType() extends Var[Int]
case class StringType() extends Var[String]
case class UnitType() extends Var[Unit]
case class ListType[A]() extends Var[A]
case class OptionType[A]() extends Var[A]
case class SomeType[A]() extends Var[A]
case class TupleType[A]() extends Var[A]
case class ListType[A](a: TypeLevel[A]) extends Var[List[A]]
case class OptionType[A](a: TypeLevel[A]) extends Var[Option[A]]
case class SomeType[A](a: TypeLevel[A]) extends Var[Option[A]]
case class TupleType[A, B](a: TypeLevel[A], b: TypeLevel[B]) extends Var[(A, B)]
case class UserDefinedType[T](nme: String, impl: Option[TypeLevel[T]]) extends Var[T]

sealed trait ValueLevel[T] extends Term
Expand All @@ -112,8 +112,8 @@ object Symbol:
sealed abstract class App[T] extends Term.ValueLevel[T]
object App:
case class App1[A, B](fun: ValueLevel[A => B], arg: ValueLevel[A], tpe: TypeLevel[B]) extends Term.ValueLevel.App[B]
case class App2[A, B, C](fun: ValueLevel[(A, B) => C], arg: ValueLevel[A], arg2: ValueLevel[B], tpe: TypeLevel[C]) extends Term.ValueLevel.App[C]
case class AppCtor1[T, A](tpe: TypeLevel[T], arg: ValueLevel[A]) extends Term.ValueLevel.App[T]
case class AppCtor2[T, A, B](nme: String, tpe: TypeLevel[T], arg1: ValueLevel[A], arg2: ValueLevel[B]) extends Term.ValueLevel.App[T]
case class AppPure[G[_], A](fun: ValueLevel[G[A]], arg: ValueLevel[A], tpe: TypeLevel[G[A]]) extends Term.ValueLevel.App[G[A]]
case class AppVargs[G[_], A](fun: ValueLevel[G[A]], tpe: TypeLevel[G[A]], vargs: ValueLevel[A]*) extends Term.ValueLevel.App[G[A]]
case class Dot1[A, B, C, D](fun: ValueLevel[D], arg1: ValueLevel[A], arg2: ValueLevel[B], tpe: TypeLevel[C]) extends Term.ValueLevel.App[C]
Expand All @@ -131,18 +131,14 @@ object Symbol:
case class IntLiteral(tpe: TypeLevel[Int], i: Int) extends Var[Int]
case class StringLiteral(tpe: TypeLevel[String], s: String) extends Var[String]
case class UnitLiteral(tpe: TypeLevel[Unit], u: Unit) extends Var[Unit]
case class ListCtor[A](tpe: TypeLevel[A]) extends Var[A]
case class OptionCtor[A](tpe: TypeLevel[A]) extends Var[A]
case class SomeCtor[A](tpe: TypeLevel[A]) extends Var[A]
case class TupleCtor[A, B](tpe: TypeLevel[Tuple2[A, B]]) extends Var[(A, B)]
case class UserDefinedValue[T](nme: String, tpe: TypeLevel[T], impl: Option[ValueLevel[T]]) extends Var[T]

extension [T] (v: ValueLevel[T])
def tpe: TypeLevel[T] =
v match
case Term.ValueLevel.App.App1(fun, arg, tpe) => tpe
case Term.ValueLevel.App.App2(fun, arg1, arg2, tpe) => tpe
case Term.ValueLevel.App.AppCtor1(tpe, arg) => tpe
case Term.ValueLevel.App.AppCtor2(nme, tpe, arg1, arg2) => tpe
case Term.ValueLevel.App.AppPure(fun, arg, tpe) => tpe
case Term.ValueLevel.App.AppVargs(fun, tpe, vargs*) => tpe
case Term.ValueLevel.App.Dot1(fun, arg1, arg2, tpe) => tpe
Expand All @@ -154,19 +150,15 @@ object Symbol:
case Term.ValueLevel.Var.IntLiteral(tpe, i) => tpe
case Term.ValueLevel.Var.StringLiteral(tpe, s) => tpe
case Term.ValueLevel.Var.UnitLiteral(tpe, u) => tpe
case Term.ValueLevel.Var.ListCtor(tpe) => tpe
case Term.ValueLevel.Var.OptionCtor(tpe) => tpe
case Term.ValueLevel.Var.SomeCtor(tpe) => tpe
case Term.ValueLevel.Var.TupleCtor(tpe) => tpe
case Term.ValueLevel.Var.UserDefinedValue(nme, tpe, impl) => tpe

extension [T] (v: Term.ValueLevel[T])

def findImpl: Option[Term.ValueLevel[T]] =
v match
case Term.ValueLevel.App.App1(fun, arg, tpe) => Some(v)
case Term.ValueLevel.App.App2(fun, arg1, arg2, tpe) => Some(v)
case Term.ValueLevel.App.AppCtor1(tpe, arg) => Some(v)
case Term.ValueLevel.App.AppCtor2(nme, tpe, arg1, arg2) => Some(v)
case Term.ValueLevel.App.AppPure(fun, arg, tpe) => Some(v)
case Term.ValueLevel.App.AppVargs(fun, tpe, vargs*) => Some(v)
case Term.ValueLevel.App.Dot1(fun, arg1, arg2, tpe) => Some(v)
Expand All @@ -178,17 +170,13 @@ object Symbol:
case Term.ValueLevel.Var.IntLiteral(tpe, i) => Some(v)
case Term.ValueLevel.Var.StringLiteral(tpe, s) => Some(v)
case Term.ValueLevel.Var.UnitLiteral(tpe, s) => Some(v)
case Term.ValueLevel.Var.ListCtor(tpe) => Some(v)
case Term.ValueLevel.Var.OptionCtor(tpe) => Some(v)
case Term.ValueLevel.Var.SomeCtor(tpe) => Some(v)
case Term.ValueLevel.Var.TupleCtor(tpe) => Some(v)
case u@Term.ValueLevel.Var.UserDefinedValue(nme, tpe, impl) => impl.fold(None)(i => i.findImpl)

def findVargs[A]: Option[Seq[Term.ValueLevel[A]]] =
v.findImpl.fold(None)(i => i match
case Term.ValueLevel.App.App1(fun, arg, tpe) => None
case Term.ValueLevel.App.App2(fun, arg1, arg2, tpe) => None
case Term.ValueLevel.App.AppCtor1(tpe, arg) => None
case Term.ValueLevel.App.AppCtor2(nme, tpe, arg1, arg2) => None
case Term.ValueLevel.App.AppPure(fun, arg, tpe) => None
case Term.ValueLevel.App.AppVargs(fun, tpe, vargs*) => Some(vargs.asInstanceOf[Seq[Term.ValueLevel[A]]])
case Term.ValueLevel.App.Dot1(fun, arg1, arg2, tpe) => None
Expand All @@ -200,9 +188,5 @@ object Symbol:
case Term.ValueLevel.Var.IntLiteral(tpe, i) => None
case Term.ValueLevel.Var.StringLiteral(tpe, s) => None
case Term.ValueLevel.Var.UnitLiteral(tpe, s) => None
case Term.ValueLevel.Var.ListCtor(tpe) => None
case Term.ValueLevel.Var.OptionCtor(tpe) => None
case Term.ValueLevel.Var.SomeCtor(tpe) => None
case Term.ValueLevel.Var.TupleCtor(tpe) => None
case Term.ValueLevel.Var.UserDefinedValue(nme, tpe, impl) => None
)
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ object Applications:
case Term.TypeLevel.App.Infix(tfun, ta, tb) => Right(tb.asInstanceOf[Term.TypeLevel[B]])
case Term.TypeLevel.Lam.Function1Type() => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
case Term.TypeLevel.Var.UserDefinedType(nme, impl) => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
case Term.TypeLevel.Var.ListType() => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
case Term.TypeLevel.Var.OptionType() => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
case Term.TypeLevel.Var.SomeType() => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
case Term.TypeLevel.Var.TupleType() => Left(List(Error(s"${sp.file}:${sp.line}\nApplication Error")))
)
yield ValueExpr(Term.ValueLevel.App.App1(f.value, a.value, t))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,24 @@ import dc10.scala.Symbol.{Extension, Term}
import dc10.scala.Symbol.Term.{TypeLevel, ValueLevel}
import dc10.scala.Symbol.Term.TypeLevel.App.Infix
import org.tpolecat.sourcepos.SourcePos
import dc10.scala.Statement.TypeDef.Match
import dc10.scala.Symbol.Term.ValueLevel.Blc.ForComp
import dc10.scala.Symbol.Term.ValueLevel.App.App1
import dc10.scala.Symbol.Term.ValueLevel.App.AppCtor1
import dc10.scala.Symbol.Term.ValueLevel.App.AppCtor2
import dc10.scala.Symbol.Term.ValueLevel.App.AppPure
import dc10.scala.Symbol.Term.ValueLevel.App.AppVargs
import dc10.scala.Symbol.Term.ValueLevel.App.Dot1
import dc10.scala.Symbol.Term.ValueLevel.App.Dotless
import dc10.scala.Symbol.Term.ValueLevel.Lam.Lam1
import dc10.scala.Symbol.Term.ValueLevel.Lam.Lam2
import dc10.scala.Symbol.Term.ValueLevel.Var.BooleanLiteral
import dc10.scala.Symbol.Term.ValueLevel.Var.IntLiteral
import dc10.scala.Symbol.Term.ValueLevel.Var.StringLiteral
import dc10.scala.Symbol.Term.ValueLevel.Var.ListCtor
import dc10.scala.Symbol.Term.ValueLevel.Var.OptionCtor
import dc10.scala.Symbol.Term.ValueLevel.Var.SomeCtor
import dc10.scala.Symbol.Term.ValueLevel.Var.UserDefinedValue

trait Functions[F[_]]:

extension [A, B] (domain: F[TypeExpr[A]])
@scala.annotation.targetName("fun1T")
def ==>(codomain: F[TypeExpr[B]]): F[TypeExpr[A => B]]

extension [A, B] (domain: F[(TypeExpr[A], TypeExpr[A])])
extension [A, B, C] (domain: F[(TypeExpr[A], TypeExpr[B])])
@scala.annotation.targetName("fun2T")
def ==>(codomain: F[TypeExpr[B]]): F[TypeExpr[(A, A) => B]]
def ==>(codomain: F[TypeExpr[C]]): F[TypeExpr[(A, B) => C]]

extension [A, B] (fa: F[ValueExpr[A]])
@scala.annotation.targetName("fun1V")
def ==>(f: ValueExpr[A] => F[ValueExpr[B]]): F[ValueExpr[A => B]]

extension [A, B] (fa: F[(ValueExpr[A], ValueExpr[A])])
extension [A, B, C] (fa: F[(ValueExpr[A], ValueExpr[B])])
@scala.annotation.targetName("fun2V")
def ==>(f: (ValueExpr[A], ValueExpr[A]) => F[ValueExpr[B]]): F[ValueExpr[(A, A) => B]]
def ==>(f: (ValueExpr[A], ValueExpr[B]) => F[ValueExpr[C]]): F[ValueExpr[(A, B) => C]]

def EXT[G[_], B](func: F[G[B]])(using sp: SourcePos): F[G[B]]

Expand Down Expand Up @@ -78,18 +60,17 @@ object Functions:
b.tpe,
)
)

yield TypeExpr(t)

extension [A, B] (domain: StateT[ErrorF, List[Statement], (TypeExpr[A], TypeExpr[A])])
extension [A, B, C] (domain: StateT[ErrorF, List[Statement], (TypeExpr[A], TypeExpr[B])])
@scala.annotation.targetName("fun2T")
def ==>(
codomain: StateT[ErrorF, List[Statement], TypeExpr[B]]
): StateT[ErrorF, List[Statement], TypeExpr[(A, A) => B]] =
codomain: StateT[ErrorF, List[Statement], TypeExpr[C]]
): StateT[ErrorF, List[Statement], TypeExpr[(A, B) => C]] =
for
a <- domain
b <- codomain
v <- StateT.pure[ErrorF, List[Statement], TypeLevel[(A, A) => B]](
v <- StateT.pure[ErrorF, List[Statement], TypeLevel[(A, B) => C]](
Term.TypeLevel.App.App3(
Term.TypeLevel.Lam.Function2Type(),
a._1.tpe,
Expand All @@ -114,22 +95,18 @@ object Functions:
v <- StateT.pure[ErrorF, List[Statement], ValueLevel[A => B]](Term.ValueLevel.Lam.Lam1(a.value, b.value, t))
yield ValueExpr(v)

extension [A, B] (fa: StateT[ErrorF, List[Statement], (ValueExpr[A], ValueExpr[A])])
extension [A, B, C] (fa: StateT[ErrorF, List[Statement], (ValueExpr[A], ValueExpr[B])])
@scala.annotation.targetName("fun2V")
def ==>(
f: (ValueExpr[A], ValueExpr[A]) => StateT[ErrorF, List[Statement], ValueExpr[B]]
): StateT[ErrorF, List[Statement], ValueExpr[(A, A) => B]] =
f: (ValueExpr[A], ValueExpr[B]) => StateT[ErrorF, List[Statement], ValueExpr[C]]
): StateT[ErrorF, List[Statement], ValueExpr[(A, B) => C]] =
for
a <- StateT.liftF(fa.runEmptyA)
b <- f(a._1, a._2)
t <- StateT.pure[ErrorF, List[Statement], TypeLevel[(A, A) => B]](Term.TypeLevel.App.App3(Term.TypeLevel.Lam.Function2Type(), a._1.value.tpe, a._2.value.tpe, b.value.tpe))
v <- StateT.pure[ErrorF, List[Statement], ValueLevel[(A, A) => B]](Term.ValueLevel.Lam.Lam2(a._1.value, a._2.value, b.value, t))
t <- StateT.pure[ErrorF, List[Statement], TypeLevel[(A, B) => C]](Term.TypeLevel.App.App3(Term.TypeLevel.Lam.Function2Type(), a._1.value.tpe, a._2.value.tpe, b.value.tpe))
v <- StateT.pure[ErrorF, List[Statement], ValueLevel[(A, B) => C]](Term.ValueLevel.Lam.Lam2(a._1.value, a._2.value, b.value, t))
yield ValueExpr(v)





def EXT[G[_], B](
func: StateT[ErrorF, List[Statement], G[B]]
)(using sp: SourcePos): StateT[ErrorF, List[Statement], G[B]] =
Expand All @@ -148,8 +125,8 @@ object Functions:
for
(l, a) <- StateT.liftF(f.runEmpty)
v <- StateT.pure[ErrorF, List[Statement], ValueLevel[Option[A]]](
ForComp(l, a.value, Term.TypeLevel.App.App1(Term.TypeLevel.Var.OptionType(), a.value.tpe,
)))
Term.ValueLevel.Blc.ForComp(l, a.value, Term.TypeLevel.App.App1(Term.TypeLevel.Var.OptionType(a.value.tpe), a.value.tpe,
)))
yield ValueExpr(v)

extension [G[_], A] (nme: String)
Expand All @@ -169,16 +146,16 @@ object Functions:
case dc10.scala.Symbol.Term.TypeLevel.Var.IntType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.StringType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.UnitType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.ListType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.OptionType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.SomeType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.TupleType() => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.ListType(arg) => Right(arg.asInstanceOf[TypeLevel[A]])
case dc10.scala.Symbol.Term.TypeLevel.Var.OptionType(arg) => Right(arg.asInstanceOf[TypeLevel[A]])
case dc10.scala.Symbol.Term.TypeLevel.Var.SomeType(arg) => Right(arg.asInstanceOf[TypeLevel[A]])
case dc10.scala.Symbol.Term.TypeLevel.Var.TupleType(arg1, arg2) => ???
case dc10.scala.Symbol.Term.TypeLevel.Var.UserDefinedType(nme, impl) => ???
)
i <- StateT.liftF[ErrorF, List[Statement], ValueLevel[A]](g.value.findImpl.fold(Left(List(Error(""))))(i => i match
case dc10.scala.Symbol.Term.ValueLevel.App.App1(fun, arg, tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.App.App1(fun, arg, tpe) => Right(arg.asInstanceOf[ValueLevel[A]])
case dc10.scala.Symbol.Term.ValueLevel.App.App2(fun, arg1, arg2, tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.App.AppCtor1(tpe, arg) => ???
case dc10.scala.Symbol.Term.ValueLevel.App.AppCtor2(nme, tpe, arg1, arg2) => ???
case dc10.scala.Symbol.Term.ValueLevel.App.AppPure(fun, arg, tpe) => Right(arg)
case dc10.scala.Symbol.Term.ValueLevel.App.AppVargs(fun, tpe, vargs*) => ???
case dc10.scala.Symbol.Term.ValueLevel.App.Dot1(fun, arg1, arg2, tpe) => ???
Expand All @@ -190,10 +167,6 @@ object Functions:
case dc10.scala.Symbol.Term.ValueLevel.Var.IntLiteral(tpe, i) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.StringLiteral(tpe, s) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.UnitLiteral(tpe, s) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.ListCtor(tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.OptionCtor(tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.SomeCtor(tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.TupleCtor(tpe) => ???
case dc10.scala.Symbol.Term.ValueLevel.Var.UserDefinedValue(nme, tpe, impl) => ???
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.tpolecat.sourcepos.SourcePos
trait Variables[F[_]]:
def DEF[A, T](nme: String, arg: F[ValueExpr[A]], tpe: F[TypeExpr[T]])(using sp: SourcePos): F[ValueExpr[A => T]]
def DEF[A, T](nme: String, arg: F[ValueExpr[A]], tpe: F[TypeExpr[T]], impl: ValueExpr[A] => F[ValueExpr[T]])(using sp: SourcePos): F[ValueExpr[A => T]]
def DEF[A, T](nme: String, arg1: F[ValueExpr[A]], arg2: F[ValueExpr[A]], tpe: F[TypeExpr[T]], impl: (ValueExpr[A], ValueExpr[A]) => F[ValueExpr[T]])(using sp: SourcePos): F[ValueExpr[(A, A) => T]]
def DEF[A, B, T](nme: String, arg1: F[ValueExpr[A]], arg2: F[ValueExpr[B]], tpe: F[TypeExpr[T]], impl: (ValueExpr[A], ValueExpr[B]) => F[ValueExpr[T]])(using sp: SourcePos): F[ValueExpr[(A, B) => T]]
def TYPE[T](nme: String): F[TypeExpr[T]]
def TYPE[T](nme: String, impl: F[TypeExpr[T]]): F[TypeExpr[T]]
def VAL[T, A](nme: String, tpe: F[TypeExpr[T]])(using sp: SourcePos): F[ValueExpr[T]]
Expand Down Expand Up @@ -57,20 +57,20 @@ object Variables:
_ <- StateT.modifyF[ErrorF, List[Statement]](ctx => ctx.ext(d))
yield ValueExpr(v)

def DEF[A, T](
def DEF[A, B, T](
nme: String,
arg1: StateT[ErrorF, List[Statement], ValueExpr[A]],
arg2: StateT[ErrorF, List[Statement], ValueExpr[A]],
arg2: StateT[ErrorF, List[Statement], ValueExpr[B]],
tpe: StateT[ErrorF, List[Statement], TypeExpr[T]],
impl: (ValueExpr[A], ValueExpr[A]) => StateT[ErrorF, List[Statement], ValueExpr[T]]
)(using sp: SourcePos): StateT[ErrorF, List[Statement], ValueExpr[(A, A) => T]] =
impl: (ValueExpr[A], ValueExpr[B]) => StateT[ErrorF, List[Statement], ValueExpr[T]]
)(using sp: SourcePos): StateT[ErrorF, List[Statement], ValueExpr[(A, B) => T]] =
for
a <- StateT.liftF(arg1.runEmptyA)
b <- StateT.liftF(arg2.runEmptyA)
r <- StateT.liftF(tpe.runEmptyA)
i <- impl(a, b)
t <- StateT.pure[ErrorF, List[Statement], (TypeExpr[A], TypeExpr[A])]((TypeExpr[A](a.value.tpe), TypeExpr[A](b.value.tpe))) ==> tpe
f <- StateT.pure[ErrorF, List[Statement], (ValueExpr[A], ValueExpr[A])]((a, a)) ==> impl
t <- StateT.pure[ErrorF, List[Statement], (TypeExpr[A], TypeExpr[B])]((TypeExpr[A](a.value.tpe), TypeExpr[B](b.value.tpe))) ==> tpe
f <- StateT.pure[ErrorF, List[Statement], (ValueExpr[A], ValueExpr[B])]((a, b)) ==> impl
v <- StateT.pure(Term.ValueLevel.Var.UserDefinedValue(nme, t.tpe, Some(f.value)))
d <- StateT.pure[ErrorF, List[Statement], ValueDef](ValueDef.Def(0, v, a.value, r.tpe, Some(i.value)))
_ <- StateT.modifyF[ErrorF, List[Statement]](ctx => ctx.ext(d))
Expand Down
Loading

0 comments on commit cec5ca7

Please sign in to comment.