Skip to content

A peer-to-peer stack for code collaboration

Add vectors with clojure-like syntax. (#122)

James Haydon2c2786bbde39
diff --git a/src/Radicle.hs b/src/Radicle.hs
index acd9a44..6942982 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -29,6 +29,7 @@ module Radicle
, pattern Number
, pattern Boolean
, pattern List
+ , pattern Vec
, pattern Primop
, pattern Dict
, pattern Ref
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 4e87f90..cd69f46 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -21,6 +21,7 @@ import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
import Data.Scientific (Scientific, floatingOrInteger)
import Data.Semigroup ((<>))
+import qualified Data.Sequence as Seq
import Generics.Eot
import qualified GHC.Exts as GhcExts
import qualified Text.Megaparsec.Error as Par
@@ -125,6 +126,7 @@ data ValueF r =
| NumberF Scientific
| BooleanF Bool
| ListF [r]
+ | VecF (Seq.Seq r)
| PrimopF Ident
-- | Map from *pure* Values -- annotations shouldn't change lookup semantics.
| DictF (Map.Map Value r)
@@ -170,6 +172,11 @@ pattern List vs <- (Ann.match -> ListF vs)
where
List = Ann.annotate . ListF
+pattern Vec :: ValueConC t => Seq.Seq (Annotated t ValueF) -> Annotated t ValueF
+pattern Vec vs <- (Ann.match -> VecF vs)
+ where
+ Vec = Ann.annotate . VecF
+
pattern Primop :: ValueConC t => Ident -> Annotated t ValueF
pattern Primop i <- (Ann.match -> PrimopF i)
where
@@ -394,6 +401,7 @@ baseEval val = logValPos val $ case val of
$ WrongNumberOfArgs ("application: " <> show xs)
2
(length xs)
+ Vec xs -> Vec <$> traverse baseEval xs
Dict mp -> do
let evalBoth (a,b) = (,) <$> baseEval a <*> baseEval b
Dict . Map.fromList <$> traverse evalBoth (Map.toList mp)
@@ -406,6 +414,8 @@ class FromRad a where
default fromRad :: (HasEot a, FromRadG (Eot a)) => Value -> Either Text a
fromRad = fromRadG
+instance FromRad Value where
+ fromRad = pure
instance FromRad Scientific where
fromRad x = case x of
Number n -> pure n
@@ -423,6 +433,7 @@ instance FromRad Text where
instance FromRad a => FromRad [a] where
fromRad x = case x of
List xs -> traverse fromRad xs
+ Vec xs -> traverse fromRad (toList xs)
_ -> Left "Expecting list"
instance FromRad (Env Value) where
fromRad x = case x of
@@ -522,8 +533,11 @@ kwLookup key = Map.lookup (Keyword $ Ident key)
a ?? n = n `note` a
hoistEither :: MonadError e m => Either e a -> m a
-hoistEither (Left e) = throwError e
-hoistEither (Right x) = pure x
+hoistEither = hoistEitherWith identity
+
+hoistEitherWith :: MonadError e' m => (e -> e') -> Either e a -> m a
+hoistEitherWith f (Left e) = throwError (f e)
+hoistEitherWith _ (Right x) = pure x
-- * Generic encoding/decoding of Radicle values.
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index b5ff5cd..6c90e76 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -5,6 +5,7 @@ import Protolude hiding (SrcLoc, try)
import Data.Char (isAlphaNum, isLetter)
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.Map as Map
+import qualified Data.Sequence as Seq
import qualified Data.Text as T
import GHC.Exts (IsString(..))
import Text.Megaparsec
@@ -62,6 +63,9 @@ parensP = inside "(" ")"
bracesP :: Parser a -> Parser a
bracesP = inside "{" "}"
+sqBracketsP :: Parser a -> Parser a
+sqBracketsP = inside "[" "]"
+
stringLiteralP :: VParser
stringLiteralP = lexeme $
tag =<< StringF . toS <$> (char '"' >> manyTill L.charLiteral (char '"'))
@@ -98,6 +102,9 @@ keywordP = do
listP :: VParser
listP = parensP (tag =<< (ListF <$> valueP `sepBy` spaceConsumer))
+vecP :: VParser
+vecP = sqBracketsP (tag =<< (VecF . Seq.fromList <$> valueP `sepBy` spaceConsumer))
+
dictP :: VParser
dictP = bracesP (tag =<< (DictF . Map.fromList <$> evenItems))
where
@@ -124,6 +131,7 @@ valueP = do
, atomOrPrimP <?> "identifier"
, quoteP <?> "quote"
, listP <?> "list"
+ , vecP <?> "vector"
, dictP <?> "dict"
]
spaceConsumer
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index ae929b9..2927708 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -5,6 +5,7 @@ import Protolude hiding (TypeError, (<>))
import Data.Copointed (Copointed(..))
import qualified Data.Map as Map
+import Data.Sequence (Seq(..))
import qualified Data.Text as T
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Text
@@ -35,6 +36,10 @@ instance (Copointed t, Ann.Annotation t) => Pretty (Ann.Annotated t ValueF) wher
[] -> "()"
[v'] -> parens $ pretty v'
_ -> parens $ hang 1 (sep $ pretty <$> vs)
+ Vec vs -> case vs of
+ Empty -> "[]"
+ v' :<| Empty -> brackets $ pretty v'
+ _ -> brackets . sep $ pretty <$> toList vs
Primop i -> pretty i
Dict mp -> braces . align $
sep [ pretty k <+> pretty val
diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hs
index 812bcfb..4dc923e 100644
--- a/src/Radicle/Internal/Primops.hs
+++ b/src/Radicle/Internal/Primops.hs
@@ -129,9 +129,11 @@ purePrimops = Primops $ fromList $ first Ident <$>
_ -> throwErrorHere $ TypeError "tail: expects list argument")
, ( "nth"
, evalArgs $ \case
- [Number n, List xs] -> case floatingOrInteger n of
+ [Number n, vs] -> case floatingOrInteger n of
Left (_ :: Double) -> throwErrorHere $ OtherError "nth: first argument was not an integer"
- Right i -> case xs `atMay` i of
+ Right i -> do
+ xs <- hoistEitherWith (const (toLangError . OtherError $ "nth: first argument must be sequential")) $ fromRad vs
+ case xs `atMay` i of
Just x -> pure x
Nothing -> throwErrorHere $ OtherError "nth: index out of bounds"
[_,_] -> throwErrorHere $ TypeError "nth: expects a integer and a list"
@@ -256,8 +258,9 @@ purePrimops = Primops $ fromList $ first Ident <$>
, evalOneArg "seq" $
\case
x@(List _) -> pure x
+ x@(Vec _) -> pure x
Dict kvs -> pure $ List [List [k, v] | (k,v) <- Map.toList kvs ]
- _ -> throwErrorHere $ TypeError "seq: can only create a list from a list or a dict"
+ _ -> throwErrorHere $ TypeError "seq: can only be used on a list, vector or dictionary"
)
, ( "to-json"
, evalOneArg "to-json" $ \v -> String . toS . Aeson.encode <$>
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 89d517b..2500bf9 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -7,6 +7,7 @@ import Data.List (isInfixOf, isSuffixOf)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromJust)
import Data.Scientific (Scientific)
+import Data.Sequence (Seq(..))
import Data.String.Interpolate (i)
import Data.String.QQ (s)
import qualified Data.Text as T
@@ -39,6 +40,14 @@ test_eval =
|]
prog `succeedsWith` String "Steve Wozniak"
+ , testCase "vector syntax creates vectors" $ do
+ "[]" `succeedsWith` Vec Empty
+ "[1 2]" `succeedsWith` Vec (fromList [Number 1, Number 2])
+ "[1 :a [:b 2]]" `succeedsWith` Vec (fromList [Number 1, kw "a", Vec (fromList [kw "b", Number 2])])
+
+ , testCase "vectors evaluate their elements" $
+ "[(+ 1 2) (* 3 4)]" `succeedsWith` Vec (fromList [Number 3, Number 12])
+
, testCase "'dict' creates a Dict with given key/vals" $ do
let prog1 = [s|(dict 'why "not")|]
prog1 `succeedsWith` Dict (fromList [(Atom $ toIdent "why", String "not")])
@@ -61,6 +70,12 @@ test_eval =
let prog = [s|(tail (list #t #f #t))|]
prog `succeedsWith` List [Boolean False, Boolean True]
+ , testCase "'nth' extracts elements from lists and vectors" $ do
+ "(nth 0 [0 1 2])" `succeedsWith` Number 0
+ "(nth 1 [0 1 2])" `succeedsWith` Number 1
+ "(nth 0 '(0 1 2))" `succeedsWith` Number 0
+ "(nth 1 '(0 1 2))" `succeedsWith` Number 1
+
, testProperty "'eq?' considers equal values equal" $ \(val :: Value) -> do
let prog = [i|(eq? #{renderPrettyDef val} #{renderPrettyDef val})|]
res = runTest' $ toS prog