Create a list of values ​​from a list of types

I have a level type list like this

data TList (ixs :: [*]) (f :: * -> *) where
  TNil  :: TList '[] f
  (:-:) :: f ix -> TList ixs f -> TList (ix ': ixs) f

And I'm trying to create a new TList using an existing one. The idea is to have a function

genTList :: TList ixs f -> t -> TList ixs g

where 't' is some function capable of constructing a value of type 'g x', where 'x' is one of the types that make up the list of ixs.

So that given

data Foo x

and (some)

generate :: forall x . Bar x

I could get something like this

genTList (Foo Int :-: Foo String) generate = Bar :-: Bar

So, essentially for each element “x” in the type list, I want to have the type “Bar x” and also build its value without a parametric constructor, because I know that “Bar x” has no constructor parameters.

- (https://gist.github.com/lolepezy/30820595afd9217083c5ca629e350b55), ().

, ?

+4
1

class TListGen ixs (g :: * -> *) where
  genTList :: Proxy ixs -> g ix -> TList ixs g

, , , , , ix.

, ,

genTList (Proxy :: Proxy '[Int, String]) (Just False)

, g Maybe ix Bool. . - , , , ix. , 2:

class TListGen ixs (g :: * -> *) where
  genTList :: Proxy ixs -> (forall ix . g ix) -> TList ixs g

RankNTypes.

, g. , Just False , Nothing .

, OVERLAPS , , ixs , TList:

class TListGen ixs (g :: * -> *) where
  genTList :: (forall ix . g ix) -> TList ixs g

instance TListGen '[] g where
  genTList _ = TNil

instance TListGen ixs g => TListGen (ix ': ixs) g where
  genTList g = g :-: genTList g

:

GHCi> genTList Nothing :: TList '[ Int, String ] Maybe

, , Show:

• No instance for (Show (TList '[Int, String] Maybe))
    arising from a use of ‘print’
• In a stmt of an interactive GHCi command: print it

Show TList, .

, , , generics-sop.

TList NP ( , ), genTList pure_NP,

GHCi> import Generics.SOP.NP
GHCi> pure_NP Nothing :: NP Maybe '[ Int, String ]
Nothing :* (Nothing :* Nil)
+6

Source: https://habr.com/ru/post/1678113/


All Articles