Skip to content

Commit 92e10cb

Browse files
committed
Rebase pretty-printing for Rocq cert and get everything compiling
1 parent fd1c05b commit 92e10cb

File tree

33 files changed

+611
-60
lines changed

33 files changed

+611
-60
lines changed

plutus-core/executables/plutus/AnyProgram/Compile.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ compileProgram = curry $ \case
7979
-- pir to plc
8080
----------------------------------------
8181
(SPir n1@SName a1, SPlc n2 SUnit) ->
82-
withA @Ord a1 $ withA @Pretty a1 $ withA @AnnInline a1 $
82+
withA @Ord a1 $ withA @Pretty a1 $ withA @Show a1 $ withA @AnnInline a1 $
8383
-- Note: PIR.compileProgram subsumes pir typechecking
84-
(PLC.runQuoteT . flip runReaderT compCtx . PIR.compileProgram)
84+
(PLC.runQuoteT . flip runReaderT compCtx . PIR.compileProgram (const (return ())))
8585
>=> plcToOutName n1 n2
8686
-- completely drop annotations for now
8787
>=> pure . void

plutus-core/plutus-core.cabal

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cabal-version: 3.0
2-
name: plutus-core
3-
version: 1.46.0.0
4-
license: Apache-2.0
1+
cabal-version: 3.0
2+
name: plutus-core
3+
version: 1.46.0.0
4+
license: Apache-2.0
55
license-files:
66
LICENSE
77
NOTICE
@@ -521,6 +521,7 @@ library plutus-ir
521521
PlutusIR.Core.Instance.Pretty
522522
PlutusIR.Core.Instance.Pretty.Readable
523523
PlutusIR.Core.Instance.Scoping
524+
PlutusIR.Core.Instance.ShowRocq
524525
PlutusIR.Core.Plated
525526
PlutusIR.Core.Type
526527
PlutusIR.Error
@@ -570,6 +571,7 @@ library plutus-ir
570571
build-depends:
571572
, algebraic-graphs >=0.7
572573
, base >=4.9 && <5
574+
, bytestring
573575
, containers
574576
, data-default-class
575577
, dlist
@@ -591,6 +593,7 @@ library plutus-ir
591593
, semigroups >=0.19.1
592594
, text
593595
, transformers
596+
, vector
594597
, witherable
595598

596599
test-suite plutus-ir-test

plutus-core/plutus-core/src/PlutusCore/Core/Type.hs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ module PlutusCore.Core.Type
4141
, termAnn
4242
, typeAnn
4343
, mapFun
44+
, mapUniType
45+
, mapUniTerm
4446
, tyVarDeclAnn
4547
, tyVarDeclName
4648
, tyVarDeclKind
@@ -185,15 +187,43 @@ data TyVarDecl tyname ann = TyVarDecl
185187
{ _tyVarDeclAnn :: ann
186188
, _tyVarDeclName :: tyname
187189
, _tyVarDeclKind :: Kind ann
188-
} deriving stock (Functor, Show, Generic)
190+
} deriving stock (Functor, Generic)
191+
192+
-- | Custom instance that behaves as a derived instance of Show, but without record syntax.
193+
-- Used in the Rocq PIR certifier dumping format.
194+
instance (Show tn, Show ann) => Show (TyVarDecl tn ann) where
195+
showsPrec p (TyVarDecl x y z) =
196+
showParen (p >= 11) $
197+
showString "TyVarDecl" .
198+
showString " " .
199+
showsPrec 11 x .
200+
showString " " .
201+
showsPrec 11 y .
202+
showString " " .
203+
showsPrec 11 z
204+
189205
makeLenses ''TyVarDecl
190206

