HaskellDBに関する最近の投稿で、私はHListをもう一度調べるようになりました。異種リストの例が実際にあるGHCにあるよう-XDataKinds
に、HListがDataKindsでどのように見えるかを調査したいと思いました。これまでのところ、私は次のものを持っています:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import Data.Tagged
data Record :: [*] -> * where
RNil :: Record '[]
(:*:) :: Tagged f (FieldV f) -> Record t -> Record (f ': t)
type family FieldV a :: *
emptyRecord = RNil
(=:) :: (v ~ FieldV f) => f -> v -> Tagged f v
f =: v = Tagged v
class HasField x xs where
(=?) :: Record xs -> x -> FieldV x
instance HasField x (x ': xs) where
(Tagged v :*: _) =? _ = v
instance HasField x xs => HasField x (a ': xs) where
(_ :*: r) =? f = r =? f
--------------------------------------------------------------------------------
data EmployeeName = EmployeeName
type instance FieldV EmployeeName = String
data EmployeeID = EmployeeID
type instance FieldV EmployeeID = Int
employee = (EmployeeName =: "James")
:*: ((EmployeeID =: 5) :*: RNil)
employeeName = employee =? EmployeeName
employeeId = employee =? EmployeeID
これは期待どおりに機能しますが、このプロジェクトでの私の目標は、可能な限り型クラスなしでそれを実行することでした。したがって、ここには2つの質問があります。(=?)
まず、型クラスなしで(レコードフィールドアクセサ関数)を書くことは可能ですか?そうでない場合は、インスタンスを重複させずに書き込むことができますか?
私の最初の質問では不可能だと思いますが、2番目の質問は可能かもしれません。人々の意見を聞きたいです!