Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1.  
  2. import Data.Type.Index
  3. data Sum :: [Type] -> Type where
  4. L :: x -> Sum (x : xs)
  5. R :: Sum xs -> Sum (x : xs)
  6.  
  7.  
  8. inj :: (Elem xs x) => x -> Sum xs
  9. inj = inj' elemIndex
  10.  
  11.  
  12. inj' :: Index xs x -> x -> Sum xs
  13. inj' = \case
  14. IZ -> L
  15. IS x -> R . inj' x
  16.  
  17. class Proj x xs where
  18. proj :: Proxy x -> Sum xs -> Maybe x
  19.  
  20. instance Proj x (x:xs) where
  21. proj _ (L x) = Just x
  22. proj _ _ = Nothing
  23.  
  24. instance Proj x ys => Proj x (y:ys) where
  25. proj p (R ys) = proj p ys
  26.  
  27. data FList f xs where
  28. FNil :: FList f '[]
  29. (:<) :: Typeable x => f x -> FList f xs -> FList f (x : xs)
  30.  
  31. newtype Rev b a = Rev (a -> b)
  32.  
  33. elim :: FList (Rev a) xs -> Sum xs -> a
  34. elim (Rev f :< fs) (L x) = f x
  35. elim (_ :< fs) (R xs) = elim fs xs
  36.  
  37. infixr 5 :<
  38.  
  39. test, test1 :: Sum [Int, Double, Maybe String]
  40. test = inj (3 :: Int)
  41. test1 = inj (Just "fooobar")
  42.  
  43. f = (Rev show :< Rev show :< Rev show :< FNil)
  44. res = (elim f test1, elim f test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement