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

Add EViewInterface to LF - stub compilation/interpretation in speedy #14486

Merged
merged 5 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Alpha.hs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ alphaExpr' env = \case
ELocation _ e1 -> \case
ELocation _ e2 -> alphaExpr' env e1 e2
_ -> False
EViewInterface iface1 expr1 -> \case
EViewInterface iface2 expr2
-> alphaTypeCon iface1 iface2
&& alphaExpr' env expr1 expr2
_ -> False
EExperimental n1 t1 -> \case
EExperimental n2 t2 -> n1 == n2 && alphaType t1 t2
_ -> False
Expand Down
5 changes: 5 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ data Expr
| EScenario !Scenario
-- | An expression annotated with a source location.
| ELocation !SourceLoc !Expr
-- | Obtain an interface view
| EViewInterface
{ viewInterfaceInterface :: !(Qualified TypeConName)
, viewInterfaceExpr :: !Expr
}
-- | Experimental Expression Hook
| EExperimental !T.Text !Type
deriving (Eq, Data, Generic, NFData, Ord, Show)
Expand Down
1 change: 1 addition & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/FreeVars.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ freeVarsStep = \case
EInterfaceTemplateTypeRepF _ e -> e
ESignatoryInterfaceF _ e -> e
EObserverInterfaceF _ e -> e
EViewInterfaceF _ e -> e
EExperimentalF _ t -> freeVarsInType t

where
Expand Down
2 changes: 2 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ instance Pretty Expr where
[interfaceArg ty, TmArg expr]
EObserverInterface ty expr -> pPrintAppKeyword lvl prec "observer_interface"
[interfaceArg ty, TmArg expr]
EViewInterface iface expr -> pPrintAppKeyword lvl prec "view"
[interfaceArg iface, TmArg expr]
EExperimental name _ -> pPrint $ "$" <> name

instance Pretty DefTypeSyn where
Expand Down
3 changes: 3 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Recursive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ data ExprF expr
| EInterfaceTemplateTypeRepF !(Qualified TypeConName) !expr
| ESignatoryInterfaceF !(Qualified TypeConName) !expr
| EObserverInterfaceF !(Qualified TypeConName) !expr
| EViewInterfaceF !(Qualified TypeConName) !expr
| EExperimentalF !T.Text !Type
deriving (Foldable, Functor, Traversable)

Expand Down Expand Up @@ -221,6 +222,7 @@ instance Recursive Expr where
EInterfaceTemplateTypeRep a b -> EInterfaceTemplateTypeRepF a b
ESignatoryInterface a b -> ESignatoryInterfaceF a b
EObserverInterface a b -> EObserverInterfaceF a b
EViewInterface a b -> EViewInterfaceF a b
EExperimental a b -> EExperimentalF a b

instance Corecursive Expr where
Expand Down Expand Up @@ -265,4 +267,5 @@ instance Corecursive Expr where
EInterfaceTemplateTypeRepF a b -> EInterfaceTemplateTypeRep a b
ESignatoryInterfaceF a b -> ESignatoryInterface a b
EObserverInterfaceF a b -> EObserverInterface a b
EViewInterfaceF a b -> EViewInterface a b
EExperimentalF a b -> EExperimental a b
3 changes: 3 additions & 0 deletions compiler/daml-lf-ast/src/DA/Daml/LF/Ast/Subst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ applySubstInExpr subst@Subst{..} = \case
ELocation l e -> ELocation
l
(applySubstInExpr subst e)
EViewInterface iface expr -> EViewInterface
iface
(applySubstInExpr subst expr)
EExperimental name ty ->
EExperimental name (applySubstInType subst ty)

Expand Down
3 changes: 3 additions & 0 deletions compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/DecodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,9 @@ decodeExprSum exprSum = mayDecode "exprSum" exprSum $ \case
LF1.ExprSumObserverInterface LF1.Expr_ObserverInterface {..} -> EObserverInterface
<$> mayDecode "expr_ObserverInterfaceInterface" expr_ObserverInterfaceInterface decodeTypeConName
<*> mayDecode "expr_ObserverInterfaceExpr" expr_ObserverInterfaceExpr decodeExpr
LF1.ExprSumViewInterface LF1.Expr_ViewInterface {..} -> EViewInterface
<$> mayDecode "expr_ViewInterfaceInterface" expr_ViewInterfaceInterface decodeTypeConName
<*> mayDecode "expr_ViewInterfaceExpr" expr_ViewInterfaceExpr decodeExpr
LF1.ExprSumExperimental (LF1.Expr_Experimental name mbType) -> do
ty <- mayDecode "expr_Experimental" mbType decodeType
pure $ EExperimental (decodeString name) ty
Expand Down
4 changes: 4 additions & 0 deletions compiler/daml-lf-proto/src/DA/Daml/LF/Proto3/EncodeV1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ encodeExpr' = \case
expr_ObserverInterfaceInterface <- encodeQualTypeConName ty
expr_ObserverInterfaceExpr <- encodeExpr val
pureExpr $ P.ExprSumObserverInterface P.Expr_ObserverInterface{..}
EViewInterface iface expr -> do
expr_ViewInterfaceInterface <- encodeQualTypeConName iface
expr_ViewInterfaceExpr <- encodeExpr expr
pureExpr $ P.ExprSumViewInterface P.Expr_ViewInterface{..}
EExperimental name ty -> do
let expr_ExperimentalName = encodeString name
expr_ExperimentalType <- encodeType ty
Expand Down
1 change: 1 addition & 0 deletions compiler/daml-lf-tools/src/DA/Daml/LF/Simplifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ safetyStep = \case
EInterfaceTemplateTypeRepF _ s -> s <> Safe 0
ESignatoryInterfaceF _ s -> s <> Safe 0
EObserverInterfaceF _ s -> s <> Safe 0
EViewInterfaceF _ _ -> Unsafe
EExperimentalF _ _ -> Unsafe

isTypeClassDictionary :: DefValue -> Bool
Expand Down
4 changes: 4 additions & 0 deletions compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@ typeOf' = \case
EUpdate upd -> typeOfUpdate upd
EScenario scen -> typeOfScenario scen
ELocation _ expr -> typeOf' expr
EViewInterface ifaceId expr -> do
iface <- inWorld (lookupInterface ifaceId)
checkExpr expr (TCon ifaceId)
pure (intView iface)
EExperimental name ty -> do
checkFeature featureExperimental
checkExperimentalType name ty
Expand Down
2 changes: 2 additions & 0 deletions compiler/damlc/daml-lf-conversion/src/DA/Daml/LFConversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,8 @@ internalFunctions = listToUFM $ map (bimap mkModuleNameFS mkUniqSet)
])
, ("DA.Internal.Desugar",
[ "mkMethod"
, "mkInterfaceView"
, "view"
])
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ message Expr {
Expr interface_expr = 3;
}

// Obtain an interface view
// *Available in versions >= 1.dev*
message ViewInterface {
TypeConName interface = 1;
Expr expr = 2;
}

// Obtain the type representation of a contract through an interface
// *Available in versions >= 1.dev*
message InterfaceTemplateTypeRep {
Expand Down Expand Up @@ -1137,6 +1144,10 @@ message Expr {
// Unsafe downcast interface payloads.
UnsafeFromRequiredInterface unsafe_from_required_interface = 45;

// Invoke an interface method.
// *Available in versions >= 1.dev*
ViewInterface view_interface = 46;

Experimental experimental = 9999; // *Available only in 1.dev*
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,14 @@ private[archive] class DecodeV1(minor: LV.Minor) {
case PLF.Expr.SumCase.SUM_NOT_SET =>
throw Error.Parsing("Expr.SUM_NOT_SET")

case PLF.Expr.SumCase.VIEW_INTERFACE =>
assertSince(LV.Features.interfaces, "Expr.view_interface")
val viewInterface = lfExpr.getViewInterface
EViewInterface(
ifaceId = decodeTypeConName(viewInterface.getInterface),
expr = decodeExpr(viewInterface.getExpr, definition),
)

case PLF.Expr.SumCase.EXPERIMENTAL =>
assertSince(LV.v1_dev, "Expr.experimental")
val experimental = lfExpr.getExperimental
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ private[lf] final class PhaseOne(
compileExp(env, exp) { exp =>
Return(SBObserverInterface(ifaceId)(exp))
}
case EViewInterface(ifaceId, exp) =>
compileExp(env, exp) { exp =>
Return(SBViewInterface(ifaceId)(exp))
}
case EExperimental(name, _) =>
Return(SBExperimental(name))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,16 @@ private[lf] object SBuiltin {
}
}

final case class SBViewInterface(
ifaceId: TypeConName
) extends SBuiltin(1) {
override private[speedy] def execute(args: util.ArrayList[SValue], machine: Machine): Unit = {
crash(
s"Tried to run unsupported view with interface ${ifaceId}."
)
}
}

/** $insertFetch[tid]
* :: ContractId a
* -> List Party (signatories)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ object Ast {
body: Expr,
) extends Expr

/** Obtain the view of an interface. */
final case class EViewInterface(
ifaceId: TypeConName,
expr: Expr,
) extends Expr

//
// Kinds
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ private[daml] class AstRewriter(
EInterfaceTemplateTypeRep(apply(ifaceId), apply(body))
case ESignatoryInterface(ifaceId, body) =>
ESignatoryInterface(apply(ifaceId), apply(body))
case EViewInterface(ifaceId, expr) =>
EViewInterface(apply(ifaceId), apply(expr))
case EObserverInterface(ifaceId, body) =>
EObserverInterface(apply(ifaceId), apply(body))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,10 @@ private[validation] object Typing {
discard(handleLookup(ctx, pkgInterface.lookupInterface(ifaceId)))
checkExpr(body, TTyCon(ifaceId))
TList(TParty)
case EViewInterface(ifaceId, expr) =>
val iface = handleLookup(ctx, pkgInterface.lookupInterface(ifaceId))
checkExpr(expr, TTyCon(ifaceId))
iface.view
case EExperimental(_, typ) =>
typ
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private[validation] object ExprIterable {
iterator(body)
case ESignatoryInterface(iface @ _, body) =>
iterator(body)
case EViewInterface(ifaceId @ _, expr) =>
iterator(expr)
case EObserverInterface(iface @ _, body) =>
iterator(body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ private[validation] object TypeIterable {
Iterator(TTyCon(ifaceId)) ++ iterator(body)
case EObserverInterface(ifaceId, body) =>
Iterator(TTyCon(ifaceId)) ++ iterator(body)
case EViewInterface(ifaceId, expr) =>
Iterator(TTyCon(ifaceId)) ++
iterator(expr)
case EVar(_) | EVal(_) | EBuiltin(_) | EPrimCon(_) | EPrimLit(_) | EApp(_, _) | ECase(_, _) |
ELocation(_, _) | EStructCon(_) | EStructProj(_, _) | EStructUpd(_, _, _) | ETyAbs(_, _) |
EExperimental(_, _) =>
Expand Down