123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- {-# LANGUAGE PatternSynonyms #-}
- {-# LANGUAGE ViewPatterns #-}
- module Data.SCargot.Repr.WellFormed
- (
- R.WellFormedSExpr(..)
- , R.toWellFormed
- , R.fromWellFormed
-
- , cons
- , uncons
-
- , pattern (:::)
- , pattern L
- , pattern A
- , pattern Nil
-
- , fromPair
- , fromList
- , fromAtom
- , asPair
- , asList
- , isAtom
- , asAtom
- , asAssoc
- , car
- , cdr
- ) where
- import Control.Applicative ((<$>), (<*>), pure)
- import Data.SCargot.Repr as R
- uncons :: WellFormedSExpr a -> Maybe (WellFormedSExpr a, WellFormedSExpr a)
- uncons R.WFSAtom {} = Nothing
- uncons (R.WFSList (x:xs)) = Just (x, R.WFSList xs)
- cons :: WellFormedSExpr a -> WellFormedSExpr a -> Maybe (WellFormedSExpr a)
- cons _ (R.WFSAtom {}) = Nothing
- cons x (R.WFSList xs) = Just (R.WFSList (x:xs))
- pattern x ::: xs <- (uncons -> Just (x, xs))
- pattern L xs = R.WFSList xs
- pattern A a = R.WFSAtom a
- pattern Nil = R.WFSList []
- getShape :: WellFormedSExpr a -> String
- getShape A {} = "atom"
- getShape Nil = "empty list"
- getShape (L sx) = "list of length " ++ show (length sx)
- fromPair :: (WellFormedSExpr t -> Either String a)
- -> (WellFormedSExpr t -> Either String b)
- -> WellFormedSExpr t -> Either String (a, b)
- fromPair pl pr (L [l, r]) = (,) <$> pl l <*> pr r
- fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
- fromList :: (WellFormedSExpr t -> Either String a)
- -> WellFormedSExpr t -> Either String [a]
- fromList p (L ss) = mapM p ss
- fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
- fromAtom :: WellFormedSExpr t -> Either String t
- fromAtom (A a) = return a
- fromAtom sx = Left ("fromAtom: expected atom; found " ++ getShape sx)
- asPair :: ((WellFormedSExpr t, WellFormedSExpr t) -> Either String a)
- -> WellFormedSExpr t -> Either String a
- asPair f (L [l, r]) = f (l, r)
- asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
- asList :: ([WellFormedSExpr t] -> Either String a)
- -> WellFormedSExpr t -> Either String a
- asList f (L ls) = f ls
- asList _ sx = Left ("asList: expected list; found " ++ getShape sx)
- isAtom :: Eq t => t -> WellFormedSExpr t -> Either String ()
- isAtom s (A s')
- | s == s' = return ()
- | otherwise = Left "isAtom: failed to match atom"
- isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
- isNil :: WellFormedSExpr t -> Either String ()
- isNil Nil = return ()
- isNil sx = Left ("isNil: expected nil; found " ++ getShape sx)
- asAtom :: (t -> Either String a) -> WellFormedSExpr t -> Either String a
- asAtom f (A s) = f s
- asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
- asAssoc :: ([(WellFormedSExpr t, WellFormedSExpr t)] -> Either String a)
- -> WellFormedSExpr t -> Either String a
- asAssoc f (L ss) = gatherPairs ss >>= f
- where gatherPairs (L [a, b] : ss) = (:) <$> pure (a, b) <*> gatherPairs ss
- gatherPairs [] = pure []
- gatherPairs (sx:_) = Left ("asAssoc: expected pair; found " ++ getShape sx)
- asAssoc _ sx = Left ("asAssoc: expected list; found " ++ getShape sx)
- car :: (WellFormedSExpr t -> Either String t')
- -> [WellFormedSExpr t] -> Either String t'
- car f (x:_) = f x
- car _ [] = Left "car: Taking car of zero-element list"
- cdr :: ([WellFormedSExpr t] -> Either String t')
- -> [WellFormedSExpr t] -> Either String t'
- cdr f (_:xs) = f xs
- cdr _ [] = Left "cdr: Taking cdr of zero-element list"
|