|
@@ -1,11 +1,17 @@
|
|
|
{-# LANGUAGE PatternSynonyms #-}
|
|
|
+{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
|
module Data.SCargot.Repr.Basic
|
|
|
(
|
|
|
R.SExpr(..)
|
|
|
+
|
|
|
+ , cons
|
|
|
+ , uncons
|
|
|
|
|
|
, pattern (:::)
|
|
|
, pattern A
|
|
|
+ , pattern L
|
|
|
+ , pattern DL
|
|
|
, pattern Nil
|
|
|
|
|
|
, _car
|
|
@@ -48,17 +54,73 @@ _cdr f (x ::: xs) = (:::) <$> pure x <*> f xs
|
|
|
_cdr _ (A a) = pure (A a)
|
|
|
_cdr _ Nil = pure Nil
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+uncons :: SExpr a -> Maybe (SExpr a, SExpr a)
|
|
|
+uncons (SCons x xs) = Just (x, xs)
|
|
|
+uncons _ = Nothing
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+cons :: SExpr a -> SExpr a -> SExpr a
|
|
|
+cons = SCons
|
|
|
+
|
|
|
+mkList :: [SExpr a] -> SExpr a
|
|
|
+mkList [] = SNil
|
|
|
+mkList (x:xs) = SCons x (mkList xs)
|
|
|
+
|
|
|
+mkDList :: [SExpr a] -> a -> SExpr a
|
|
|
+mkDList [] a = SAtom a
|
|
|
+mkDList (x:xs) a = SCons x (mkDList xs a)
|
|
|
+
|
|
|
+gatherDList :: SExpr a -> Maybe ([SExpr a], a)
|
|
|
+gatherDList SNil = Nothing
|
|
|
+gatherDList SAtom {} = Nothing
|
|
|
+gatherDList sx = go sx
|
|
|
+ where go SNil = Nothing
|
|
|
+ go (SAtom a) = return ([], a)
|
|
|
+ go (SCons x xs) = do
|
|
|
+ (ys, a) <- go xs
|
|
|
+ return (x:ys, a)
|
|
|
+
|
|
|
infixr 5 :::
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
pattern x ::: xs = SCons x xs
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
pattern A x = SAtom x
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
pattern Nil = SNil
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+pattern L xs <- (gatherList -> Right xs)
|
|
|
+ where L xs = mkList xs
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+pattern DL xs x <- (gatherDList -> Just (xs, x))
|
|
|
+ where DL xs x = mkDList xs x
|
|
|
+
|
|
|
getShape :: SExpr a -> String
|
|
|
getShape Nil = "empty list"
|
|
|
getShape sx = go 0 sx
|