Skip to content

Commit

Permalink
[PlutusTx] [Builtins] Split 'FromBuiltin' and 'ToBuiltin' (#5971)
Browse files Browse the repository at this point in the history
This addresses the [inconsistencies](#5547 (comment)) that we got from using the now retired `FromBuiltin`/`ToBuiltin` classes for two mutually exclusive purposes. Now we have two versions of those classes: one is used for converting builtins to Plutus Tx types and back, while the other one is for making builtins transparent/opaque within Plutus Tx. These two purposes are very similar, but also subtly different and that is explained in the docs.
  • Loading branch information
effectfully authored May 15, 2024
1 parent 5f207e5 commit 92d94ca
Show file tree
Hide file tree
Showing 14 changed files with 521 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}

module PlutusCore.Builtin.Polymorphism
( Opaque (..)
Expand Down Expand Up @@ -214,7 +215,8 @@ type family AllElaboratedArgs constr x where
-- built-in type.
type AllBuiltinArgs
:: forall a. (GHC.Type -> GHC.Type) -> (GHC.Type -> GHC.Constraint) -> a -> GHC.Constraint
type AllBuiltinArgs uni constr x = AllElaboratedArgs constr (ElaborateBuiltin uni x)
class AllElaboratedArgs constr (ElaborateBuiltin uni x) => AllBuiltinArgs uni constr x
instance AllElaboratedArgs constr (ElaborateBuiltin uni x) => AllBuiltinArgs uni constr x

-- Custom type errors to guide the programmer adding a new built-in function.

Expand Down
2 changes: 1 addition & 1 deletion plutus-ledger-api/test-plugin/Spec/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ valueToLists = ListTx.map (fmap AssocMap.toList) . AssocMap.toList . getValue
eqValueCode :: CompiledCode Value -> CompiledCode Value -> (Bool, PLC.CountingSt)
eqValueCode valueCode1 valueCode2 = (res, cost) where
prog =
$$(compile [|| \value1 value2 -> toBuiltin ((value1 :: Value) == value2) ||])
$$(compile [|| \value1 value2 -> toOpaque ((value1 :: Value) == value2) ||])
`unsafeApplyCode` valueCode1 `unsafeApplyCode` valueCode2
(errOrRes, cost)
= PLC.runCekNoEmit PLC.defaultCekParameters PLC.counting
Expand Down
2 changes: 1 addition & 1 deletion plutus-tx-plugin/src/PlutusTx/Compiler/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module PlutusTx.Compiler.Builtins (
, lookupBuiltinType
, errorFunc) where

import PlutusTx.Builtins.Class qualified as Builtins
import PlutusTx.Builtins.HasOpaque qualified as Builtins
import PlutusTx.Builtins.Internal qualified as Builtins

import PlutusTx.Compiler.Error
Expand Down
2 changes: 1 addition & 1 deletion plutus-tx-plugin/src/PlutusTx/Compiler/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import PlutusTx.PIRTypes
import PlutusTx.PLCTypes (PLCType, PLCVar)

-- I feel like we shouldn't need this, we only need it to spot the special String type, which is annoying
import PlutusTx.Builtins.Class qualified as Builtins
import PlutusTx.Builtins.HasOpaque qualified as Builtins
import PlutusTx.Trace

import PlutusIR qualified as PIR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@
)
)
)
(termbind
(nonstrict)
(vardecl
`$fHasFromOpaqueBuiltinDataBuiltinData_$cfromOpaque`
(fun (con data) (con data))
)
(lam eta (con data) eta)
)
(termbind
(strict)
(vardecl
Expand All @@ -271,7 +279,7 @@
(termbind
(nonstrict)
(vardecl
`$fFromBuiltinBuiltinListList_$cfromBuiltin`
`$fHasFromOpaqueBuiltinListList_$cfromOpaque`
(all
arep
(type)
Expand All @@ -292,7 +300,7 @@
a
(type)
(lam
`$dFromBuiltin`
`$dHasFromOpaque`
[ [ (lam arep (type) (lam a (type) (fun arep a))) arep ] a ]
(let
(rec)
Expand Down Expand Up @@ -323,7 +331,7 @@
[
[
{ Cons a }
[ `$dFromBuiltin` [ { head arep } l ] ]
[ `$dHasFromOpaque` [ { head arep } l ] ]
]
[ go [ { tail arep } l ] ]
]
Expand All @@ -347,11 +355,6 @@
)
(builtin fstPair)
)
(termbind
(nonstrict)
(vardecl id (all a (type) (fun a a)))
(abs a (type) (lam x a x))
)
(termbind
(strict)
(vardecl
Expand Down Expand Up @@ -405,12 +408,12 @@
[
{
{
`$fFromBuiltinBuiltinListList_$cfromBuiltin`
`$fHasFromOpaqueBuiltinListList_$cfromOpaque`
(con data)
}
(con data)
}
{ id (con data) }
`$fHasFromOpaqueBuiltinDataBuiltinData_$cfromOpaque`
]
a
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Changed

- Split `PlutusTx.Builtins.Class` into `PlutusTx.Builtins.HasBuiltin` and `PlutusTx.Builtins.HasOpaque` in #5971:
+ Split 'FromBuiltin' into 'HasFromBuiltin' and 'HasFromOpaque'
+ Split 'ToBuiltin' into 'HasToBuiltin' and 'HasToOpaque'
4 changes: 3 additions & 1 deletion plutus-tx/plutus-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ library
PlutusTx.Blueprint.Write
PlutusTx.Bool
PlutusTx.Builtins
PlutusTx.Builtins.Class
PlutusTx.Builtins.HasBuiltin
PlutusTx.Builtins.HasOpaque
PlutusTx.Builtins.Internal
PlutusTx.Code
PlutusTx.Coverage
Expand Down Expand Up @@ -108,6 +109,7 @@ library
PlutusTx.IsData.Instances
PlutusTx.IsData.TH
PlutusTx.Lift.Instances
PlutusTx.Lift.TestInstances
PlutusTx.Lift.TH
PlutusTx.Lift.THUtils

Expand Down
Loading

0 comments on commit 92d94ca

Please sign in to comment.