Skip to content

Commit 85b72f1

Browse files
committed
SCP-3392 UPLC simplifier
Don't look here yet, just benchmarking something.
1 parent a28a62d commit 85b72f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+917
-302
lines changed

plutus-benchmark/nofib/test/Spec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ testClausify = testGroup "clausify"
4646
, testCase "formula3" $ mkClausifyTest Clausify.F3
4747
, testCase "formula4" $ mkClausifyTest Clausify.F4
4848
, testCase "formula5" $ mkClausifyTest Clausify.F5
49-
, Tx.fitsInto "formula1 (size)" (Clausify.mkClausifyCode Clausify.F1) 5190
49+
, Tx.fitsInto "formula1 (size)" (Clausify.mkClausifyCode Clausify.F1) 5824
5050
, runTestNested $ Tx.goldenBudget "formulaBudget" $ Clausify.mkClausifyCode Clausify.F1
5151
]
5252

@@ -64,7 +64,7 @@ testKnights = testGroup "knights" -- Odd sizes call "error" because there are n
6464
, testCase "depth 100, 4x4" $ mkKnightsTest 100 4
6565
, testCase "depth 100, 6x6" $ mkKnightsTest 100 6
6666
, testCase "depth 100, 8x8" $ mkKnightsTest 100 8
67-
, Tx.fitsInto "depth 10, 4x4 (size)" (Knights.mkKnightsCode 10 4) 3669
67+
, Tx.fitsInto "depth 10, 4x4 (size)" (Knights.mkKnightsCode 10 4) 3724
6868
, runTestNested $ Tx.goldenBudget "knightsBudget" $ Knights.mkKnightsCode 10 4
6969
]
7070

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
({ cpu: 52200152258
2-
| mem: 174669548
1+
({ cpu: 48335021398
2+
| mem: 161687548
33
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
({ cpu: 10658709240
2-
| mem: 33475372
1+
({ cpu: 9122720170
2+
| mem: 28316372
33
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
({ cpu: 19730440366
2-
| mem: 62692642
1+
({ cpu: 17086419328
2+
| mem: 53812042
33
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
({ cpu: 263250282408
2-
| mem: 828489180
1+
({ cpu: 228731019613
2+
| mem: 712547680
33
})

plutus-core/plutus-core.cabal

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ library
143143
UntypedPlutusCore.Evaluation.Machine.Cek.Internal
144144
UntypedPlutusCore.Parser
145145
UntypedPlutusCore.Rename
146+
UntypedPlutusCore.MkUPlc
146147
UntypedPlutusCore.Check.Scope
147148
UntypedPlutusCore.Check.Uniques
148149
UntypedPlutusCore.Core.Type
@@ -220,6 +221,7 @@ library
220221
PlutusIR.TypeCheck.Internal
221222

222223
UntypedPlutusCore.Analysis.Definitions
224+
UntypedPlutusCore.Analysis.Usages
223225
UntypedPlutusCore.Core
224226
UntypedPlutusCore.Core.Instance
225227
UntypedPlutusCore.Core.Instance.Eq
@@ -236,9 +238,11 @@ library
236238
UntypedPlutusCore.Evaluation.Machine.Cek.EmitterMode
237239
UntypedPlutusCore.Mark
238240
UntypedPlutusCore.Rename.Internal
241+
UntypedPlutusCore.Simplify
239242
UntypedPlutusCore.Size
240243
UntypedPlutusCore.Subst
241-
UntypedPlutusCore.Transform.Simplify
244+
UntypedPlutusCore.Transform.ForceDelay
245+
UntypedPlutusCore.Transform.Inline
242246

243247
Data.Aeson.Flatten
244248
Data.Aeson.THReader
@@ -452,6 +456,7 @@ test-suite untyped-plutus-core-test
452456
bytestring -any,
453457
hedgehog -any,
454458
flat -any,
459+
lens -any,
455460
mtl -any,
456461
plutus-core -any,
457462
plutus-core-testlib -any,

plutus-core/plutus-ir/src/PlutusIR/Compiler.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module PlutusIR.Compiler (
1212
AsTypeError (..),
1313
AsTypeErrorExt (..),
1414
Provenance (..),
15+
DatatypeComponent (..),
1516
noProvenance,
1617
CompilationOpts,
1718
coOptimize,

plutus-core/plutus-ir/src/PlutusIR/Compiler/Datatype.hs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ mkConstructorType :: Datatype TyName Name uni fun (Provenance a) -> VarDecl TyNa
308308
-- we don't need to do anything to the declared type
309309
-- see note [Abstract data types]
310310
-- FIXME: normalize constructors also here
311-
mkConstructorType (Datatype _ _ tvs _ _) constr = PIR.mkIterTyForall tvs $ _varDeclType constr
311+
mkConstructorType (Datatype _ _ tvs _ _) constr =
312+
let constrTy = PIR.mkIterTyForall tvs $ _varDeclType constr
313+
in fmap (\a -> DatatypeComponent ConstructorType a) constrTy
312314

313315
-- See note [Scott encoding of datatypes]
314316
-- | Make a constructor of a 'Datatype' with the given pattern functor. The constructor argument mostly serves to identify the constructor
@@ -333,34 +335,35 @@ mkConstructor dty d@(Datatype ann _ tvs _ constrs) index = do
333335
pure $ zipWith (VarDecl ann) caseArgNames caseTypes
334336

335337
-- This is inelegant, but it should never fail
336-
let constr = constrs !! index
338+
let thisConstr = constrs !! index
337339
let thisCase = PIR.mkVar ann $ casesAndTypes !! index
338340

339341
-- constructor args and their types
340342
argsAndTypes <- do
341343
-- these types appear *outside* the scope of the abstraction for the datatype, so we need to use the concrete datatype here
342344
-- see note [Abstract data types]
343345
-- FIXME: normalize datacons' types also here
344-
let argTypes = unveilDatatype (getType dty) d <$> constructorArgTypes constr
346+
let argTypes = unveilDatatype (getType dty) d <$> constructorArgTypes thisConstr
345347
-- we don't have any names for these things, we just had the type, so we call them "arg_i
346348
argNames <- for [0..(length argTypes -1)] (\i -> safeFreshName $ "arg_" <> showText i)
347349
pure $ zipWith (VarDecl ann) argNames argTypes
348350

349351

350-
pure $
351-
-- /\t_1 .. t_n
352-
PIR.mkIterTyAbs tvs $
353-
-- \arg_1 .. arg_m
354-
PIR.mkIterLamAbs argsAndTypes $
355-
-- See Note [Recursive datatypes]
356-
-- wrap
357-
wrap ann dty (fmap (PIR.mkTyVar ann) tvs)$
358-
-- forall out
359-
TyAbs ann resultType (Type ann) $
360-
-- \case_1 .. case_j
361-
PIR.mkIterLamAbs casesAndTypes $
362-
-- c_i arg_1 .. arg_m
363-
PIR.mkIterApp ann thisCase (fmap (PIR.mkVar ann) argsAndTypes)
352+
let constr =
353+
-- /\t_1 .. t_n
354+
PIR.mkIterTyAbs tvs $
355+
-- \arg_1 .. arg_m
356+
PIR.mkIterLamAbs argsAndTypes $
357+
-- See Note [Recursive datatypes]
358+
-- wrap
359+
wrap ann dty (fmap (PIR.mkTyVar ann) tvs)$
360+
-- forall out
361+
TyAbs ann resultType (Type ann) $
362+
-- \case_1 .. case_j
363+
PIR.mkIterLamAbs casesAndTypes $
364+
-- c_i arg_1 .. arg_m
365+
PIR.mkIterApp ann thisCase (fmap (PIR.mkVar ann) argsAndTypes)
366+
pure $ fmap (\a -> DatatypeComponent Constructor a) constr
364367

365368
-- Destructors
366369

@@ -379,15 +382,16 @@ mkDestructor dty (Datatype ann _ tvs _ _) = do
379382
let appliedReal = PIR.mkIterTyApp ann (getType dty) (fmap (PIR.mkTyVar ann) tvs)
380383

381384
xn <- safeFreshName "x"
382-
pure $
383-
-- /\t_1 .. t_n
384-
PIR.mkIterTyAbs tvs $
385-
-- \x
386-
LamAbs ann xn appliedReal $
387-
-- See note [Recursive datatypes]
388-
-- unwrap
389-
unwrap ann dty $
390-
Var ann xn
385+
let destr =
386+
-- /\t_1 .. t_n
387+
PIR.mkIterTyAbs tvs $
388+
-- \x
389+
LamAbs ann xn appliedReal $
390+
-- See note [Recursive datatypes]
391+
-- unwrap
392+
unwrap ann dty $
393+
Var ann xn
394+
pure $ fmap (\a -> DatatypeComponent Destructor a) destr
391395

392396
-- See note [Scott encoding of datatypes]
393397
-- | Make the type of a destructor for a 'Datatype'.
@@ -396,8 +400,8 @@ mkDestructor dty (Datatype ann _ tvs _ _) = do
396400
-- = forall (a :: *) . (List a) -> (<pattern functor of List>)
397401
-- = forall (a :: *) . (List a) -> (forall (out_List :: *) . (out_List -> (a -> List a -> out_List) -> out_List))
398402
-- @
399-
mkDestructorTy :: ann -> Type TyName uni ann -> Datatype TyName Name uni fun ann -> Type TyName uni ann
400-
mkDestructorTy ann pf dt@(Datatype _ _ tvs _ _) =
403+
mkDestructorTy :: PIRType uni a -> Datatype TyName Name uni fun (Provenance a) -> PIRType uni a
404+
mkDestructorTy pf dt@(Datatype ann _ tvs _ _) =
401405
-- we essentially "unveil" the abstract type, so this
402406
-- is a function from the (instantiated) abstract type
403407
-- to the (unwrapped, i.e. the pattern functor of the) "real" Scott-encoded type that we can use as
@@ -409,9 +413,8 @@ mkDestructorTy ann pf dt@(Datatype _ _ tvs _ _) =
409413
-- t t_1 .. t_n
410414
let appliedAbstract = mkDatatypeValueType ann dt
411415
-- forall t_1 .. t_n
412-
in
413-
PIR.mkIterTyForall tvs $
414-
TyFun ann appliedAbstract pf
416+
destrTy = PIR.mkIterTyForall tvs $ TyFun ann appliedAbstract pf
417+
in fmap (\a -> DatatypeComponent DestructorType a) destrTy
415418

416419
-- The main function
417420

@@ -425,8 +428,8 @@ compileDatatype r body d = do
425428
let
426429
tyVars = [PIR.defVar concreteTyDef]
427430
tys = [getType $ PIR.defVal concreteTyDef]
428-
vars = fmap PIR.defVar constrDefs ++ [PIR.defVar destrDef]
429-
vals = fmap PIR.defVal constrDefs ++ [PIR.defVal destrDef]
431+
vars = fmap PIR.defVar constrDefs ++ [ PIR.defVar destrDef ]
432+
vals = fmap PIR.defVal constrDefs ++ [ PIR.defVal destrDef ]
430433
-- See note [Abstract data types]
431434
pure $ PIR.mkIterApp p (PIR.mkIterInst p (PIR.mkIterTyAbs tyVars (PIR.mkIterLamAbs vars body)) tys) vals
432435

@@ -443,11 +446,11 @@ compileDatatypeDefs r d@(Datatype ann tn _ destr constrs) = do
443446

444447
constrDefs <- for (zip constrs [0..]) $ \(c, i) -> do
445448
let constrTy = mkConstructorType d c
446-
PIR.Def (VarDecl ann (_varDeclName c) constrTy) <$> mkConstructor (PIR.defVal concreteTyDef) d i
449+
PIR.Def (VarDecl (DatatypeComponent Constructor ann) (_varDeclName c) constrTy) <$> mkConstructor (PIR.defVal concreteTyDef) d i
447450

448451
destrDef <- do
449-
let destTy = mkDestructorTy ann pf d
450-
PIR.Def (VarDecl ann destr destTy) <$> mkDestructor (PIR.defVal concreteTyDef) d
452+
let destTy = mkDestructorTy pf d
453+
PIR.Def (VarDecl (DatatypeComponent Destructor ann) destr destTy) <$> mkDestructor (PIR.defVal concreteTyDef) d
451454

452455
pure (concreteTyDef, constrDefs, destrDef)
453456

plutus-core/plutus-ir/src/PlutusIR/Compiler/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ data CompilationOpts = CompilationOpts {
5757
makeLenses ''CompilationOpts
5858

5959
defaultCompilationOpts :: CompilationOpts
60-
defaultCompilationOpts = CompilationOpts True False False False 8 True True True False
60+
defaultCompilationOpts = CompilationOpts True False False False 12 True True True False
6161

6262
data CompilationCtx uni fun a = CompilationCtx {
6363
_ccOpts :: CompilationOpts

plutus-core/untyped-plutus-core/src/UntypedPlutusCore.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import PlutusCore.Name as Export
1414
import UntypedPlutusCore.Core as Export
1515
import UntypedPlutusCore.Core.Instance.Flat as Export
1616
import UntypedPlutusCore.DeBruijn as Export
17+
import UntypedPlutusCore.Simplify as Export
1718
import UntypedPlutusCore.Size as Export
1819
import UntypedPlutusCore.Subst as Export
19-
import UntypedPlutusCore.Transform.Simplify as Export
2020
-- Also has some functions
2121

2222

0 commit comments

Comments
 (0)