{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Generic.HKD.Build
( Build (..)
) where
import Data.Kind (Type)
import Data.GenericLens.Internal (HList (..))
import Data.Generic.HKD.Types (HKD (..), GHKD_)
import GHC.Generics
import Prelude hiding (uncurry)
class Fill (f :: Type -> Type) (structure :: Type) (types :: [Type])
| structure f -> types, types -> f where
fill :: HList types -> HKD structure f
class GFill (f :: Type -> Type) (xs :: [Type]) (ys :: [Type]) (rep :: Type -> Type)
| xs rep -> ys, ys f rep -> xs, xs -> f where
gfill :: HList xs -> (HList ys, GHKD_ f rep p)
instance GFill f xs ys inner
=> GFill f xs ys (M1 index meta inner) where
gfill = fmap M1 . gfill @f
instance (GFill f xs ys left, GFill f ys zs right)
=> GFill f xs zs (left :*: right) where
gfill xs = do
let (ys, left) = gfill @f xs
(zs, right) = gfill @f ys
(zs, left :*: right)
instance GFill f (f x ': xs) xs (Rec0 x) where
gfill (x :> xs) = (xs, K1 x)
instance (Generic shape, GFill f with '[] (Rep shape))
=> Fill f shape with where
fill = HKD . snd . gfill @f @_ @'[]
class Build (structure :: Type) (f :: Type -> Type) (k :: Type)
| f structure -> k where
build :: k
class GBuild (f :: Type -> Type) (structure :: Type) (xs :: [Type]) (k :: Type)
| f structure xs -> k where
gbuild :: (HList xs -> HKD structure f) -> k
instance GBuild f structure xs k
=> GBuild f structure (x ': xs) (x -> k) where
gbuild k x = gbuild @_ @_ @xs \xs -> k (x :> xs)
instance GBuild f structure '[] (HKD structure f) where
gbuild k = k Nil
instance (Fill f structure xs, GBuild f structure xs k)
=> Build structure f k where
build = gbuild @f @structure @xs fill