Skip to content

Commit afcd1bc

Browse files
Marty Stumpfmichaelpj
authored andcommitted
Address a comment in PR 5160. (IntersectMBO#5211)
* Address a comment in PR 5160. Co-authored-by: Michael Peyton Jones <[email protected]>
1 parent 3bee536 commit afcd1bc

33 files changed

+27
-16
lines changed

plutus-core/plutus-ir/src/PlutusIR/Transform/Inline/CallSiteInline.hs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,34 @@ Also, we currently reject `Constant` (has acceptable cost but not acceptable siz
142142
We may want to check their sizes instead of just rejecting them.
143143
-}
144144

145-
-- | A list of `LamAbs` and `TyAbs`, in order, of a let-binding.
146-
type TermOrTypeOrder = [TermOrType]
145+
{-|
146+
The (syntactic) arity of a term. That is, a record of the arguments that the
147+
term expects before it may do some work. Since we have both type and lambda
148+
abstractions, this is not a simple argument count, but rather a list of values
149+
indicating whether the next argument should be a term or a type.
150+
151+
Note that this is the syntactic arity, i.e. it just corresponds to the number of
152+
syntactic lambda and type abstractions on the outside of the term. It is thus
153+
an under-approximation of how many arguments the term may need.
154+
e.g. consider the term @let id = \x -> x in id@: the variable @id@ has syntactic
155+
arity @[]@, but does in fact need an argument before it does any work.
156+
-}
157+
type Arity = [TermOrType]
147158

148-
-- | Datatype capturing both terms and types.
159+
-- | Is the next argument a term or a type?
149160
data TermOrType =
150161
MkTerm | MkType
151162
deriving stock (Eq, Show)
152163

153164
instance Pretty TermOrType where
154165
pretty = viaShow
155166

156-
-- | Counts the type and term lambdas in the RHS of a binding and returns an ordered list
157-
countLam ::
158-
Term tyname name uni fun a -- ^ the RHS of the let binding
159-
-> TermOrTypeOrder
160-
countLam = \case
161-
LamAbs _ _ _ body -> MkTerm : countLam body
162-
TyAbs _ _ _ body -> MkType : countLam body
163-
-- Whenever we encounter a body that is not a lambda abstraction, we are done counting
167+
-- | Computes the 'Arity' of a term.
168+
computeArity ::
169+
Term tyname name uni fun a
170+
-> Arity
171+
computeArity = \case
172+
LamAbs _ _ _ body -> MkTerm : computeArity body
173+
TyAbs _ _ _ body -> MkType : computeArity body
174+
-- Whenever we encounter a body that is not a lambda or type abstraction, we are done counting
164175
_ -> []

plutus-core/plutus-ir/test/TransformSpec.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import PlutusIR.Parser
1919
import PlutusIR.Test
2020
import PlutusIR.Transform.Beta qualified as Beta
2121
import PlutusIR.Transform.DeadCode qualified as DeadCode
22-
import PlutusIR.Transform.Inline.CallSiteInline (countLam)
22+
import PlutusIR.Transform.Inline.CallSiteInline (computeArity)
2323
import PlutusIR.Transform.Inline.UnconditionalInline qualified as UInline
2424
import PlutusIR.Transform.LetFloatIn qualified as LetFloatIn
2525
import PlutusIR.Transform.LetFloatOut qualified as LetFloatOut
@@ -42,7 +42,7 @@ transform =
4242
, letFloatInRelaxed
4343
, recSplit
4444
, inline
45-
, countLamTest
45+
, computeArityTest
4646
, beta
4747
, unwrapCancel
4848
, deadCode
@@ -211,10 +211,10 @@ inline =
211211
, "letOverAppMulti" -- multiple occurrences of an over-application of a function
212212
]
213213

214-
countLamTest :: TestNested
215-
countLamTest = testNested "countLamTest" $
214+
computeArityTest :: TestNested
215+
computeArityTest = testNested "computeArityTest" $
216216
map
217-
(goldenPir (countLam . runQuote . PLC.rename) pTerm)
217+
(goldenPir (computeArity . runQuote . PLC.rename) pTerm)
218218
[ "var" -- from inline tests, testing let terms
219219
, "tyvar"
220220
, "single"

0 commit comments

Comments
 (0)