Skip to content

Commit

Permalink
Interface: Rename fixedChoice to choice (#13883)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
remyhaemmerle-da authored May 17, 2022
1 parent fee168c commit ea0d101
Showing 27 changed files with 51 additions and 52 deletions.
2 changes: 1 addition & 1 deletion compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Base.hs
Original file line number Diff line number Diff line change
@@ -960,7 +960,7 @@ data DefInterface = DefInterface
, intName :: !TypeConName
, intRequires :: !(S.Set (Qualified TypeConName))
, intParam :: !ExprVarName
, intFixedChoices :: !(NM.NameMap TemplateChoice)
, intChoices :: !(NM.NameMap TemplateChoice)
, intMethods :: !(NM.NameMap InterfaceMethod)
, intPrecondition :: !Expr
}
2 changes: 1 addition & 1 deletion compiler/daml-lf-ast/src/DA/Daml/LF/Ast/World.hs
Original file line number Diff line number Diff line change
@@ -160,7 +160,7 @@ lookupInterfaceChoice :: (Qualified TypeConName, ChoiceName) -> World ->
lookupInterfaceChoice (ifaceRef, chName) world = do
DefInterface{..} <- lookupInterface ifaceRef world
maybeToEither (LEChoice ifaceRef chName) $
NM.lookup chName intFixedChoices
NM.lookup chName intChoices

lookupInterfaceMethod :: (Qualified TypeConName, MethodName) -> World -> Either LookupError InterfaceMethod
lookupInterfaceMethod (ifaceRef, methodName) world = do
2 changes: 1 addition & 1 deletion compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs
Original file line number Diff line number Diff line change
@@ -238,7 +238,7 @@ decodeDefInterface LF1.DefInterface {..} = do
intName <- decodeDottedNameId TypeConName defInterfaceTyconInternedDname
intRequires <- decodeSet DuplicateRequires decodeTypeConName defInterfaceRequires
intParam <- decodeNameId ExprVarName defInterfaceParamInternedStr
intFixedChoices <- decodeNM DuplicateChoice decodeChoice defInterfaceFixedChoices
intChoices <- decodeNM DuplicateChoice decodeChoice defInterfaceChoices
intMethods <- decodeNM DuplicateMethod decodeInterfaceMethod defInterfaceMethods
intPrecondition <- mayDecode "defInterfacePrecond" defInterfacePrecond decodeExpr
pure DefInterface {..}
2 changes: 1 addition & 1 deletion compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs
Original file line number Diff line number Diff line change
@@ -1020,7 +1020,7 @@ encodeDefInterface DefInterface{..} = do
defInterfaceMethods <- encodeNameMap encodeInterfaceMethod intMethods
defInterfaceParamInternedStr <- encodeNameId unExprVarName intParam
defInterfacePrecond <- encodeExpr intPrecondition
defInterfaceFixedChoices <- encodeNameMap encodeTemplateChoice intFixedChoices
defInterfaceChoices <- encodeNameMap encodeTemplateChoice intChoices
pure $ P.DefInterface{..}

encodeInterfaceMethod :: InterfaceMethod -> Encode P.InterfaceMethod
4 changes: 2 additions & 2 deletions compiler/daml-lf-tools/src/DA/Daml/LF/Completer.hs
Original file line number Diff line number Diff line change
@@ -32,5 +32,5 @@ completeTemplateImplements :: LF.World -> LF.TemplateImplements -> LF.TemplateIm
completeTemplateImplements world tpi@TemplateImplements{..} =
case lookupInterface tpiInterface world of
Left _ -> error ("Could not find interface " <> T.unpack (T.intercalate "." (unTypeConName (qualObject tpiInterface))))
Right DefInterface { intFixedChoices } ->
tpi { tpiInheritedChoiceNames = S.fromList (NM.names intFixedChoices) }
Right DefInterface { intChoices } ->
tpi { tpiInheritedChoiceNames = S.fromList (NM.names intChoices) }
8 changes: 4 additions & 4 deletions compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/Check.hs
Original file line number Diff line number Diff line change
@@ -875,9 +875,9 @@ checkIface m iface = do
forM_ (intMethods iface) checkIfaceMethod