191207
-- | A "variable declaration", i.e. a name and a type for a variable.
192208
data VarDecl tyname name uni ann = VarDecl
193209
{ _varDeclAnn :: ann
194210
, _varDeclName :: name
195211
, _varDeclType :: Type tyname uni ann
196-
} deriving stock (Functor, Show, Generic)
212+
} deriving stock (Functor, Generic)
213+
214+
-- | Custom instance that behaves as a derived instance of Show, but without record syntax.
215+
-- Used in the Rocq PIR certifier dumping format.
216+
instance (Show tn, Show ann, GShow uni, Show n) => Show (VarDecl tn n uni ann) where
217+
showsPrec p (VarDecl x y z) =
218+
showParen (p >= 11) $
219+
showString "VarDecl" .
220+
showString " " .
221+
showsPrec 11 x .
222+
showString " " .
223+
showsPrec 11 y .
224+
showString " " .
225+
showsPrec 11 z
226+
197227
makeLenses ''VarDecl
198228

199229
-- | A "type declaration", i.e. a kind for a type.
@@ -270,6 +300,43 @@ mapFun f = go where
270300
go (Constr ann ty i args) = Constr ann ty i (map go args)
271301
go (Case ann ty arg cs) = Case ann ty (go arg) (map go cs)
272302

303+
mapUniType
304+
:: forall uni uni' tyname ann.
305+
(SomeTypeIn uni -> SomeTypeIn uni')
306+
-> Type tyname uni ann
307+
-> Type tyname uni' ann
308+
mapUniType f = go where
309+
go :: Type tyname uni ann -> Type tyname uni' ann
310+
go (TyVar ann name) = TyVar ann name
311+
go (TyFun ann ty1 ty2) = TyFun ann (go ty1) (go ty2)
312+
go (TyIFix ann ty1 ty2) = TyIFix ann (go ty1) (go ty2)
313+
go (TyForall ann name k t) = TyForall ann name k (go t)
314+
go (TyBuiltin ann con) = TyBuiltin ann (f con)
315+
go (TyLam ann name k t) = TyLam ann name k (go t)
316+
go (TyApp ann t1 t2) = TyApp ann (go t1) (go t2)
317+
go (TySOP ann tys) = TySOP ann (map (map go) tys)
318+
319+
mapUniTerm
320+
:: forall uni uni' tyname name fun ann.
321+
(SomeTypeIn uni -> SomeTypeIn uni')
322+
-> (Some (ValueOf uni) -> Some (ValueOf uni'))
323+
-> Term tyname name uni fun ann
324+
-> Term tyname name uni' fun ann
325+
mapUniTerm fTy fCon = go where
326+
go :: Term tyname name uni fun ann -> Term tyname name uni' fun ann
327+
go (LamAbs ann name ty body) = LamAbs ann name (mapUniType fTy ty) (go body)
328+
go (TyAbs ann name kind body) = TyAbs ann name kind (go body)
329+
go (IWrap ann pat arg term) = IWrap ann (mapUniType fTy pat) (mapUniType fTy arg) (go term)
330+
go (Apply ann fun arg) = Apply ann (go fun) (go arg)
331+
go (Unwrap ann term) = Unwrap ann (go term)
332+
go (Error ann ty) = Error ann (mapUniType fTy ty)
333+
go (TyInst ann term ty) = TyInst ann (go term) (mapUniType fTy ty)
334+
go (Var ann name) = Var ann name
335+
go (Constant ann con) = Constant ann (fCon con)
336+
go (Builtin ann fun) = Builtin ann fun
337+
go (Constr ann ty i args) = Constr ann (mapUniType fTy ty) i (map go args)
338+
go (Case ann ty arg cs) = Case ann (mapUniType fTy ty) (go arg) (map go cs)
339+
273340
-- | This is a wrapper to mark the place where the binder is introduced (i.e. LamAbs/TyAbs)
274341
-- and not where it is actually used (TyVar/Var..).
275342
-- This marking allows us to skip the (de)serialization of binders at LamAbs/TyAbs positions

plutus-core/plutus-core/src/PlutusCore/Name/Unique.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ data Name = Name
6060
, _nameUnique :: Unique
6161
-- ^ A 'Unique' assigned to the name, allowing for cheap comparisons in the compiler.
6262
}
63-
deriving stock (Show, Generic, Lift)
63+
deriving stock (Generic, Lift, Show)
6464
deriving anyclass (NFData)
6565

6666
-- | Allowed characters in the starting position of a non-quoted identifier.
@@ -93,7 +93,7 @@ toPrintedName txt = if isValidUnquotedName txt then txt else "`" <> txt <> "`"
9393
those used for terms.
9494
-}
9595
newtype TyName = TyName {unTyName :: Name}
96-
deriving stock (Show, Generic, Lift)
96+
deriving stock (Generic, Lift, Show)
9797
deriving newtype (Eq, Ord, NFData, Hashable, PrettyBy config)
9898

9999
instance Wrapped TyName
@@ -123,7 +123,7 @@ instance Hashable Name where
123123

124124
-- | A unique identifier
125125
newtype Unique = Unique {unUnique :: Int}
126-
deriving stock (Eq, Show, Ord, Lift)
126+
deriving stock (Eq, Ord, Lift, Generic, Show)
127127
deriving newtype (Enum, NFData, Pretty, Hashable)
128128

129129
-- | The unique of a type-level name.

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import Control.Monad.Except
6161
import Control.Monad.Extra (orM, whenM)
6262
import Data.Monoid
6363
import Data.Monoid.Extra (mwhen)
64+
import Data.Text (Text)
6465
import Debug.Trace (traceM)
6566
import PlutusCore qualified as PLC
6667
import PlutusCore.Error (throwingEither)
@@ -89,6 +90,8 @@ import PlutusIR.Transform.ThunkRecursions qualified as ThunkRec
8990
import PlutusIR.Transform.Unwrap qualified as Unwrap
9091
import PlutusPrelude
9192

93+
import PlutusIR.Core.Instance.ShowRocq
94+
9295
isVerbose :: Compiling m e uni fun a => m Bool
9396
isVerbose = view $ ccOpts . coVerbose
9497

@@ -101,11 +104,18 @@ logVerbose = whenM (orM [isVerbose, isDebug]) . traceM
101104
logDebug :: Compiling m e uni fun a => String -> m ()
102105
logDebug = whenM isDebug . traceM
103106

104-
runCompilerPass :: (Compiling m e uni fun a, b ~ Provenance a) => m (P.Pass m tyname name uni fun b) -> Term tyname name uni fun b -> m (Term tyname name uni fun b)
105-
runCompilerPass mpasses t = do
107+
runCompilerPass
108+
:: ( Compiling m e uni fun a, b ~ Provenance a
109+
, ShowRocq tyname name uni fun a
110+
)
111+
=> (Text -> m ())
112+
-> m (P.Pass m tyname name uni fun b)
113+
-> Term tyname name uni fun b
114+
-> m (Term tyname name uni fun b)
115+
runCompilerPass dumpCert mpasses t = do
106116
passes <- mpasses
107117
pedantic <- view (ccOpts . coPedantic)
108-
res <- runExceptT $ P.runPass logVerbose pedantic passes t
118+
res <- runExceptT $ P.runPass logVerbose dumpCert pedantic passes t
109119
throwingEither _Error res
110120

111121
floatOutPasses :: Compiling m e uni fun a => m (P.Pass m TyName Name uni fun (Provenance a))
@@ -181,20 +191,34 @@ dce = do
181191
-- to dump a "readable" version of pir (i.e. floated).
182192
compileToReadable
183193
:: forall m e uni fun a b
184-
. (Compiling m e uni fun a, b ~ Provenance a)
185-
=> Program TyName Name uni fun b
194+
. (Compiling m e uni fun a, b ~ Provenance a
195+
, PLC.Everywhere uni (ComposeC Show AsRocq)
196+
, PLC.GShow (AsRocqUni uni)
197+
, Show fun
198+
)
199+
=> (Text -> m ())
200+
-> Program TyName Name uni fun b
186201
-> m (Program TyName Name uni fun b)
187-
compileToReadable (Program a v t) = do
202+
compileToReadable dumpCert (Program a v t) = do
188203
validateOpts v
189204
let pipeline :: m (P.Pass m TyName Name uni fun b)
190205
pipeline = ala Ap foldMap [typeCheckTerm, dce, simplifier, floatOutPasses]
191-
Program a v <$> runCompilerPass pipeline t
206+
Program a v <$> runCompilerPass dumpCert pipeline t
192207

193208
-- | The 2nd half of the PIR compiler pipeline.
194209
-- Compiles a 'Term' into a PLC Term, by removing/translating step-by-step the PIR's language constructs to PLC.
195210
-- Note: the result *does* have globally unique names.
196-
compileReadableToPlc :: forall m e uni fun a b . (Compiling m e uni fun a, b ~ Provenance a) => Program TyName Name uni fun b -> m (PLCProgram uni fun a)
197-
compileReadableToPlc (Program a v t) = do
211+
compileReadableToPlc
212+
:: forall m e uni fun a b
213+
. (Compiling m e uni fun a, b ~ Provenance a
214+
, PLC.Everywhere uni (ComposeC Show AsRocq)
215+
, PLC.GShow (AsRocqUni uni)
216+
, Show fun
217+
)
218+
=> (Text -> m ())
219+
-> Program TyName Name uni fun b
220+
-> m (PLCProgram uni fun a)
221+
compileReadableToPlc dumpCert (Program a v t) = do
198222

199223
let
200224
pipeline :: m (P.Pass m TyName Name uni fun b)
@@ -216,18 +240,23 @@ compileReadableToPlc (Program a v t) = do
216240
]
217241

218242
go =
219-
runCompilerPass pipeline
243+
runCompilerPass dumpCert pipeline
220244
>=> (<$ logVerbose " !!! lowerTerm")
221245
>=> lowerTerm
222246

223247
PLC.Program a v <$> go t
224248

225249
--- | Compile a 'Program' into a PLC Program. Note: the result *does* have globally unique names.
226-
compileProgram :: Compiling m e uni fun a
227-
=> Program TyName Name uni fun a -> m (PLCProgram uni fun a)
228-
compileProgram =
250+
compileProgram :: ( Compiling m e uni fun a
251+
, PLC.Everywhere uni (ComposeC Show AsRocq)
252+
, PLC.GShow (AsRocqUni uni)
253+
, Show fun
254+
)
255+
=> (Text -> m ()) ->
256+
Program TyName Name uni fun a -> m (PLCProgram uni fun a)
257+
compileProgram dumpCert =
229258
(pure . original)
230259
>=> (<$ logDebug "!!! compileToReadable")
231-
>=> compileToReadable
260+
>=> (compileToReadable dumpCert)
232261
>=> (<$ logDebug "!!! compileReadableToPlc")
233-
>=> compileReadableToPlc
262+
>=> (compileReadableToPlc dumpCert)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ Also we should pull out more stuff (e.g. see 'NonStrict' which uses unit).
4141
data LetKind = RecTerms | NonRecTerms | Types | DataTypes
4242
deriving stock (Show, Eq, Ord)
4343

44+
kindToPass :: LetKind -> PassId
45+
kindToPass k = case k of
46+
RecTerms -> PassCompileLetRec
47+
NonRecTerms -> PassCompileLetNonRec
48+
Types -> PassCompileLetType
49+
DataTypes -> PassCompileLetData
50+
4451
compileLetsPassSC
4552
:: Compiling m e uni fun a
4653
=> TC.PirTCConfig uni fun
@@ -57,6 +64,7 @@ compileLetsPass
5764
compileLetsPass tcconfig letKind =
5865
NamedPass "compile lets" $
5966
Pass
67+
(kindToPass letKind)
6068
(compileLets letKind)
6169
[Typechecks tcconfig, GloballyUniqueNames]
6270
[ConstCondition (Typechecks tcconfig)]

0 commit comments

Comments
 (0)