私はを使って、HaskellのAPLに似たプログラミングを行っています。私は、それが完全に実行可能であると述べているが、hypercuboidデータ型のためにApplicative
インスタンスを定義することに固執した。簡略化された例はas followsです。hypercuboidの応用例
{-# LANGUAGE KindSignatures, GADTs, TypeFamilies, TypeOperators, MultiParamTypeClasses, DataKinds #-}
import Control.Applicative
import Data.Traversable (Traversable)
class (Applicative f, Traversable f) => Dim f
class Shapely (fs :: [* -> *])
instance Shapely '[]
instance (Dim f, Shapely fs) => Shapely (f ': fs)
-------------------------------------
-- Hypercuboid datatype
-------------------------------------
data Hyper :: [* -> *] -> * -> * where
Scalar :: a -> Hyper '[] a
Prism :: (Dim f, Shapely fs) =>
Hyper fs (f a) -> Hyper (f ': fs) a
instance Functor (Hyper fs) where
fmap f (Scalar a) = Scalar $ f a
fmap f (Prism p) = Prism $ fmap (fmap f) p
instance Applicative (Hyper fs) where
pure a = undefined
{- `pure a = Scalar a` gives:
Couldn't match type ‘fs’ with ‘'[]’
‘fs’ is a rigid type variable bound by
the instance declaration
Expected type: Hyper fs a
Actual type: Hyper '[] a
-}
(例えば、リストの長さタイプインデックス)Vector n a
タイプ約Applicative
インスタンスが知られているものを想起Iは約pure
replicate
として(与えられた値のn進複製)考えてみました。しかし、これは、私が知る限り、ハスケルでは使用できないタイプレベルのケース(例:pure a = case fs of '[] -> Scalar a; (f ': gs) -> <something using the fact that Applicative f>
)が必要なようです。