Je travaille sur extended abstract by Prof. J.Gibbons sur la programmation de type APL dans Haskell. Je me suis contenté de définir Applicative
instance pour le type de données hypercuboid, bien que le document indique que c'est parfaitement faisable. L'exemple simplifié est as follows.Instance d'application pour l'hypercubide
{-# 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
-}
Rappelant ce qui est connu Applicative
par exemple pour le type Vector n a
(par exemple une indexation de type liste par sa longueur) J'ai essayé de penser à pure
comme replicate
(réplication n-aire d'une valeur donnée). Mais il semble que cela nécessite un cas typique (par exemple pure a = case fs of '[] -> Scalar a; (f ': gs) -> <something using the fact that Applicative f>
) qui n'est pas disponible chez Haskell autant que je sache.