-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Value implementation that matches finos/morphir-elm and ditches recur…
…sion schemes (#75) * Starting alternate implementation of Value that closer matches Elm * Starting alternate implementation of Value that closer matches Elm * Add more operators to Type and Value * Value module ideas * Implement more ValueModule methods
- Loading branch information
1 parent
f285a5b
commit 0b73053
Showing
13 changed files
with
784 additions
and
49 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
morphir-ir/shared/src/main/scala/zio/morphir/ir/Source.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package zio.morphir.ir | ||
|
||
object Source { | ||
type Located[+A] = source.Located[A] | ||
val Located: source.Located.type = source.Located | ||
|
||
type Location = source.Location | ||
val Location: source.Location.type = source.Location | ||
|
||
type Region = source.Region | ||
val Region: source.Region.type = source.Region | ||
} |
3 changes: 3 additions & 0 deletions
3
morphir-ir/shared/src/main/scala/zio/morphir/ir/source/Located.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package zio.morphir.ir.source | ||
|
||
final case class Located[+A](at: Region, value: A) |
14 changes: 14 additions & 0 deletions
14
morphir-ir/shared/src/main/scala/zio/morphir/ir/source/Location.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package zio.morphir.ir.source | ||
import zio.prelude._ | ||
final case class Location(row: Int, column: Int) | ||
object Location { | ||
val default: Location = Location(0, 0) | ||
val home: Location = Location(0, 0) | ||
|
||
implicit val LocationIdentity: Identity[Location] = new Identity[Location] { | ||
final val identity: Location = home | ||
|
||
override def combine(l: => Location, r: => Location): Location = | ||
Location(row = l.row + r.row, column = l.column + r.column) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
morphir-ir/shared/src/main/scala/zio/morphir/ir/source/Region.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package zio.morphir.ir.source | ||
import zio.prelude._ | ||
final case class Region(start: Location, end: Location) | ||
object Region { | ||
val default: Region = Region(Location.default, Location.default) | ||
implicit val RegionIdentity: Identity[Region] = new Identity[Region] { | ||
lazy val identity: Region = default | ||
|
||
override def combine(l: => Region, r: => Region): Region = Region(l.start <> r.start, l.end <> r.end) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
morphir-ir/shared/src/main/scala/zio/morphir/ir/value/Definition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package zio.morphir.ir.value | ||
|
||
import zio.Chunk | ||
import zio.morphir.ir.Name | ||
import zio.morphir.ir.Pattern.{AsPattern, WildcardPattern} | ||
import zio.morphir.ir.TypeModule.Type | ||
import zio.morphir.ir.value.Value.Lambda | ||
|
||
final case class Definition[+TA, +VA]( | ||
inputTypes: Chunk[(Name, VA, Type[TA])], | ||
outputType: Type[TA], | ||
body: Value[TA, VA] | ||
) { self => | ||
|
||
def mapAttributes[TB, VB](f: TA => TB, g: VA => VB): Definition[TB, VB] = | ||
Definition( | ||
inputTypes.map { case (n, va, t) => (n, g(va), t.mapAttributes(f)) }, | ||
outputType.mapAttributes(f), | ||
body.mapAttributes(f, g) | ||
) | ||
|
||
def toValue: Value[TA, VA] = self.inputTypes.toList match { | ||
case Nil => self.body | ||
case (firstArgName, va, _) :: restOfArgs => | ||
val definition = self.copy(inputTypes = Chunk.fromIterable(restOfArgs)) | ||
Lambda( | ||
attributes = va, | ||
argumentPattern = AsPattern(WildcardPattern(va), firstArgName, va), | ||
body = definition.toValue | ||
) | ||
} | ||
|
||
def toSpecification: Specification[TA] = { | ||
Specification( | ||
inputTypes.map { case (n, _, t) => (n, t) }, | ||
output = self.outputType | ||
) | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
morphir-ir/shared/src/main/scala/zio/morphir/ir/value/Specification.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package zio.morphir.ir.value | ||
|
||
import zio.Chunk | ||
import zio.morphir.ir.Name | ||
import zio.morphir.ir.TypeModule.Type | ||
|
||
final case class Specification[+TA](inputs: Chunk[(Name, Type[TA])], output: Type[TA]) { self => | ||
def map[B](f: TA => B): Specification[B] = | ||
Specification(inputs.map { case (name, tpe) => (name, tpe.mapAttributes(f)) }, output.mapAttributes(f)) | ||
} | ||
|
||
object Specification { | ||
def create[Attributes](inputs: (Name, Type[Attributes])*): Inputs[Attributes] = | ||
new Inputs(() => Chunk.fromIterable(inputs)) | ||
|
||
final class Inputs[Annotations](private val inputs: () => Chunk[(Name, Type[Annotations])]) extends AnyVal { | ||
def apply(output: Type[Annotations]): Specification[Annotations] = | ||
Specification(inputs(), output) | ||
} | ||
} |
Oops, something went wrong.