Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# OPTIONS_GHC -Wall #-}
- {-# LANGUAGE FlexibleInstances #-}
- {-# LANGUAGE GADTs #-}
- {-# LANGUAGE RoleAnnotations #-}
- module List (List, fromText, list, unList) where
- import qualified Data.Text as T
- type role List nominal
- data List a where
- Nil :: List a
- Cons :: a -> List a -> List a
- IsText :: T.Text -> List Char
- instance Functor List where
- fmap _ Nil = Nil
- fmap f (Cons x xs) = Cons (f x) (fmap f xs)
- fmap f (IsText text) = foldr Cons Nil $ fmap f (T.unpack text)
- list :: [a] -> List a
- list = foldr Cons Nil
- fromText :: T.Text -> List Char
- fromText = IsText
- unList :: List a -> [a]
- unList Nil = []
- unList (Cons x xs) = x : unList xs
- unList (IsText text) = T.unpack text
- -- This whole facade comes crashing down unless you hide the constructors.
- -- I.e. if you don't hide the constructors, it is very easy to write a
- -- "function" such that `xs == ys` but `f xs /= f ys`.
- -- To prevent this, you have to prevent people from pattern matching the
- -- constructors and force access through `unList`.
- instance Eq a => Eq (List a) where
- xs == ys = unList xs == unList ys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement