Skip to content

Commit e366a0d

Browse files
committed
cool
1 parent 747f534 commit e366a0d

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Instance/Pretty/Readable.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module UntypedPlutusCore.Core.Instance.Pretty.Readable () where
1212
import PlutusCore.Pretty.PrettyConst
1313
import PlutusCore.Pretty.Readable
1414
import PlutusPrelude
15+
import Prettyprinter.Custom (parens')
1516
import UntypedPlutusCore.Core.Type
1617

1718
import Prettyprinter
@@ -53,6 +54,10 @@ instance
5354
Constr _ i es -> iterAppDocM $ \_ prettyArg ->
5455
("constr" <+> prettyArg i) :| [prettyArg es]
5556
Case _ arg cs -> iterAppDocM $ \_ prettyArg -> "case" :| [prettyArg arg, prettyArg (toList cs)]
57+
Let _ ns t -> iterAppDocM $ \_ prettyArg ->
58+
"let" :| [parens' (sep $ prettyArg <$> ns), prettyArg t]
59+
Bind _ t bs -> iterAppDocM $ \_ prettyArg ->
60+
"bind" :| [prettyArg t, prettyArg bs]
5661

5762
instance
5863
(PrettyReadableBy configName (Term name uni fun a)) =>

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Core/Instance/Scoping.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ instance name ~ Name => EstablishScoping (Term name uni fun) where
5050
-- that none of the transformations leak variables outside of the branch they're bound in.
5151
pure . referenceOutOfScope branchBounds $
5252
Case NotAName aScoped . Vector.fromList $ map referenceInBranch esScopedPoked
53+
establishScoping (Let _ _ns _t) = error "no"
54+
establishScoping (Bind _ _t _bs) = error "no"
55+
-- TODO: Current scope checking uses `NameAnn` which only allows a single name to be annotated.
56+
-- It's hard to support `Let` and `Bind` which binds multiple names at once
5357

5458
instance name ~ Name => EstablishScoping (Program name uni fun) where
5559
establishScoping (Program _ ver term) = Program NotAName ver <$> establishScoping term
@@ -65,6 +69,8 @@ instance name ~ Name => CollectScopeInfo (Term name uni fun) where
6569
collectScopeInfo (Builtin _ _) = mempty
6670
collectScopeInfo (Constr _ _ es) = foldMap collectScopeInfo es
6771
collectScopeInfo (Case _ arg cs) = collectScopeInfo arg <> foldMap collectScopeInfo cs
72+
collectScopeInfo (Let _ _ns _t) = error "no"
73+
collectScopeInfo (Bind _ _t _bs) = error "no"
6874

6975
instance name ~ Name => CollectScopeInfo (Program name uni fun) where
7076
collectScopeInfo (Program _ _ term) = collectScopeInfo term

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Evaluation/Machine/Cek/Internal.hs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ data CekValue uni fun ann =
308308
-- Check the docs of 'BuiltinRuntime' for details.
309309
-- | A constructor value, including fully computed arguments and the tag.
310310
| VConstr {-# UNPACK #-} !Word64 !(EmptyOrMultiStack uni fun ann)
311-
| VBinds !(ArgStackNonEmpty uni fun ann)
311+
| VLet ![NamedDeBruijn] !(NTerm uni fun ann) !(CekValEnv uni fun ann)
312312

313313
deriving stock instance (GShow uni, Everywhere uni Show, Show fun, Show ann, Closed uni)
314314
=> Show (CekValue uni fun ann)
@@ -640,6 +640,11 @@ dischargeCekValue value0 = DischargeNonConstant $ goValue value0 where
640640
-- @term@ is fully discharged, so we can return it directly without any further discharging.
641641
VBuiltin _ term _ -> term
642642
VConstr ind args -> Constr () ind . map goValue $ argStackToList args
643+
VLet names body env ->
644+
Let
645+
()
646+
((\(NamedDeBruijn n _ix) -> NamedDeBruijn n deBruijnInitIndex) <$> names)
647+
(goValEnv env 1 body)
643648

644649
-- Instantiate all the free variables of a term by looking them up in an environment.
645650
-- Mutually recursive with @goValue@.
@@ -670,6 +675,8 @@ dischargeCekValue value0 = DischargeNonConstant $ goValue value0 where
670675
Error _ -> Error ()
671676
Constr _ ind args -> Constr () ind $ map (go shift) args
672677
Case _ scrut alts -> Case () (go shift scrut) $ fmap (go shift) alts
678+
Let _ names body -> Let () names (go (shift + fromIntegral (length names)) body)
679+
Bind _ t bs -> Bind () (go shift t) (fmap (go shift) bs)
673680

674681
instance (PrettyUni uni, Pretty fun) => PrettyBy PrettyConfigPlc (CekValue uni fun ann) where
675682
prettyBy cfg = prettyBy cfg . dischargeResultToTerm . dischargeCekValue
@@ -706,6 +713,8 @@ data Context uni fun ann
706713
-- ^ @(constr i V0 ... Vj-1 _ Nj ... Nn)@
707714
| FrameCases !(CekValEnv uni fun ann) !(V.Vector (NTerm uni fun ann)) !(Context uni fun ann)
708715
-- ^ @(case _ C0 .. Cn)@
716+
| FrameAwaitLetBinds !(CekValEnv uni fun ann) !(NTerm uni fun ann) ![NTerm uni fun ann] ![CekValue uni fun ann] !(Context uni fun ann)
717+
| FrameMine ![CekValue uni fun ann] !(Context uni fun ann)
709718
| NoFrame
710719

711720
deriving stock instance (GShow uni, Everywhere uni Show, Show fun, Show ann, Closed uni)
@@ -719,6 +728,7 @@ instance (Closed uni, uni `Everywhere` ExMemoryUsage) => ExMemoryUsage (CekValue
719728
VLamAbs {} -> singletonRose 1
720729
VBuiltin {} -> singletonRose 1
721730
VConstr {} -> singletonRose 1
731+
VLet {} -> singletonRose 1
722732
{-# INLINE memoryUsage #-}
723733

724734
{- Note [ArgStack vs Spine]
@@ -833,6 +843,16 @@ enterComputeCek = computeCek
833843
-- s ; ρ ▻ error ↦ <> A
834844
computeCek !_ !_ (Error _) =
835845
throwErrorWithCause (OperationalError CekEvaluationFailure) (Error ())
846+
-- ???
847+
computeCek !ctx !env (Let _ names body) = do
848+
stepAndMaybeSpend BLamAbs
849+
returnCek ctx (VLet names body env)
850+
computeCek !ctx !env (Bind _ body bs) = do
851+
--stepAndMaybeSpend BApply
852+
-- computeCek (FrameAwaitLetBinds env t bs ctx) env t
853+
case bs of
854+
(t : rest) -> computeCek (FrameAwaitLetBinds env body rest [] ctx) env t
855+
[] -> computeCek ctx env body
836856

837857
{- | The returning phase of the CEK machine.
838858
Returns 'EvaluationSuccess' in case the context is empty, otherwise pops up one frame
@@ -872,6 +892,11 @@ enterComputeCek = computeCek
872892
SpineLast arg -> applyEvaluate ctx fun (VCon arg)
873893
SpineCons arg rest -> applyEvaluate (FrameAwaitFunConN rest ctx) fun (VCon arg)
874894
-- s , [_ V1 .. Vn] ◅ lam x (M,ρ) ↦ s , [_ V2 .. Vn]; ρ [ x ↦ V1 ] ▻ M
895+
returnCek (FrameMine args ctx) l =
896+
case l of
897+
VLet _ body env -> computeCek ctx (foldr Env.cons env args) body
898+
_ -> error "no"
899+
875900
returnCek (FrameAwaitFunValueN args ctx) fun =
876901
case args of
877902
LastStackNonEmpty arg ->
@@ -906,6 +931,14 @@ enterComputeCek = computeCek
906931
Right (HeadOnly fX) -> computeCek ctx env fX
907932
Right (HeadSpine f xs) -> computeCek (FrameAwaitFunConN xs ctx) env f
908933
_ -> throwErrorDischarged (StructuralError NonConstrScrutinizedMachineError) e
934+
-- returnCek (FrameAwaitLetTerm env bs ctx) e =
935+
-- case bs of
936+
-- (next : todo) -> computeCek (FrameAwaitLetBinds env e todo [] ctx) env next
937+
-- [] -> returnCek ctx e -- no bindings
938+
returnCek (FrameAwaitLetBinds env l todo done ctx) e =
939+
case todo of
940+
(next : todo') -> computeCek (FrameAwaitLetBinds env l todo' (e : done) ctx) env next
941+
[] -> computeCek (FrameMine (e : done) ctx) env l
909942

910943
-- | @force@ a term and proceed.
911944
-- If v is a delay then compute the body of v;

0 commit comments

Comments
 (0)