Liqwid-Labs / agora

Governance modules for Cardano protocols

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Drop uses of `Plutarch.Monadic` in favour of `TermCont`.

emiflake opened this issue · comments

Plutarch.Monadic is likely to be dropped in the future in upstream Plutarch. TermCont is a much better tool altogether, and is only slightly less pretty-looking. It features less magic and you can create better logic functions by directly wiring continuations.

It seems like there also may be some utility functions that could make this a real superpower. Chase has echoed this sentiment in the discord, and see https://github.com/mlabs-haskell/liqwidx/issues/33

Yep, that's my idea too. There's also some useful aspects of allowing early returns for unwrapping partial types. We currently have Plutarch.Monadic-based passert, but it could make sense to have a tcassert instead, in a world where we're using TermCont.

Of course as with all usage of the continuation monad, we do need to be careful to not turn it into goto hell.

Does Plutarch have documentation for TermCont? Some basic introduction over it would be very helpful.

@SeungheonOh there is Do syntax with TermCont.md, which is a little sparse. But: "TermCont is the familiar Cont monad, specialized for Plutarch terms." Here I've laid them all in similar format, which might help see the differences / similarities.

newtype ContT    :: forall (r :: Type).  (Type -> Type) -> Type -> Type where
  ContT    :: forall r m a. {runContT    ::  (a -> m r)      -> m r      } -> ContT @r @m a

-- In practice, `Cont r a` is actually defined as `type Cont r = ContT r Identity`
newtype Cont    :: forall (r :: Type).                     Type -> Type where
  Cont     :: forall r a.   {runCont     ::  (a -> r)        -> r        } -> Cont @r a

newtype TermCont :: forall (r :: PType). S              -> Type -> Type where
  TermCont :: forall r s a. {runTermCont :: ((a -> Term s r) -> Term s r)} -> TermCont @r s a

Keeping that in mind, we can now just look at Cont documentation and it will give us intuition (in order of personal preference):

It doesn't seem that plutarch is fully ready for TermCont right? For example, we have to wrap pletFields in TermCont to use it in the monad.

It doesn't seem that plutarch is fully ready for TermCont right? For example, we have to wrap pletFields in TermCont to use it in the monad.

There are some ergonomics issues, yeah. See https://github.com/mlabs-haskell/liqwidx/issues/33. But we can temporarily write our own helpers until it's fixed upstream. Specifically pletFields is quite hard, though. I might patch that in the fork for now.