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
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
Prev Previous commit
Next Next commit
replacing DefTemplate choices
  • Loading branch information
S11001001 committed May 23, 2022
commit ae412c5db7cacc49f1d2e7c3d77c30b6e9e8b9ec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.{util => j}
import com.daml.lf.data.ImmArray.ImmArraySeq
import com.daml.lf.data.Ref
import com.daml.nonempty.NonEmpty
import com.daml.nonempty.NonEmptyReturningOps._

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -176,13 +177,15 @@ final case class Enum(constructors: ImmArraySeq[Ref.Name]) extends DataType[Noth
}

final case class DefTemplate[+Ty](
choices: Map[Ref.ChoiceName, TemplateChoice[Ty]],
unresolvedInheritedChoices: Map[Ref.ChoiceName, Ref.TypeConName],
tChoices: TemplateChoices[Ty],
key: Option[Ty],
implementedInterfaces: Seq[Ref.TypeConName],
) extends DefTemplate.GetChoices[Ty] {
def map[B](f: Ty => B): DefTemplate[B] = Functor[DefTemplate].map(this)(f)

@deprecated("use tChoices.directChoices or tChoices.resolvedChoices instead", since = "2.3.0")
def choices = tChoices.directChoices

/** Remove choices from `unresolvedInheritedChoices` and add to `choices`
* given the `astInterfaces` from an [[EnvironmentInterface]]. If the result
* has any `unresolvedInheritedChoices` left, these choices were not found.
Expand Down Expand Up @@ -230,6 +233,7 @@ object DefTemplate {

/** Choices in a [[DefTemplate]]. */
sealed abstract class TemplateChoices[+Ty] {
import TemplateChoices.{Resolved, Unresolved}

/** Choices defined directly on the template */
def directChoices: Map[Ref.ChoiceName, TemplateChoice[Ty]]
Expand All @@ -238,14 +242,39 @@ sealed abstract class TemplateChoices[+Ty] {
* resolved
*/
def resolvedChoices
: Map[Ref.ChoiceName, NonEmpty[Map[Option[Ref.Identifier], TemplateChoice[Ty]]]]
: Map[Ref.ChoiceName, NonEmpty[Map[Option[Ref.TypeConName], 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]]] =
: j.Map[Ref.ChoiceName, _ <: j.Map[j.Optional[Ref.TypeConName], _ <: TemplateChoice[Ty]]] =
resolvedChoices.transform((_, m) => m.forgetNE.mapKeys(toOptional).asJava).asJava

/** Coerce to [[Resolved]] based on the environment `astInterfaces`, or fail
* with the choices that could not be resolved.
*/
def resolveChoices[O >: Ty](
astInterfaces: PartialFunction[Ref.TypeConName, DefInterface[O]]
): Either[NonEmpty[Map[Ref.ChoiceName, Ref.TypeConName]], Resolved[O]] = this match {
case u @ Unresolved(direct, unresolved) =>
val getAstInterface = astInterfaces.lift
val (missing, resolved) = unresolved.partitionMap { case pair @ (choiceName, tcn) =>
val resolution = for {
astIf <- getAstInterface(tcn)
tchoice <- astIf.choices get choiceName
} yield (choiceName, (some(tcn), tchoice))
resolution toRight pair
}
missing match {
case NonEmpty(missing) => Left(missing.toMap)
case _ =>
val indirectGrouped =
resolved.groupBy1(_._1).transform { (_, ics) => ics.map(_._2).toMap }
Right(Resolved(indirectGrouped.unionWith(u.resolvedChoices)(_ ++ _)))
}
case r @ Resolved(_) => Right(r)
}
}

object TemplateChoices {
Expand All @@ -254,12 +283,12 @@ object TemplateChoices {
unresolvedInheritedChoices: NonEmpty[Map[Ref.ChoiceName, Ref.TypeConName]],
) extends TemplateChoices[Ty] {
override def resolvedChoices =
directChoices transform ((_, c) => NonEmpty(Map, (none[Ref.Identifier], c)))
directChoices transform ((_, c) => NonEmpty(Map, (none[Ref.TypeConName], c)))
}

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