Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language
  2.  DataKinds,
  3.  TypeOperators,
  4.  PolyKinds,
  5.  TypeFamilies,
  6.  UndecidableInstances,
  7.  GADTs,
  8.  RankNTypes
  9. #-}
  10.  
  11. data Nat = Z | S Nat
  12.            deriving (Show, Eq, Ord)
  13.  
  14. data SNat (n :: Nat) where
  15.   SZ :: SNat Z
  16.   SS :: SNat n -> SNat (S n)
  17.  
  18. --
  19.  
  20. type family Head (xs :: [a]) :: a where
  21.  Head (x ': xs) = x
  22.  
  23. type family Drop (n :: Nat) (xs :: [a]) :: [a] where
  24. Drop (S n) (x ': xs) = Drop n xs
  25.  Drop Z xs = xs
  26.  
  27. type family (!!) (xs :: [a]) (n :: Nat) :: a where
  28.  (!!) xs n = Head (Drop n xs)
  29.  
  30. --
  31.  
  32. data StateN (xs :: [* -> * -> *]) s a where
  33.   StateN :: xs !! n ~ x => (s -> (SNat n,x s a)) -> StateN xs s a
  34.  
  35. newtype StackS   s a = StackS  {runStackS   :: Maybe (a,s) }
  36. newtype LinearS  s a = LinearS {runLinearS  :: (a,Maybe s) }
  37.  
  38. eg :: (Int,Int) -> StateN '[LinearS,StackS] (Int,Int) Int
  39. eg (nT,mT) = StateN (\(i,j) -> case (i,j) of
  40.              (0,_) -> (SS SZ,StackS Nothing)
  41.              (n,1) -> (SZ,LinearS (n+1,Nothing))
  42.              (n,m) -> (SZ,LinearS (n+m,Just (n,m-1)))
  43.            )
  44. -- this gives type errors as the different cases return different types.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement