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

NLG specifically for Royalflush usecase #154

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
131 changes: 102 additions & 29 deletions lam4-backend/gf-grammar/Lam4.gf
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ abstract Lam4 = {
Name ;
[Name]{0} ;

-- Special values for aggregation
ListOp ;
LExpr ;
[LExpr]{2} ;

-- (condition, value)
-- in order to flatten nested if-then-elses into if-elif-elif-…-else
IfThen ;
[IfThen]{2} ;
fun
-- Placeholder, or skip some constructs?
EmptyS : S ;
TypeDeclS : TypeDecl -> S ;
TypeDeclS : (id : String) -> TypeDecl -> S ;
EvalS,
EvalWhetherS,
ExprS : Expr -> S ;
AssignS : Name -> Expr -> S ;
ExprS : (id : String) -> Expr -> S ;
AssignS : (id : String) -> Name -> Expr -> S ;
LetIsTrue : (id : String) -> Name -> Expr -> S ;
AtomicConcept : (id : String) -> Name -> S ;

-- Metadata
MkMetadata : String -> Metadata ;
Expand All @@ -38,27 +49,109 @@ abstract Lam4 = {
Lit,
QuoteVar,
Var : Name -> Expr ;
-- | Cons Expr Expr -- list cons
-- | List [Expr] -- construct a list
-- | ListExpr ListOp [Expr]

-- the following correspond to List and ListExpr in Lam4 AST,
-- named differently because of a bug in GF,
-- see https://github.com/GrammaticalFramework/gf-core/issues/163
ConjExpr : [Expr] -> Expr ; -- construct a list

coerceListExpr : Expr -> LExpr ;
ApplyListOp : ListOp -> [LExpr] -> Expr ;

ListAnd, ListOr : ListOp ;

Unary : UnaryOp -> Expr -> Expr ;
VerboseBinExpr, -- newline + quotes around args
BinExpr : BinOp -> Expr -> Expr -> Expr ;

UnaryMinusExpr : Expr -> Expr ;
Round : (expr, prec : Expr) -> Expr ;
Default : (val, default : Expr) -> Expr ;

IfThenElse : Expr -> Expr -> Expr -> Expr ;

-- Flatten nested If Then Elses
FirstIfThen : Expr -> Expr -> IfThen ;
MiddleIfThen : Expr -> Expr -> IfThen ;
NilIfThen : Expr -> IfThen ; -- the original IfThenElse
Elif: [IfThen] -> Expr ;
{-

IF i's `the business's number of past and current clients` is larger than
* 10.0: => the business's client bonus factor is 1.5
* 4.0 => the business's client bonus factor is 1.35
* 1.0 => the business's client bonus factor is 1.2
* ELSE => the business's client bonus factor is 1.0

-}

-- For linearizing functions that have a NLG annotation
FunApp1 : (description : String) -> (arg : Expr) -> Expr ;
FunApp2 : (desc1 : String) -> (arg1 : Expr) -> (desc2 : String) -> (arg2 : Expr) -> Expr ;

-- For linearizing functions that don't have NLG annotation
FunApp : Expr -> [Expr] -> Expr ;
-- Record : (Row Expr) -> Expr ; -- record construction
OnlyFieldProject,
Project : Expr -> Name -> Expr ; -- record projection
Fun : (funname : Name) -> Metadata -> (args : [Name]) -> Expr -> Expr ; -- Function
-- Let : Decl Expr
Sig : [Name] -> [Expr] -> Expr ; -- TODO: what is this? only `Sig [] []` present in royalflush data

{- TODO: this is used in context like

LET { foo
, bar
, baz
}
IN { expression that uses definitions
, another expression
, yet another
… }

as AST it looks like
Let (foo
Let (bar
Let baz )))

transform into:

[all variables]

[all Exprs]

and linearize as
-- Definitions ---
foo = …
bar = …
baz = …

-- Expressions --
if they are just like
`the business eligibility text` = `the business eligibility text`,
then don't print them.
-}
Let : S -> Expr -> Expr ;
Record : Name -> Expr -> Expr ;
-- StatementBlock : (NonEmpty Statement)

NormIsInfringed : Name -> Expr ; -- NormIsInfringed NameOfNorm.
-- This is a predicate that checks if @nameOfNorm@ is violated (users can supply unique identifiers for Deontics and IfThenOtherwise statements that contain a Deontic)


Predicate : (predname : Name) -> Metadata -> (args : [Name]) -> Expr -> Expr ; -- Differs from a function when doing symbolic evaluation. Exact way in which they should differ is WIP.
PredApp : Expr -> [Expr] -> Expr ;

PredApp : (pred : Expr) -> (args : [Expr]) -> Expr ;

-- Aggregation of multiple PredApps being applied to the same argument(s).
PredAppMany : BinOp -> (preds : [Expr]) -> (args : [Expr]) -> Expr ;
Fold : Expr -> Expr -> Expr -> Expr ;

-- When generating natural language for some file that defines a bunch of stuff like cons, map, filter,
-- apply this function instead to keep it in the AST
-- but skip linearization.
KnownFunction : Name -> Expr ;


-- Unary and binary operators
Not,
Floor,
Expand All @@ -79,24 +172,4 @@ abstract Lam4 = {
Ge : BinOp ; -- ^ greater-than-or-equal
Eq : BinOp ; -- ^ equality (of Booleans, numbers or atoms)
Ne : BinOp ; -- ^ inequality (of Booleans, numbers or atoms)
}

{-
— <some kind of description>
LOTTERY {
— how much can be won from the jackpot
`total jackpot`: Integer,
— whether buying tickets from this lottery is tax deductible
`tax deductible status`: Boolean
}


This can be rendered as:

MkTypeDecl (MkMetadata "a game where you lose money") "Lottery" (ConsRowTypeDecl (MkRowDecl (MkMetadata "how much can be won from the jackpot") "‘total jackpot’") (ConsRowTypeDecl (MkRowDecl (MkMetadata "whether buying tickets from this lottery is tax deductible") "‘tax deductible status’") BaseRowTypeDecl))

A/An ‘Lottery’ is <insert description>.
Each lottery has associated with it information like
* its ‘total jackpot’; i.e., how much can be won from the jackpot
* its ‘tax deductible status’; i.e., whether buying tickets from this lottery is tax deductible
-}
}
Binary file modified lam4-backend/gf-grammar/Lam4.pgf
Binary file not shown.
Loading