-- check choices
checkUnique (EDuplicateInterfaceChoiceName (intName iface)) $ NM.names (intFixedChoices iface)
checkUnique (EDuplicateInterfaceChoiceName (intName iface)) $ NM.names (intChoices iface)
introExprVar (intParam iface) (TCon tcon) $ do
forM_ (intFixedChoices iface) (checkTemplateChoice tcon)
forM_ (intChoices iface) (checkTemplateChoice tcon)
checkExpr (intPrecondition iface) TBool

checkIfaceMethod :: MonadGamma m => InterfaceMethod -> m ()
@@ -943,15 +943,15 @@ checkTemplate m t@(Template _loc tpl param precond signatories observers text ch

checkIfaceImplementation :: MonadGamma m => Template -> TemplateImplements -> m ()
checkIfaceImplementation Template{tplTypeCon, tplImplements} TemplateImplements{..} = do
DefInterface {intFixedChoices, intRequires, intMethods} <- inWorld $ lookupInterface tpiInterface
DefInterface {intChoices, intRequires, intMethods} <- inWorld $ lookupInterface tpiInterface

-- check requires
let missingRequires = S.difference intRequires (S.fromList (NM.names tplImplements))
whenJust (listToMaybe (S.toList missingRequires)) $ \missingInterface ->
throwWithContext (EMissingRequiredInterface tplTypeCon tpiInterface missingInterface)

-- check fixed choices
let inheritedChoices = S.fromList (NM.names intFixedChoices)
let inheritedChoices = S.fromList (NM.names intChoices)
unless (inheritedChoices == tpiInheritedChoiceNames) $
throwWithContext $ EBadInheritedChoices tpiInterface (S.toList inheritedChoices) (S.toList tpiInheritedChoiceNames)

Original file line number Diff line number Diff line change
@@ -212,7 +212,7 @@ checkSynonym moduleName DefTypeSyn{..} =

checkInterface :: ModuleName -> DefInterface -> NCMonad ()
checkInterface moduleName DefInterface{..} = do
forM_ intFixedChoices $ \TemplateChoice{..} ->
forM_ intChoices $ \TemplateChoice{..} ->
checkName (NInterfaceChoice moduleName intName chcName)
forM_ intMethods $ \InterfaceMethod{..} ->
checkName (NInterfaceMethod moduleName intName ifmName)
Original file line number Diff line number Diff line change
@@ -189,7 +189,7 @@ checkInterface :: MonadGamma m => Module -> DefInterface -> m ()
checkInterface _mod0 iface = do
-- TODO https://github.com/digital-asset/daml/issues/12051
-- Add per interface choice context.
for_ (intFixedChoices iface) $ \ch -> do
for_ (intChoices iface) $ \ch -> do
checkType SRChoiceArg (snd (chcArgBinder ch))
checkType SRChoiceRes (chcReturnType ch)

Original file line number Diff line number Diff line change
@@ -465,7 +465,7 @@ convertInterfaces env binds = interfaceDefs
intRequires <- fmap S.fromList $ mapM (convertInterfaceTyCon env) $
MS.findWithDefault [] intName (envRequires env)
intMethods <- NM.fromList <$> convertMethods tyCon
intFixedChoices <- convertChoices env intName emptyTemplateBinds
intChoices <- convertChoices env intName emptyTemplateBinds
intPrecondition <- useSingleMethodDict env precond (`ETmApp` EVar intParam)
pure DefInterface {..}

Original file line number Diff line number Diff line change
@@ -1616,7 +1616,7 @@ message DefInterface {
// Binder for interface value ("this") in precond and fixed choices.
int32 param_interned_str = 5;
Expr precond = 6;
repeated TemplateChoice fixed_choices = 7;
repeated TemplateChoice choices = 7;
}

// Exception definition
Original file line number Diff line number Diff line change
@@ -667,7 +667,7 @@ private[archive] class DecodeV1(minor: LV.Minor) {
DefInterface.build(
requires = lfInterface.getRequiresList.asScala.view.map(decodeTypeConName),
param = getInternedName(lfInterface.getParamInternedStr, "DefInterface.param"),
fixedChoices = lfInterface.getFixedChoicesList.asScala.view.map(decodeChoice(id, _)),
choices = lfInterface.getChoicesList.asScala.view.map(decodeChoice(id, _)),
methods = lfInterface.getMethodsList.asScala.view.map(decodeInterfaceMethod),
precond = decodeExpr(lfInterface.getPrecond, s"$id:ensure"),
)
Original file line number Diff line number Diff line change
@@ -798,7 +798,7 @@ private[daml] class EncodeV1(minor: LV.Minor) {
val builder = PLF.DefInterface.newBuilder()
builder.setTyconInternedDname(dottedNameTable.insert(dottedName))
builder.setParamInternedStr(stringsTable.insert(interface.param))
builder.accumulateLeft(interface.fixedChoices.sortByKey)(_ addFixedChoices _)
builder.accumulateLeft(interface.choices.sortByKey)(_ addChoices _)
builder.accumulateLeft(interface.methods.sortByKey)(_ addMethods _)
builder.accumulateLeft(interface.requires)(_ addRequires _)
builder.setPrecond(interface.precond)
Original file line number Diff line number Diff line change
@@ -233,7 +233,7 @@ object InterfaceReader {
name: QualifiedName,
astIf: Ast.DefInterface,
): InterfaceReaderError \/ (QualifiedName, DefInterface.FWT) = for {
choices <- astIf.fixedChoices.traverse(visitChoice(name, _))
choices <- astIf.choices.traverse(visitChoice(name, _))
} yield name -> iface.DefInterface(choices)

private[lf] def toIfaceType(
Original file line number Diff line number Diff line change
@@ -342,7 +342,7 @@ private[lf] final class Compiler(
val identifier = Identifier(pkgId, QualifiedName(module.name, ifaceName))
addDef(compileFetchInterface(identifier))
addDef(compileInterfacePrecond(identifier, iface.param, iface.precond))
iface.fixedChoices.values.foreach { choice =>
iface.choices.values.foreach { choice =>
addDef(compileInterfaceChoice(identifier, iface.param, choice))
}
}
Original file line number Diff line number Diff line change
@@ -699,7 +699,7 @@ object Ast {
final case class GenDefInterface[E](
requires: Set[TypeConName],
param: ExprVarName, // Binder for template argument.
fixedChoices: Map[ChoiceName, GenTemplateChoice[E]],
choices: Map[ChoiceName, GenTemplateChoice[E]],
methods: Map[MethodName, InterfaceMethod],
precond: E, // Interface creation precondition.
)
@@ -709,33 +709,33 @@ object Ast {
def build(
requires: Iterable[TypeConName],
param: ExprVarName, // Binder for template argument.
fixedChoices: Iterable[GenTemplateChoice[E]],
choices: Iterable[GenTemplateChoice[E]],
methods: Iterable[InterfaceMethod],
precond: E,
): GenDefInterface[E] = {
val requiresSet = toSetWithoutDuplicate(
requires,
(name: TypeConName) => PackageError(s"repeated required interface $name"),
)
val fixedChoiceMap = toMapWithoutDuplicate(
fixedChoices.view.map(c => c.name -> c),
val choiceMap = toMapWithoutDuplicate(
choices.view.map(c => c.name -> c),
(name: ChoiceName) => PackageError(s"collision on interface choice name $name"),
)
val methodMap = toMapWithoutDuplicate(
methods.view.map(c => c.name -> c),
(name: MethodName) => PackageError(s"collision on interface method name $name"),
)
GenDefInterface(requiresSet, param, fixedChoiceMap, methodMap, precond)
GenDefInterface(requiresSet, param, choiceMap, methodMap, precond)
}

def apply(
requires: Set[TypeConName],
param: ExprVarName,
fixedChoices: Map[ChoiceName, GenTemplateChoice[E]],
choices: Map[ChoiceName, GenTemplateChoice[E]],
methods: Map[MethodName, InterfaceMethod],
precond: E,
): GenDefInterface[E] =
GenDefInterface(requires, param, fixedChoices, methods, precond)
GenDefInterface(requires, param, choices, methods, precond)

def unapply(arg: GenDefInterface[E]): Some[
(
@@ -746,7 +746,7 @@ object Ast {
E,
)
] =
Some((arg.requires, arg.param, arg.fixedChoices, arg.methods, arg.precond))
Some((arg.requires, arg.param, arg.choices, arg.methods, arg.precond))
}

type DefInterface = GenDefInterface[Expr]
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ private[lf] class PackageInterface(signatures: PartialFunction[PackageId, Packag
context: => Reference,
): Either[LookupError, TemplateChoiceSignature] =
lookupInterface(ifaceName, context).flatMap(
_.fixedChoices
_.choices
.get(chName)
.toRight(LookupError(Reference.TemplateChoice(ifaceName, chName), context))
)
Original file line number Diff line number Diff line change
@@ -247,11 +247,11 @@ object Util {

private def toSignature(interface: DefInterface): DefInterfaceSignature =
interface match {
case DefInterface(requires, param, fixedChoices, methods, _) =>
case DefInterface(requires, param, choices, methods, _) =>
DefInterfaceSignature(
requires,
param,
fixedChoices.transform((_, choice) => toSignature(choice)),
choices.transform((_, choice) => toSignature(choice)),
methods,
(),
)
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ class AstSpec extends AnyWordSpec with TableDrivenPropertyChecks with Matchers {
def interface = DefInterface(
param = Name.assertFromString("x"),
precond = ETrue,
fixedChoices = Map.empty,
choices = Map.empty,
methods = Map.empty,
requires = Set.empty,
)
@@ -374,7 +374,7 @@ class AstSpec extends AnyWordSpec with TableDrivenPropertyChecks with Matchers {
DefInterface.build(
requires = List.empty,
param = Name.assertFromString("x"),
fixedChoices = List(
choices = List(
choiceBuilder(choice1, TUnit, EUnit),
choiceBuilder(choice2, TBool, ETrue),
choiceBuilder(choice3, TText, eText),
@@ -389,7 +389,7 @@ class AstSpec extends AnyWordSpec with TableDrivenPropertyChecks with Matchers {
DefInterface.build(
requires = List.empty,
param = Name.assertFromString("x"),
fixedChoices = List(
choices = List(
choiceBuilder(choice1, TUnit, EUnit),
choiceBuilder(choice2, TBool, ETrue),
choiceBuilder(choice1, TText, eText),
@@ -405,7 +405,7 @@ class AstSpec extends AnyWordSpec with TableDrivenPropertyChecks with Matchers {
DefInterface.build(
requires = List.empty,
param = Name.assertFromString("x"),
fixedChoices = List.empty,
choices = List.empty,
methods = List(ifaceMethod1, ifaceMethod1),
precond = ETrue,
)
Original file line number Diff line number Diff line change
@@ -346,11 +346,11 @@ private[daml] class AstRewriter(

def apply(x: DefInterface): DefInterface =
x match {
case DefInterface(requires, param, fixedChoices, methods, precond) =>
case DefInterface(requires, param, choices, methods, precond) =>
DefInterface(
requires.map(apply(_)),
param,
fixedChoices.transform((_, v) => apply(v)),
choices.transform((_, v) => apply(v)),
methods.transform((_, v) => apply(v)),
apply(precond),
)
Original file line number Diff line number Diff line change
@@ -813,7 +813,7 @@ class ParsersSpec extends AnyWordSpec with ScalaCheckPropertyChecks with Matcher
n"asParty" -> InterfaceMethod(n"asParty", t"Party"),
n"getName" -> InterfaceMethod(n"getName", t"Text"),
),
fixedChoices = Map(
choices = Map(
n"Sleep" -> TemplateChoice(
name = n"Sleep",
consuming = true,
Original file line number Diff line number Diff line change
@@ -97,6 +97,6 @@ private[validation] object Collision {
ifaceName: DottedName,
iface: Ast.DefInterface,
): List[NamedEntity] =
(iface.fixedChoices.keys.map(NInterfaceChoice(module, ifaceName, _)) ++
(iface.choices.keys.map(NInterfaceChoice(module, ifaceName, _)) ++
iface.methods.keys.map(NInterfaceMethod(module, ifaceName, _))).toList
}
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ private[validation] object Serializability {
defInterface: DefInterface,
): Unit = {
val context = Context.DefInterface(tyCon.tycon)
defInterface.fixedChoices.values.foreach { choice =>
defInterface.choices.values.foreach { choice =>
Env(version, interface, context, SRChoiceArg, choice.argBinder._2).checkType()
Env(version, interface, context, SRChoiceRes, choice.returnType).checkType()
}
Original file line number Diff line number Diff line change
@@ -458,7 +458,7 @@ private[validation] object Typing {

private[Typing] def checkDefIface(ifaceName: TypeConName, iface: DefInterface): Unit =
iface match {
case DefInterface(requires, param, fixedChoices, methods, precond) =>
case DefInterface(requires, param, choices, methods, precond) =>
val env = introExprVar(param, TTyCon(ifaceName))
if (requires(ifaceName))
throw ECircularInterfaceRequires(ctx, ifaceName)
@@ -469,7 +469,7 @@ private[validation] object Typing {
} throw ENotClosedInterfaceRequires(ctx, ifaceName, required, requiredRequired)
env.checkExpr(precond, TBool)
methods.values.foreach(checkIfaceMethod)
fixedChoices.values.foreach(env.checkChoice(ifaceName, _))
choices.values.foreach(env.checkChoice(ifaceName, _))
}

private def checkIfaceMethod(method: InterfaceMethod): Unit = {
@@ -486,20 +486,20 @@ private[validation] object Typing {
): Unit = {

impls.foreach { case (iface, impl) =>
val DefInterfaceSignature(requires, _, fixedChoices, methods, _) =
val DefInterfaceSignature(requires, _, choices, methods, _) =
handleLookup(ctx, interface.lookupInterface(impl.interfaceId))

requires
.filterNot(impls.contains)
.foreach(required => throw EMissingRequiredInterface(ctx, tplTcon, iface, required))

val fixedChoiceSet = fixedChoices.keySet
if (impl.inheritedChoices != fixedChoiceSet) {
val choicesSet = choices.keySet
if (impl.inheritedChoices != choicesSet) {
throw EBadInheritedChoices(
ctx,
impl.interfaceId,
tplTcon,
fixedChoiceSet,
choicesSet,
impl.inheritedChoices,
)
}
Original file line number Diff line number Diff line change
@@ -218,11 +218,11 @@ private[validation] object ExprIterable {
case DefInterface(
requires @ _,
param @ _,
fixedChoices,
choices,
methods @ _,
precond,
) =>
Iterator(precond) ++ fixedChoices.values.iterator.flatMap(iterator(_))
Iterator(precond) ++ choices.values.iterator.flatMap(iterator(_))
}

def apply(expr: Expr): Iterable[Expr] =
Original file line number Diff line number Diff line change
@@ -257,10 +257,10 @@ private[validation] object TypeIterable {

private[validation] def iterator(interface: DefInterface): Iterator[Type] =
interface match {
case DefInterface(requires, _, fixedChoice, methods, precond) =>
case DefInterface(requires, _, choices, methods, precond) =>
requires.iterator.map(TTyCon) ++
iterator(precond) ++
fixedChoice.values.iterator.flatMap(iterator) ++
choices.values.iterator.flatMap(iterator) ++
methods.values.iterator.flatMap(iterator)
}

Original file line number Diff line number Diff line change
@@ -104,8 +104,7 @@ object ScriptF {
.left
.map(_.pretty)
.flatMap(mod =>
(mod.templates.view.mapValues(_.choices) ++
mod.interfaces.view.mapValues(_.fixedChoices))
(mod.templates.view.mapValues(_.choices) ++ mod.interfaces.view.mapValues(_.choices))
.collectFirst {
case (typeName, choices)
if choices.get(choiceName).exists(_.argBinder._2 == argTyp) =>
Loading

0 comments on commit ea0d101

Please sign in to comment.