Skip to content

Commit 589516f

Browse files
authored
[Builtins] Remove 'caseList' and 'caseData' (#7190)
This removes the `caseList` and `caseData` builtins in favor of adding support for lists and `Data` to `Case`.
1 parent c6c7350 commit 589516f

File tree

42 files changed

+259
-689
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+259
-689
lines changed

plutus-benchmark/lists/src/PlutusBenchmark/Lists/Sum/HandWritten.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Control.Monad.Except
88
import Data.Either
99
import PlutusCore.Compiler.Erase (eraseTerm)
1010
import PlutusCore.StdLib.Data.List qualified as BuiltinList
11-
import PlutusCore.StdLib.Data.MatchOption (MatchOption (UseCase))
11+
import PlutusCore.StdLib.Data.MatchOption (MatchOption (UseChoose))
1212
import PlutusCore.StdLib.Data.ScottList qualified as ScottList
1313
import PlutusCore.Version qualified as PLC
1414
import PlutusTx qualified as Tx
@@ -23,11 +23,11 @@ mkBuiltinList l = compiledCodeToTerm (Tx.liftCodeDef $ BI.BuiltinList l)
2323

2424
mkSumLeftBuiltinTerm :: [Integer] -> Term
2525
mkSumLeftBuiltinTerm l =
26-
UPLC.Apply () (debruijnTermUnsafe $ eraseTerm (BuiltinList.sum UseCase)) (mkBuiltinList l)
26+
UPLC.Apply () (debruijnTermUnsafe $ eraseTerm (BuiltinList.sum UseChoose)) (mkBuiltinList l)
2727

2828
mkSumRightBuiltinTerm :: [Integer] -> Term
2929
mkSumRightBuiltinTerm l =
30-
UPLC.Apply () (debruijnTermUnsafe $ eraseTerm (BuiltinList.sumr UseCase)) (mkBuiltinList l)
30+
UPLC.Apply () (debruijnTermUnsafe $ eraseTerm (BuiltinList.sumr UseChoose)) (mkBuiltinList l)
3131

3232
mkScottList :: [Integer] -> Term
3333
mkScottList l = compiledCodeToTerm (Tx.liftCode PLC.plcVersion100 l)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Removed
2+
3+
- Removed the `caseList` and `caseData` builtins in preparation for adding direct `Case`ing on lists and `Data`.

plutus-core/plutus-core/examples/PlutusCore/Examples/Everything.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ examples =
3737
FolderContents
3838
[ treeFolderContents "Examples"
3939
[ treeFolderContents "Data"
40-
[ plcTermFile "ofoldrData" $ ofoldrData UseCase
41-
, plcTermFile "ofoldrDataViaChoose" $ ofoldrData UseChoose
40+
[ plcTermFile "ofoldrDataUseChoose" $ ofoldrData UseChoose
4241
, plcTermFile "exampleData" exampleData
4342
]
4443
, treeFolderContents "Function"
@@ -51,8 +50,9 @@ examples =
5150
, plcTermFile "FoldrInterList" foldrInterList
5251
]
5352
, treeFolderContents "List"
54-
[ plcTermFile "omapList" $ omapList UseCase
55-
, plcTermFile "omapListViaChoose" $ omapList UseChoose
53+
[ plcTermFile (name ++ show optMatch) $ f optMatch
54+
| optMatch <- enumerate
55+
, (name, f) <- [("omapList", omapList)]
5656
]
5757
, treeFolderContents "Pair"
5858
[ plcTermFile "obothPair" obothPair

plutus-core/plutus-core/src/PlutusCore/Builtin/KnownType.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ data Spine a
291291
-- constructor for performance reasons (it only takes a single pattern match to access the head when
292292
-- there's no spine this way, while otherwise we'd also need to match on the spine to ensure that
293293
-- it's empty -- and the no-spine case is by far the most common one, hence we want to optimize it).
294-
--
295-
-- Used in built-in functions returning function applications such as 'CaseList'.
296294
data HeadSpine a
297295
= HeadOnly a
298296
| HeadSpine a (Spine a)

plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs

Lines changed: 93 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ data DefaultFun
184184
| LengthOfArray
185185
| ListToArray
186186
| IndexArray
187-
-- Case
188-
| CaseList
189-
| CaseData
190187
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Ix)
191188
deriving anyclass (NFData, Hashable, PrettyBy PrettyConfigPlc)
192189

@@ -2102,45 +2099,6 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
21022099
{-# INLINE indexArrayDenotation #-}
21032100
in makeBuiltinMeaning indexArrayDenotation (runCostingFunTwoArguments . paramIndexArray)
21042101

2105-
toBuiltinMeaning _ver CaseList =
2106-
let caseListDenotation
2107-
:: Opaque val (LastArg a b)
2108-
-> Opaque val (a -> [a] -> b)
2109-
-> SomeConstant uni [a]
2110-
-> BuiltinResult (Opaque (HeadSpine val) b)
2111-
caseListDenotation z f (SomeConstant (Some (ValueOf uniListA xs0))) =
2112-
case uniListA of
2113-
DefaultUniList uniA -> pure $ case xs0 of
2114-
[] -> headSpine z []
2115-
x : xs -> headSpine f [fromValueOf uniA x, fromValueOf uniListA xs]
2116-
_ ->
2117-
-- See Note [Structural vs operational errors within builtins].
2118-
throwError $ structuralUnliftingError "Expected a list but got something else"
2119-
{-# INLINE caseListDenotation #-}
2120-
in makeBuiltinMeaning
2121-
caseListDenotation
2122-
(runCostingFunThreeArguments . unimplementedCostingFun)
2123-
2124-
toBuiltinMeaning _ver CaseData =
2125-
let caseDataDenotation
2126-
:: Opaque val (Integer -> [Data] -> b)
2127-
-> Opaque val ([(Data, Data)] -> b)
2128-
-> Opaque val ([Data] -> b)
2129-
-> Opaque val (Integer -> b)
2130-
-> Opaque val (BS.ByteString -> b)
2131-
-> Data
2132-
-> Opaque (HeadSpine val) b
2133-
caseDataDenotation fConstr fMap fList fI fB = \case
2134-
Constr i ds -> headSpine fConstr [fromValue i, fromValue ds]
2135-
Map es -> headSpine fMap [fromValue es]
2136-
List ds -> headSpine fList [fromValue ds]
2137-
I i -> headSpine fI [fromValue i]
2138-
B b -> headSpine fB [fromValue b]
2139-
{-# INLINE caseDataDenotation #-}
2140-
in makeBuiltinMeaning
2141-
caseDataDenotation
2142-
(runCostingFunSixArguments . unimplementedCostingFun)
2143-
21442102
-- See Note [Inlining meanings of builtins].
21452103
{-# INLINE toBuiltinMeaning #-}
21462104

@@ -2289,105 +2247,100 @@ instance Flat DefaultFun where
22892247
ListToArray -> 90
22902248
IndexArray -> 91
22912249

2292-
CaseList -> 126
2293-
CaseData -> 127
2294-
22952250
decode = go =<< decodeBuiltin
2296-
where go 0 = pure AddInteger
2297-
go 1 = pure SubtractInteger
2298-
go 2 = pure MultiplyInteger
2299-
go 3 = pure DivideInteger
2300-
go 4 = pure QuotientInteger
2301-
go 5 = pure RemainderInteger
2302-
go 6 = pure ModInteger
2303-
go 7 = pure EqualsInteger
2304-
go 8 = pure LessThanInteger
2305-
go 9 = pure LessThanEqualsInteger
2306-
go 10 = pure AppendByteString
2307-
go 11 = pure ConsByteString
2308-
go 12 = pure SliceByteString
2309-
go 13 = pure LengthOfByteString
2310-
go 14 = pure IndexByteString
2311-
go 15 = pure EqualsByteString
2312-
go 16 = pure LessThanByteString
2313-
go 17 = pure LessThanEqualsByteString
2314-
go 18 = pure Sha2_256
2315-
go 19 = pure Sha3_256
2316-
go 20 = pure Blake2b_256
2317-
go 21 = pure VerifyEd25519Signature
2318-
go 22 = pure AppendString
2319-
go 23 = pure EqualsString
2320-
go 24 = pure EncodeUtf8
2321-
go 25 = pure DecodeUtf8
2322-
go 26 = pure IfThenElse
2323-
go 27 = pure ChooseUnit
2324-
go 28 = pure Trace
2325-
go 29 = pure FstPair
2326-
go 30 = pure SndPair
2327-
go 31 = pure ChooseList
2328-
go 32 = pure MkCons
2329-
go 33 = pure HeadList
2330-
go 34 = pure TailList
2331-
go 35 = pure NullList
2332-
go 36 = pure ChooseData
2333-
go 37 = pure ConstrData
2334-
go 38 = pure MapData
2335-
go 39 = pure ListData
2336-
go 40 = pure IData
2337-
go 41 = pure BData
2338-
go 42 = pure UnConstrData
2339-
go 43 = pure UnMapData
2340-
go 44 = pure UnListData
2341-
go 45 = pure UnIData
2342-
go 46 = pure UnBData
2343-
go 47 = pure EqualsData
2344-
go 48 = pure MkPairData
2345-
go 49 = pure MkNilData
2346-
go 50 = pure MkNilPairData
2347-
go 51 = pure SerialiseData
2348-
go 52 = pure VerifyEcdsaSecp256k1Signature
2349-
go 53 = pure VerifySchnorrSecp256k1Signature
2350-
go 54 = pure Bls12_381_G1_add
2351-
go 55 = pure Bls12_381_G1_neg
2352-
go 56 = pure Bls12_381_G1_scalarMul
2353-
go 57 = pure Bls12_381_G1_equal
2354-
go 58 = pure Bls12_381_G1_compress
2355-
go 59 = pure Bls12_381_G1_uncompress
2356-
go 60 = pure Bls12_381_G1_hashToGroup
2357-
go 61 = pure Bls12_381_G2_add
2358-
go 62 = pure Bls12_381_G2_neg
2359-
go 63 = pure Bls12_381_G2_scalarMul
2360-
go 64 = pure Bls12_381_G2_equal
2361-
go 65 = pure Bls12_381_G2_compress
2362-
go 66 = pure Bls12_381_G2_uncompress
2363-
go 67 = pure Bls12_381_G2_hashToGroup
2364-
go 68 = pure Bls12_381_millerLoop
2365-
go 69 = pure Bls12_381_mulMlResult
2366-
go 70 = pure Bls12_381_finalVerify
2367-
go 71 = pure Keccak_256
2368-
go 72 = pure Blake2b_224
2369-
go 73 = pure IntegerToByteString
2370-
go 74 = pure ByteStringToInteger
2371-
go 75 = pure AndByteString
2372-
go 76 = pure OrByteString
2373-
go 77 = pure XorByteString
2374-
go 78 = pure ComplementByteString
2375-
go 79 = pure ReadBit
2376-
go 80 = pure WriteBits
2377-
go 81 = pure ReplicateByte
2378-
go 82 = pure ShiftByteString
2379-
go 83 = pure RotateByteString
2380-
go 84 = pure CountSetBits
2381-
go 85 = pure FindFirstSetBit
2382-
go 86 = pure Ripemd_160
2383-
go 87 = pure ExpModInteger
2384-
go 88 = pure DropList
2385-
go 89 = pure LengthOfArray
2386-
go 90 = pure ListToArray
2387-
go 91 = pure IndexArray
2388-
go 126 = pure CaseList
2389-
go 127 = pure CaseData
2390-
go t = fail $ "Failed to decode builtin tag, got: " ++ show t
2251+
where go 0 = pure AddInteger
2252+
go 1 = pure SubtractInteger
2253+
go 2 = pure MultiplyInteger
2254+
go 3 = pure DivideInteger
2255+
go 4 = pure QuotientInteger
2256+
go 5 = pure RemainderInteger
2257+
go 6 = pure ModInteger
2258+
go 7 = pure EqualsInteger
2259+
go 8 = pure LessThanInteger
2260+
go 9 = pure LessThanEqualsInteger
2261+
go 10 = pure AppendByteString
2262+
go 11 = pure ConsByteString
2263+
go 12 = pure SliceByteString
2264+
go 13 = pure LengthOfByteString
2265+
go 14 = pure IndexByteString
2266+
go 15 = pure EqualsByteString
2267+
go 16 = pure LessThanByteString
2268+
go 17 = pure LessThanEqualsByteString
2269+
go 18 = pure Sha2_256
2270+
go 19 = pure Sha3_256
2271+
go 20 = pure Blake2b_256
2272+
go 21 = pure VerifyEd25519Signature
2273+
go 22 = pure AppendString
2274+
go 23 = pure EqualsString
2275+
go 24 = pure EncodeUtf8
2276+
go 25 = pure DecodeUtf8
2277+
go 26 = pure IfThenElse
2278+
go 27 = pure ChooseUnit
2279+
go 28 = pure Trace
2280+
go 29 = pure FstPair
2281+
go 30 = pure SndPair
2282+
go 31 = pure ChooseList
2283+
go 32 = pure MkCons
2284+
go 33 = pure HeadList
2285+
go 34 = pure TailList
2286+
go 35 = pure NullList
2287+
go 36 = pure ChooseData
2288+
go 37 = pure ConstrData
2289+
go 38 = pure MapData
2290+
go 39 = pure ListData
2291+
go 40 = pure IData
2292+
go 41 = pure BData
2293+
go 42 = pure UnConstrData
2294+
go 43 = pure UnMapData
2295+
go 44 = pure UnListData
2296+
go 45 = pure UnIData
2297+
go 46 = pure UnBData
2298+
go 47 = pure EqualsData
2299+
go 48 = pure MkPairData
2300+
go 49 = pure MkNilData
2301+
go 50 = pure MkNilPairData
2302+
go 51 = pure SerialiseData
2303+
go 52 = pure VerifyEcdsaSecp256k1Signature
2304+
go 53 = pure VerifySchnorrSecp256k1Signature
2305+
go 54 = pure Bls12_381_G1_add
2306+
go 55 = pure Bls12_381_G1_neg
2307+
go 56 = pure Bls12_381_G1_scalarMul
2308+
go 57 = pure Bls12_381_G1_equal
2309+
go 58 = pure Bls12_381_G1_compress
2310+
go 59 = pure Bls12_381_G1_uncompress
2311+
go 60 = pure Bls12_381_G1_hashToGroup
2312+
go 61 = pure Bls12_381_G2_add
2313+
go 62 = pure Bls12_381_G2_neg
2314+
go 63 = pure Bls12_381_G2_scalarMul
2315+
go 64 = pure Bls12_381_G2_equal
2316+
go 65 = pure Bls12_381_G2_compress
2317+
go 66 = pure Bls12_381_G2_uncompress
2318+
go 67 = pure Bls12_381_G2_hashToGroup
2319+
go 68 = pure Bls12_381_millerLoop
2320+
go 69 = pure Bls12_381_mulMlResult
2321+
go 70 = pure Bls12_381_finalVerify
2322+
go 71 = pure Keccak_256
2323+
go 72 = pure Blake2b_224
2324+
go 73 = pure IntegerToByteString
2325+
go 74 = pure ByteStringToInteger
2326+
go 75 = pure AndByteString
2327+
go 76 = pure OrByteString
2328+
go 77 = pure XorByteString
2329+
go 78 = pure ComplementByteString
2330+
go 79 = pure ReadBit
2331+
go 80 = pure WriteBits
2332+
go 81 = pure ReplicateByte
2333+
go 82 = pure ShiftByteString
2334+
go 83 = pure RotateByteString
2335+
go 84 = pure CountSetBits
2336+
go 85 = pure FindFirstSetBit
2337+
go 86 = pure Ripemd_160
2338+
go 87 = pure ExpModInteger
2339+
go 88 = pure DropList
2340+
go 89 = pure LengthOfArray
2341+
go 90 = pure ListToArray
2342+
go 91 = pure IndexArray
2343+
go t = fail $ "Failed to decode builtin tag, got: " ++ show t
23912344

23922345
size _ n = n + builtinTagWidth
23932346

plutus-core/plutus-core/stdlib/PlutusCore/StdLib/Data/Data.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,6 @@ matchData optMatch = runQuote $ do
9292
. lamAbs () fI (TyFun () integer $ TyVar () r)
9393
. lamAbs () fB (TyFun () bytestring $ TyVar () r)
9494
$ case optMatch of
95-
UseCase ->
96-
mkIterAppNoAnn (tyInst () (builtin () CaseData) $ TyVar () r)
97-
[ var () fConstr
98-
, var () fMap
99-
, var () fList
100-
, var () fI
101-
, var () fB
102-
, var () d
103-
]
10495
UseChoose ->
10596
mkIterAppNoAnn (tyInst () (builtin () ChooseData) . TyFun () unit $ TyVar () r)
10697
[ var () d

plutus-core/plutus-core/stdlib/PlutusCore/StdLib/Data/List.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ matchList optMatch = runQuote $ do
7474
. lamAbs () z (TyVar () r)
7575
. lamAbs () f (TyFun () (TyVar () a) . TyFun () listA $ TyVar () r)
7676
$ case optMatch of
77-
UseCase ->
78-
mkIterAppNoAnn
79-
(mkIterInstNoAnn (builtin () CaseList)
80-
[ TyVar () a
81-
, TyVar () r
82-
])
83-
[ var () z
84-
, var () f
85-
, var () xs
86-
]
8777
UseChoose ->
8878
mkIterAppNoAnn
8979
(mkIterInstNoAnn (builtin () ChooseList)

plutus-core/plutus-core/stdlib/PlutusCore/StdLib/Data/MatchOption.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ module PlutusCore.StdLib.Data.MatchOption
22
( MatchOption (..)
33
) where
44

5-
-- | Allows one to choose which way of doing pattern matching on built-in types to use: either via
6-
-- 'ChooseList'-like builtins or via 'CaseList'-like ones.
5+
-- | Allows one to choose which way of doing pattern matching on built-in types to use: currently
6+
-- only 'ChooseList'-like builtins are supported.
77
data MatchOption
88
= UseChoose
9-
| UseCase
109
deriving stock (Show, Eq, Bounded, Enum)

plutus-core/plutus-core/stdlib/PlutusCore/StdLib/Everything.hs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module PlutusCore.StdLib.Everything
1010
( stdLib
1111
) where
1212

13+
import PlutusPrelude
14+
1315
import PlutusCore.Default
1416
import PlutusCore.FsTree
1517

@@ -63,18 +65,18 @@ stdLib =
6365
, plcTermFile "Uncurry" Builtin.uncurry
6466
]
6567
, treeFolderContents "List"
66-
[ plcTypeFile "List" list
67-
, plcTermFile "MatchList" $ Builtin.matchList UseCase
68-
, plcTermFile "FoldrList" $ Builtin.foldrList UseCase
69-
, plcTermFile "FoldList" $ Builtin.foldList UseCase
70-
, plcTermFile "MatchListViaChoose" $ Builtin.matchList UseChoose
71-
, plcTermFile "FoldrListViaChoose" $ Builtin.foldrList UseChoose
72-
, plcTermFile "FoldListViaChoose" $ Builtin.foldList UseChoose
73-
]
68+
$ plcTypeFile "List" list
69+
: [ plcTermFile (name ++ show optMatch) $ f optMatch
70+
| optMatch <- enumerate
71+
, (name, f) <-
72+
[ ("MatchList", Builtin.matchList)
73+
, ("FoldrList", Builtin.foldrList)
74+
, ("FoldList", Builtin.foldList)
75+
]
76+
]
7477
, treeFolderContents "Data"
75-
[ plcTypeFile "Data" dataTy
76-
, plcTermFile "matchData" $ matchData UseCase
77-
, plcTermFile "matchDataViaChoose" $ matchData UseChoose
78+
[ plcTypeFile "Data" dataTy
79+
, plcTermFile "matchDataUseChoose" $ matchData UseChoose
7880
]
7981
, treeFolderContents "ScottList"
8082
[ plcTypeFile "List" listTy

0 commit comments

Comments
 (0)