33{-# LANGUAGE DefaultSignatures #-}
44{-# LANGUAGE PolyKinds #-}
55
6- {- |
7- Note about design decision on nested spines.
8- `getSpine (Just Value) = JustSpine ValueSpine` - looks more usable,
9- than `getSpine (Just Value) = JustSpine`.
10- But it seem to break deriving for parametised types like `Maybe a`,
11- and can be done with `fmap getSpine mValue`. Probably it actually
12- works exaclty for functorial parameters.
6+ {- | Spine is datatype, which tags only constructors of ADT skipping their content.
7+ TH deriving utility generates Spines which are Enums but one could introduce
8+ more complex Spine datatypes manually.
9+
10+ Initially this module didn't depend on any cardano code, and this state of
11+ things can be restored if needed. For Plutus version we attach some additional
12+ information to spines.
13+
14+ A note on design decision on nested spines.
15+
16+ `getSpine (Just Value) = JustSpine ValueSpine`
17+
18+ seems to be more sensible than:
19+
20+ `getSpine (Just Value) = JustSpine`.
21+
22+ But it seem to break deriving for parametised types like `Maybe a`,
23+ and can be done with `fmap getSpine mValue`. Probably it actually
24+ works exaclty for functorial parameters.
1325-}
14- module Data.Spine where
26+ module Data.Spine (
27+ -- * Common spines
28+ HasSpine (.. ),
29+ deriveSpine ,
30+ allSpines ,
31+
32+ -- * Plutus Spines
33+ HasPlutusSpine (.. ),
34+ derivePlutusSpine ,
35+ spineFieldsNum ,
36+ fieldNum ,
37+ ) where
1538
1639import Data.Data (Proxy )
1740import Data.List (elemIndex )
@@ -25,11 +48,6 @@ import PlutusTx (FromData, ToData, UnsafeFromData, unstableMakeIsData)
2548import Prelude
2649
2750-- | Definitions
28-
29- {- | Spine is datatype, which tags only constructors of ADT skipping their content.
30- TH deriving utility generates Spines which are Enums but one could introduce
31- more complex Spine datatypes manually.
32- -}
3351class
3452 ( Ord (Spine sop )
3553 , Show (Spine sop )
4159 type Spine sop = spine | spine -> sop
4260 getSpine :: sop -> Spine sop
4361
44- -- | Version of `HasSpine` knowing its Plutus Data encoding
62+ {- | Version of `HasSpine` that knows its Plutus Data encoding and keeps
63+ names of fields for every constructor.
64+ -}
4565class
4666 ( HasSpine sop
4767 , UnsafeFromData sop
@@ -59,7 +79,6 @@ spineFieldsNum :: forall sop. (HasPlutusSpine sop) => Spine sop -> Natural
5979spineFieldsNum spine =
6080 toNat $ length $ (fieldsMap @ sop ) Map. ! spine
6181
62- -- FIXME: use spine do discriminate
6382fieldNum ::
6483 forall sop label .
6584 (HasPlutusSpine sop , KnownSymbol label ) =>
@@ -72,7 +91,7 @@ fieldNum proxyLabel =
7291 fieldName = symbolVal proxyLabel
7392 fieldIndex dict = toNat <$> elemIndex fieldName dict
7493
75- allSpines :: forall sop . (HasPlutusSpine sop ) => [Spine sop ]
94+ allSpines :: forall sop . (HasSpine sop ) => [Spine sop ]
7695allSpines = [Prelude. minBound .. Prelude. maxBound ]
7796
7897-- | Phantom type param is required for `HasSpine` injectivity
@@ -91,7 +110,6 @@ addSuffix :: Name -> String -> Name
91110addSuffix (Name (OccName name) flavour) suffix =
92111 Name (OccName $ name <> suffix) flavour
93112
94- -- FIXME: cleaner return type
95113reifyDatatype :: Name -> Q (Name , [Name ], [[Name ]])
96114reifyDatatype ty = do
97115 (TyConI tyCon) <- reify ty
0 commit comments