plutarch
Safe HaskellNone
LanguageGHC2021

Plutarch.DataRepr.Internal.Field

Description

Deprecated: Use the new mechanism instead

Synopsis

PDataField class & deriving utils

class PDataFields (a :: S -> Type) where Source #

Class allowing letFields to work for a Plutarch type (`S -> Type`), usually via PIsDataRepr, but is derived for some other types for convenience.

Minimal complete definition

Nothing

Associated Types

type PFields (a :: S -> Type) :: [PLabeledType] Source #

Fields in HRec bound by letFields

type PFields (a :: S -> Type) = Helper (PInner a)

Methods

ptoFields :: forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)) Source #

Convert a Term to a PDataList

default ptoFields :: forall (s :: S). (PDataFields (PInner a), PFields (PInner a) ~ PFields a) => Term s a -> Term s (PDataRecord (PFields a)) Source #

Instances

Instances details
(PIsData a, PDataFields a) => PDataFields (PAsData a) Source # 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

Associated Types

type PFields (PAsData a) 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

type PFields (PAsData a) = PFields a

Methods

ptoFields :: forall (s :: S). Term s (PAsData a) -> Term s (PDataRecord (PFields (PAsData a))) Source #

PDataFields (PDataRecord as) Source # 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

Associated Types

type PFields (PDataRecord as) 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

type PFields (PDataRecord as) = as

Methods

ptoFields :: forall (s :: S). Term s (PDataRecord as) -> Term s (PDataRecord (PFields (PDataRecord as))) Source #

PDataFields (PDataSum '[as]) Source # 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

Associated Types

type PFields (PDataSum '[as]) 
Instance details

Defined in Plutarch.DataRepr.Internal.Field

type PFields (PDataSum '[as]) = as

Methods

ptoFields :: forall (s :: S). Term s (PDataSum '[as]) -> Term s (PDataRecord (PFields (PDataSum '[as]))) Source #

pletFields :: forall (fs :: [Symbol]) (a :: S -> Type) (s :: S) (b :: S -> Type) (ps :: [PLabeledType]) (bs :: [ToBind]). (PDataFields a, ps ~ PFields a, bs ~ Bindings ps fs, BindFields ps bs) => Term s a -> (HRecOf a fs s -> Term s b) -> Term s b Source #

Bind a HRec of named fields containing all the specified fields.

pfield :: forall (name :: Symbol) (b :: S -> Type) (p :: S -> Type) (s :: S) (a :: S -> Type) (as :: [PLabeledType]) (n :: Nat). (PDataFields p, as ~ PFields p, n ~ PLabelIndex name as, KnownNat n, a ~ PUnLabel (IndexList n as), PFromDataable a b) => Term s (p :--> b) Source #

Get a single field from a Term.

  • NB*: If you access more than one field from the same value you should use pletFields instead, which will generate the bindings more efficiently.

BindFields class mechanism

class BindFields (ps :: [PLabeledType]) (bs :: [ToBind]) where Source #

Methods

bindFields :: forall {r :: S -> Type} (s :: S). Proxy bs -> Term s (PDataRecord ps) -> TermCont s (HRec (BoundTerms ps bs s)) Source #

Bind all the fields in a PDataList term to a corresponding HList of Terms.

A continuation is returned to enable sharing of the generated bound-variables.

type family Bindings (ps :: [PLabeledType]) (fs :: [Symbol]) :: [ToBind] where ... Source #

Map BindField over [PLabeledType], with Skips removed at tail

Equations

Bindings ('[] :: [PLabeledType]) _1 = '[] :: [ToBind] 
Bindings ((name ':= _1) ': ps) fs = BindField name fs ': CutSkip (Bindings ps fs) 

type family BoundTerms (ps :: [PLabeledType]) (bs :: [ToBind]) (s :: S) :: [(Symbol, Type)] where ... Source #

Get the Term representations to be bound based on the result of Bindings.

Equations

BoundTerms ('[] :: [PLabeledType]) _1 _2 = '[] :: [(Symbol, Type)] 
BoundTerms _1 ('[] :: [ToBind]) _2 = '[] :: [(Symbol, Type)] 
BoundTerms (_1 ': ps) ('Skip ': bs) s = BoundTerms ps bs s 
BoundTerms ((name ':= p) ': ps) ('Bind ': bs) s = '(name, Term s (PAsData p)) ': BoundTerms ps bs s 

type family Drop (n :: Nat) (as :: [k]) :: [k] where ... Source #

Drop first n fields of a list

Equations

Drop 0 (xs :: [k]) = xs 
Drop n (_1 ': xs :: [k]) = Drop (n - 1) xs 

type HRecOf (t :: S -> Type) (fs :: [Symbol]) (s :: S) = HRec (BoundTerms (PFields t) (Bindings (PFields t) fs) s) Source #

The HRec yielded by 'pletFields @fs t'.

type family PMemberFields (t :: S -> Type) (fs :: [Symbol]) (s :: S) (as :: [(Symbol, Type)]) where ... Source #

Constrain an HRec to contain the specified fields from the given Plutarch type.

Example ===

import qualified GHC.Generics as GHC
import Generics.SOP

import Plutarch.Prelude
import Plutarch.DataRepr

newtype PFooType s = PFooType (Term s (PDataRecord '["frst" ':= PInteger, "scnd" ':= PBool, "thrd" ':= PString]))
  deriving stock (GHC.Generic)
  deriving anyclass (Generic)
  deriving anyclass (PIsDataRepr)
  deriving
    (PlutusType, PIsData, PDataFields, PEq)
    via PIsDataReprInstances PFooType

foo :: PMemberFields PFooType '["scnd", "frst"] s as => HRec as -> Term s PInteger
foo h = pif (getField "scnd" h) (getField "frst" h) 0

Equations

PMemberFields _1 ('[] :: [Symbol]) _2 _3 = () 
PMemberFields t (name ': rest) s as = (PMemberField t name s as, PMemberFields t rest s as) 

type family PMemberField (t :: S -> Type) (name :: Symbol) (s :: S) (as :: [(Symbol, Type)]) where ... Source #

Single field version of PMemberFields.

Equations

PMemberField t name s as = (IndexLabel name as ~ Term s (PAsData (PLookupLabel name (PFields t))), ElemOf name (Term s (PAsData (PLookupLabel name (PFields t)))) as) 

Re-exports

data HRec (as :: [(Symbol, Type)]) where Source #

Constructors

HNil :: HRec ('[] :: [(Symbol, Type)]) 
HCons :: forall (name :: Symbol) a (as1 :: [(Symbol, Type)]). Labeled name a -> HRec as1 -> HRec ('(name, a) ': as1) 

Instances

Instances details
(IndexLabel name as ~ a, ElemOf name a as, Term s (PAsData b) ~ a, PFromDataable b c) => HasField (name :: Symbol) (HRec as) (Term s c) Source # 
Instance details

Defined in Plutarch.DataRepr.Internal.HList

Methods

getField :: HRec as -> Term s c Source #

newtype Labeled (sym :: Symbol) a Source #

Constructors

Labeled 

Fields