Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

choice name overloading in lf/interface #13938

Merged
merged 31 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6d1eff1
introduce TemplateChoices to mediate template choice resolution
S11001001 May 19, 2022
56baad0
stub out TemplateChoices test
S11001001 May 23, 2022
d0db5be
Merge commit '1343dcd827012a334a6378a9d552c2c16df02e4f' into 13918-lf…
S11001001 May 23, 2022
8f14792
make TemplateChoices test compile
S11001001 May 23, 2022
ae412c5
replacing DefTemplate choices
S11001001 May 23, 2022
6775a71
Merge commit '49a5474232b59a8f438042c80e487c92eb3ef819' into 13918-lf…
S11001001 May 24, 2022
0eae9a7
adapt DefTemplate
S11001001 May 24, 2022
6492abc
read TemplateChoices from ast
S11001001 May 24, 2022
f88df38
report a bigger error, with a more helpful stringification
S11001001 May 24, 2022
3f5ed0d
fix infinite loop
S11001001 May 24, 2022
9c22a8e
if any resolutions fail in EnvironmentInterface, discard the template…
S11001001 May 24, 2022
6db12ca
if any resolutions fail in Interface, discard the remaining unresolved
S11001001 May 24, 2022
52cb05a
interface tests compile with the new choices structure
S11001001 May 24, 2022
30c62cb
fix choice resolution checks
S11001001 May 24, 2022
68bd517
simple downstream signatures, a shim approach for Java codegen
S11001001 May 24, 2022
ea4a2d9
move assumeOneChoicePerName to assumeNoOverloadedChoices method
S11001001 May 24, 2022
e2322ed
navigator, java, scala codegen compile
S11001001 May 24, 2022
b577087
script runner compiles
S11001001 May 24, 2022
5755985
no changelog
S11001001 May 24, 2022
492d8f0
Merge commit 'd3264236ba9e5ebef6f44731949114a2baef5d4a' into 13918-lf…
S11001001 May 24, 2022
f4b43d5
change issue # for script
S11001001 May 24, 2022
d015616
docs explain new behavior for resolve functions
S11001001 May 24, 2022
a92e8c2
optimize the common unresolved case
S11001001 May 24, 2022
964bff4
test the choice resolution map's extra information
S11001001 May 24, 2022
fb4c2e0
properly test TemplateChoices traverse
S11001001 May 24, 2022
740defc
add some private markers to keep the namespace clean
S11001001 May 24, 2022
74a732f
one possible direction for unresolved choices
S11001001 May 27, 2022
6a79ea4
another possible direction for unresolved choices
S11001001 May 27, 2022
370bdbf
much more direct approach based on the map monoid
S11001001 May 27, 2022
955b300
groupMap1 for NonEmpty
S11001001 May 27, 2022
9ccdca2
fix tests for multiple choices
S11001001 May 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
introduce TemplateChoices to mediate template choice resolution
  • Loading branch information
S11001001 committed May 19, 2022
commit 6d1eff10924e12629655cf3e8ee183d646a4b470
1 change: 1 addition & 0 deletions daml-lf/interface/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ da_scala_library(
"//daml-lf/archive:daml_lf_archive_reader",
"//daml-lf/data",
"//daml-lf/language",
"//libs-scala/nonempty",
"@maven//:com_google_protobuf_protobuf_java",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import scalaz.std.tuple._
import scalaz.syntax.applicative.^
import scalaz.syntax.semigroup._
import scalaz.syntax.traverse._
import scalaz.syntax.std.map._
import scalaz.{Applicative, Bifunctor, Bitraverse, Bifoldable, Foldable, Functor, Monoid, Traverse}
import java.{util => j}

import com.daml.lf.data.ImmArray.ImmArraySeq
import com.daml.lf.data.Ref
import com.daml.nonempty.NonEmpty

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -200,8 +202,7 @@ final case class DefTemplate[+Ty](
this.copy(choices = choices ++ resolved, unresolvedInheritedChoices = missing.toMap)
}

def getKey: j.Optional[_ <: Ty] =
key.fold(j.Optional.empty[Ty])(k => j.Optional.of(k))
def getKey: j.Optional[_ <: Ty] = toOptional(key)
}

object DefTemplate {
Expand All @@ -227,6 +228,62 @@ object DefTemplate {
}
}

/** Choices in a [[DefTemplate]]. */
sealed abstract class TemplateChoices[+Ty] {

/** Choices defined directly on the template */
def directChoices: Map[Ref.ChoiceName, TemplateChoice[Ty]]

/** Choices defined on the template, or on resolved implemented interfaces if
* resolved
*/
def resolvedChoices
: Map[Ref.ChoiceName, NonEmpty[Map[Option[Ref.Identifier], TemplateChoice[Ty]]]]

final def getDirectChoices: j.Map[Ref.ChoiceName, _ <: TemplateChoice[Ty]] =
directChoices.asJava

final def getResolvedChoices
: j.Map[Ref.ChoiceName, _ <: j.Map[j.Optional[Ref.Identifier], _ <: TemplateChoice[Ty]]] =
resolvedChoices.transform((_, m) => m.forgetNE.mapKeys(toOptional).asJava).asJava
}

object TemplateChoices {
final case class Unresolved[+Ty](
directChoices: Map[Ref.ChoiceName, TemplateChoice[Ty]],
unresolvedInheritedChoices: NonEmpty[Map[Ref.ChoiceName, Ref.TypeConName]],
) extends TemplateChoices[Ty] {
override def resolvedChoices =
directChoices transform ((_, c) => NonEmpty(Map, (none[Ref.Identifier], c)))
}

final case class Resolved[+Ty](
resolvedChoices: Map[Ref.ChoiceName, NonEmpty[
Map[Option[Ref.Identifier], TemplateChoice[Ty]]
]]
) extends TemplateChoices[Ty] {
override def directChoices = resolvedChoices collect (Function unlift { case (cn, m) =>
m get None map ((cn, _))
})
}

implicit val `TemplateChoices traverse`: Traverse[TemplateChoices] = new Traverse[TemplateChoices]
with Foldable.FromFoldMap[TemplateChoices] {
override def foldMap[A, B: Monoid](fa: TemplateChoices[A])(f: A => B): B = fa match {
case Unresolved(direct, _) => direct foldMap (_ foldMap f)
case Resolved(resolved) => resolved foldMap (_.toNEF foldMap (_ foldMap f))
}

override def traverseImpl[G[_]: Applicative, A, B](
fa: TemplateChoices[A]
)(f: A => G[B]): G[TemplateChoices[B]] = fa match {
case u @ Unresolved(_, _) =>
u.directChoices traverse (_ traverse f) map (dc => u.copy(directChoices = dc))
case Resolved(r) => r traverse (_.toNEF traverse (_ traverse f)) map (Resolved(_))
}
}
}

final case class TemplateChoice[+Ty](param: Ty, consuming: Boolean, returnType: Ty) {
def map[C](f: Ty => C): TemplateChoice[C] =
Functor[TemplateChoice].map(this)(f)
Expand Down