Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphically deriving PLift,PConstant without the headaches #286

Closed
t1lde opened this issue Feb 15, 2022 · 3 comments · Fixed by #354
Closed

Polymorphically deriving PLift,PConstant without the headaches #286

t1lde opened this issue Feb 15, 2022 · 3 comments · Fixed by #354
Labels
enhancement New feature or request

Comments

@t1lde
Copy link
Collaborator

t1lde commented Feb 15, 2022

DerivePConstantViaNewtype is pretty painful to use with polymorphic types - the Constraints required for the derived instances are leaky. An example is:

instance 
  forall a. 
  ( a ~ (PConstanted (PLifted a))
  , PUnsafeLiftDecl a
  ) =>
  PUnsafeLiftDecl (PFoo a) where 
  type PLifted (PFoo a) = Foo tag (PLifted a)

deriving via
  (DerivePConstantViaNewtype 
    (Foo  a)
    (PFoo (PConstanted a))
    (PConstanted a))
  instance 
  (a ~ (PLifted (PConstanted a))
  , PUnsafeLiftDecl (PConstanted a)
  , PConstant a
  ) =>
  PConstant (Foo a) 

One idea to simplify is with Constraint synonyms which provide a shorthand for the constraints:

type PConstantable :: Constraint
type PConstantable a =  (a ~ (PLifted (PConstant a)), PUnsafeLiftDecl (PConstanted a), PConstant a)

type PLiftable a :: Constraint
type PLiftable a = (a ~ (PConstanted (PLifted a)), PUnsafeLiftDecl a)
@t1lde t1lde added api enhancement New feature or request and removed api labels Feb 15, 2022
@peter-mlabs
Copy link

As an example of using Data instead, we can have

data Component a = Component
  {cValue :: Plutus.Value,
   cDatum :: a 
  }

data PComponent (a :: PType)  (s :: S)
  = PComponent (Term s (PDataRecord '["_0" ':= PValue, "_1" ':= a]))
  deriving stock (GHC.Generic)
  deriving anyclass (SOP.Generic, PIsDataRepr)
  deriving
    (PMatch, PIsData)
    via PIsDataReprInstances (PComponent a)

instance 
  forall a . 
  ( a ~ (PConstanted (PLifted a))
  , PUnsafeLiftDecl a
  ,  Ledger.FromData (Component (PLifted a))
  , Ledger.ToData (Component (PLifted a))
  ) =>
  PUnsafeLiftDecl (PComponent a) where 
  type PLifted (PComponent a) = Component (PLifted a) 


deriving via
  (DerivePConstantViaData  
    (Component a)
    (PComponent (PConstanted a)))
  instance 
  (a ~ (PLifted (PConstanted a))
  , PUnsafeLiftDecl (PConstanted a)
  , PConstant a
  , Ledger.FromData (Component a)
  , Ledger.ToData (Component a)
  ) =>
  PConstant (Component a)

@L-as
Copy link
Member

L-as commented Feb 15, 2022

For type PLiftable a = (a ~ (PConstanted (PLifted a)), PUnsafeLiftDecl a), you should just make PLift a do this @t1lde . This ought to be enough in 99% of cases.

@L-as
Copy link
Member

L-as commented Feb 15, 2022

@peter-mlabs if you want to you can try contributing the PR

@TotallyNotChase TotallyNotChase linked a pull request Mar 4, 2022 that will close this issue
@L-as L-as closed this as completed Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants