fork(1) download
  1. {-# LANGUAGE KindSignatures, GADTs, TypeFamilies, TypeOperators, MultiParamTypeClasses, DataKinds #-}
  2.  
  3. import Control.Applicative
  4. import Data.Traversable (Traversable)
  5.  
  6. class (Applicative f, Traversable f) => Dim f
  7.  
  8. data Shapely (fs :: [* -> *]) where
  9. ShZ :: Shapely '[]
  10. ShS :: (Dim f, SShapely fs) => Shapely (f ': fs)
  11.  
  12. class SShapely (fs :: [* -> *]) where
  13. shapely :: Shapely fs
  14.  
  15. instance SShapely '[] where
  16. shapely = ShZ
  17.  
  18. instance (Dim f, SShapely fs) => SShapely (f ': fs) where
  19. shapely = ShS
  20.  
  21. -------------------------------------
  22. -- Hypercuboid datatype
  23. -------------------------------------
  24. data Hyper :: [* -> *] -> * -> * where
  25. Scalar :: a -> Hyper '[] a
  26. Prism :: (Dim f, SShapely fs) =>
  27. Hyper fs (f a) -> Hyper (f ': fs) a
  28.  
  29. instance Functor (Hyper fs) where
  30. fmap f (Scalar a) = Scalar $ f a
  31. fmap f (Prism p) = Prism $ fmap (fmap f) p
  32.  
  33. pureSh :: Shapely fs -> a -> Hyper fs a
  34. pureSh ShZ x = Scalar x
  35. pureSh ShS x = Prism (pureSh shapely (pure x))
  36.  
  37. instance SShapely fs => Applicative (Hyper fs) where
  38. pure = pureSh shapely
  39.  
  40. main = print 42
Success #stdin #stdout 0s 4712KB
stdin
Standard input is empty
stdout
42