-
Notifications
You must be signed in to change notification settings - Fork 63
/
Builtin.hs
246 lines (197 loc) · 7.49 KB
/
Builtin.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- This should have been called Plutarch.Data...
module Plutarch.Builtin (
PData (..),
pfstBuiltin,
psndBuiltin,
pasConstr,
pasMap,
pasList,
pasInt,
pasByteStr,
PBuiltinPair,
PBuiltinList (..),
pdataLiteral,
PIsData (..),
PAsData,
pforgetData,
ppairDataBuiltin,
type PBuiltinMap,
) where
import Plutarch (PlutusType (..), punsafeBuiltin, punsafeCoerce)
import Plutarch.Bool (PBool (..), PEq, pif', (#==))
import Plutarch.ByteString (PByteString)
import Plutarch.Integer (PInteger)
import Plutarch.Lift (
DerivePConstantViaCoercible (DerivePConstantViaCoercible),
PConstant,
PConstantRepr,
PConstanted,
PLift,
PLifted,
PUnsafeLiftDecl,
pconstant,
pconstantFromRepr,
pconstantToRepr,
)
import Plutarch.List (PListLike (..), plistEquals)
import Plutarch.Prelude
import qualified PlutusCore as PLC
import PlutusTx (Data)
-- | Plutus 'BuiltinPair'
data PBuiltinPair (a :: PType) (b :: PType) (s :: S)
instance (PLift a, PLift b) => PUnsafeLiftDecl (PBuiltinPair a b) where
type PLifted (PBuiltinPair a b) = (PLifted a, PLifted b)
-- FIXME: figure out good way of deriving this
instance (PConstant a, PConstant b) => PConstant (a, b) where
type PConstantRepr (a, b) = (PConstantRepr a, PConstantRepr b)
type PConstanted (a, b) = PBuiltinPair (PConstanted a) (PConstanted b)
pconstantToRepr (x, y) = (pconstantToRepr x, pconstantToRepr y)
pconstantFromRepr (x, y) = do
x' <- pconstantFromRepr @a x
y' <- pconstantFromRepr @b y
Just (x', y')
pfstBuiltin :: Term s (PBuiltinPair a b :--> a)
pfstBuiltin = phoistAcyclic $ pforce . pforce . punsafeBuiltin $ PLC.FstPair
psndBuiltin :: Term s (PBuiltinPair a b :--> b)
psndBuiltin = phoistAcyclic $ pforce . pforce . punsafeBuiltin $ PLC.SndPair
{- | Construct a builtin pair of 'PData' elements.
Uses 'PAsData' to preserve more information about the underlying 'PData'.
-}
ppairDataBuiltin :: Term s (PAsData a :--> PAsData b :--> PBuiltinPair (PAsData a) (PAsData b))
ppairDataBuiltin = punsafeBuiltin PLC.MkPairData
-- | Plutus 'BuiltinList'
data PBuiltinList (a :: PType) (s :: S)
= PCons (Term s a) (Term s (PBuiltinList a))
| PNil
pheadBuiltin :: Term s (PBuiltinList a :--> a)
pheadBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.HeadList
ptailBuiltin :: Term s (PBuiltinList a :--> PBuiltinList a)
ptailBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.TailList
pchooseListBuiltin :: Term s (PBuiltinList a :--> b :--> b :--> b)
pchooseListBuiltin = phoistAcyclic $ pforce $ pforce $ punsafeBuiltin PLC.ChooseList
pnullBuiltin :: Term s (PBuiltinList a :--> PBool)
pnullBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.NullList
pconsBuiltin :: Term s (a :--> PBuiltinList a :--> PBuiltinList a)
pconsBuiltin = phoistAcyclic $ pforce $ punsafeBuiltin PLC.MkCons
instance PConstant a => PConstant [a] where
type PConstantRepr [a] = [PConstantRepr a]
type PConstanted [a] = PBuiltinList (PConstanted a)
pconstantToRepr x = pconstantToRepr <$> x
pconstantFromRepr x = traverse (pconstantFromRepr @a) x
instance PUnsafeLiftDecl a => PUnsafeLiftDecl (PBuiltinList a) where
type PLifted (PBuiltinList a) = [PLifted a]
instance PLift a => PlutusType (PBuiltinList a) where
type PInner (PBuiltinList a) _ = PBuiltinList a
pcon' (PCons x xs) = pconsBuiltin # x # xs
pcon' PNil = pconstant []
pmatch' xs f =
pforce $
pchooseListBuiltin
# xs
# pdelay (f PNil)
# pdelay (f (PCons (pheadBuiltin # xs) (ptailBuiltin # xs)))
instance PListLike PBuiltinList where
type PElemConstraint PBuiltinList a = PLift a
pelimList match_cons match_nil ls = pmatch ls $ \case
PCons x xs -> match_cons x xs
PNil -> match_nil
pcons = plam $ \x xs -> pcon (PCons x xs)
pnil = pcon PNil
phead = pheadBuiltin
ptail = ptailBuiltin
pnull = pnullBuiltin
instance (PLift a, PEq a) => PEq (PBuiltinList a) where
(#==) xs ys = plistEquals # xs # ys
data PData s
= PDataConstr (Term s (PBuiltinPair PInteger (PBuiltinList PData)))
| PDataMap (Term s (PBuiltinList (PBuiltinPair PData PData)))
| PDataList (Term s (PBuiltinList PData))
| PDataInteger (Term s PInteger)
| PDataByteString (Term s PByteString)
instance PUnsafeLiftDecl PData where type PLifted PData = Data
deriving via (DerivePConstantViaCoercible Data PData Data) instance (PConstant Data)
instance PEq PData where
x #== y = punsafeBuiltin PLC.EqualsData # x # y
{- |
Map type used for Plutus `Data`'s Map constructor.
Note that the Plutus API doesn't use this most of the time,
instead encoding as a List of Tuple constructors.
Not to be confused with `PlutusTx.AssocMap.Map` / `PMap`
-}
type PBuiltinMap a b = (PBuiltinList (PBuiltinPair (PAsData a) (PAsData b)))
pasConstr :: Term s (PData :--> PBuiltinPair PInteger (PBuiltinList PData))
pasConstr = punsafeBuiltin PLC.UnConstrData
pasMap :: Term s (PData :--> PBuiltinList (PBuiltinPair PData PData))
pasMap = punsafeBuiltin PLC.UnMapData
pasList :: Term s (PData :--> PBuiltinList PData)
pasList = punsafeBuiltin PLC.UnListData
pasInt :: Term s (PData :--> PInteger)
pasInt = punsafeBuiltin PLC.UnIData
pasByteStr :: Term s (PData :--> PByteString)
pasByteStr = punsafeBuiltin PLC.UnBData
{-# DEPRECATED pdataLiteral "Use `pconstant` instead." #-}
pdataLiteral :: Data -> Term s PData
pdataLiteral = pconstant
data PAsData (a :: PType) (s :: S)
data PAsDataLifted (a :: PType)
instance PConstant (PAsDataLifted a) where
type PConstantRepr (PAsDataLifted a) = Data
type PConstanted (PAsDataLifted a) = PAsData a
pconstantToRepr = \case {}
pconstantFromRepr _ = Nothing
instance PUnsafeLiftDecl (PAsData a) where type PLifted (PAsData a) = PAsDataLifted a
pforgetData :: Term s (PAsData a) -> Term s PData
pforgetData = punsafeCoerce
class PIsData a where
pfromData :: Term s (PAsData a) -> Term s a
pdata :: Term s a -> Term s (PAsData a)
instance PIsData PData where
pfromData = punsafeCoerce
pdata = punsafeCoerce
instance PIsData a => PIsData (PBuiltinList (PAsData a)) where
pfromData x = punsafeCoerce $ pasList # pforgetData x
pdata x = punsafeBuiltin PLC.ListData # x
instance
( PIsData k
, PIsData v
) =>
PIsData (PBuiltinMap k v)
where
pfromData x = punsafeCoerce $ pasMap # pforgetData x
pdata x = punsafeBuiltin PLC.MapData # x
instance PIsData PInteger where
pfromData x = pasInt # pforgetData x
pdata x = punsafeBuiltin PLC.IData # x
instance PIsData PByteString where
pfromData x = pasByteStr # pforgetData x
pdata x = punsafeBuiltin PLC.BData # x
{- |
Instance for PBool following the Plutus IsData repr
given by @makeIsDataIndexed ''Bool [('False,0),('True,1)]@,
which is used in 'TxInfo' via 'Closure'.
-}
instance PIsData PBool where
pfromData x =
(phoistAcyclic $ plam toBool) # pforgetData x
where
toBool :: Term s PData -> Term s PBool
toBool d = pfstBuiltin # (pasConstr # d) #== 1
pdata x =
(phoistAcyclic $ plam toData) # x
where
toData :: Term s PBool -> Term s (PAsData PBool)
toData b =
punsafeBuiltin PLC.ConstrData
# (pif' # b # 1 # (0 :: Term s PInteger))
# nil
nil :: Term s (PBuiltinList PData)
nil = pnil
instance PIsData (PBuiltinPair PInteger (PBuiltinList PData)) where
pfromData x = pasConstr # pforgetData x
pdata x' = plet x' $ \x -> punsafeBuiltin PLC.ConstrData # (pfstBuiltin # x) #$ psndBuiltin # x
instance PEq (PAsData a) where
x #== y = punsafeBuiltin PLC.EqualsData # x # y