File: HTransExample.hs

package info (click to toggle)
haskell-generics-sop 0.5.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: haskell: 1,846; sh: 22; makefile: 4
file content (27 lines) | stat: -rw-r--r-- 933 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module HTransExample where

import Generics.SOP

class IsTupleTypeOf xs y | xs -> y where
  toTuple :: NP I xs -> y
  default toTuple :: (Generic y, Code y ~ '[ xs ]) => NP I xs -> y
  toTuple = to . SOP . Z

instance IsTupleTypeOf '[] ()
instance IsTupleTypeOf '[x1] x1 where toTuple = unI . hd
instance IsTupleTypeOf '[x1, x2] (x1, x2)
instance IsTupleTypeOf '[x1, x2, x3] (x1, x2, x3)
instance IsTupleTypeOf '[x1, x2, x3, x4] (x1, x2, x3, x4)

convert :: (AllZip IsTupleTypeOf xss ys) => NS (NP I) xss -> NS I ys
convert = htrans (Proxy :: Proxy IsTupleTypeOf) (I . toTuple)

convertFull :: (Generic a, AllZip IsTupleTypeOf (Code a) ys) => a -> NS I ys
convertFull = convert . unSOP . from