A peer-to-peer stack for code collaboration
Added the `show` primop. (#44)
James Haydon6bb3c439273a
diff --git a/package.yaml b/package.yamlindex d9c62c6..a66ee5b 100644--- a/package.yaml+++ b/package.yaml@@ -70,7 +70,7 @@ executables:dependencies:- radicle- optparse-applicative- ghc-options: -Wall -threaded+ ghc-options: -Wall -threaded -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraintsdefault-extensions:- AutoDeriveTypeable@@ -91,3 +91,4 @@ default-extensions:- TupleSections- TypeFamilies- TypeOperators+- ScopedTypeVariablesdiff --git a/src/Radicle.hs b/src/Radicle.hsindex bbc4715..5cd74be 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -54,4 +54,3 @@ module Radicleimport Control.Monad.Identity (runIdentity)import Data.Text.Prettyprint.Docimport Radicle.Internal-diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hsindex 3eeefef..4dd22d4 100644--- a/src/Radicle/Internal.hs+++ b/src/Radicle/Internal.hs@@ -6,6 +6,7 @@ module Radicle.Internalimport Radicle.Internal.Core as Ximport Radicle.Internal.Parse as Ximport Radicle.Internal.Pretty as X+import Radicle.Internal.Primops as Ximport Radicle.Internal.Subscriber as Ximport Control.Monad.Reader@@ -15,5 +16,5 @@ import qualified Text.Megaparsec as M-- | Smart constructor for Ident.mkIdent :: Text -> Maybe IdentmkIdent t = case runReader (M.runParserT identP "" t) [] of- Left _ -> Nothing+ Left _ -> NothingRight v -> pure vdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 58d583c..c32d5ce 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -1,32 +1,28 @@-{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TemplateHaskell #-}+module Radicle.Internal.Core where-import Control.Monad.Except (ExceptT(..), MonadError, catchError,- runExceptT, throwError)+import Control.Monad.Except (ExceptT(..), MonadError, runExceptT,+ throwError)import Control.Monad.State-import Data.Bifunctor (first)import Data.Data (Data)import Data.Deriving (deriveEq1, deriveShow1)-import Data.Foldable (foldlM, foldrM)import Data.Functor.Foldable (Fix(..), cata)+import Data.IntMap (IntMap)+import qualified Data.IntMap as IntMapimport Data.List.NonEmpty (NonEmpty)import qualified Data.List.NonEmpty as NonEmptyimport Data.Map (Map)-import Data.IntMap (IntMap)import qualified Data.Map as Map-import qualified Data.IntMap as IntMap-import Data.Maybe (catMaybes, isJust)import Data.Scientific (Scientific)import Data.Semigroup (Semigroup, (<>))import Data.Text (Text)+import qualified Data.Text as Timport Data.Void (Void)import GHC.Exts (IsList(..), fromString)import GHC.Generics (Generic)import qualified Text.Megaparsec.Error as Parimport Unsafe.Coerce (unsafeCoerce)-import qualified Data.Text as T--- * Value@@ -120,8 +116,7 @@ data Value r =-- | Takes the arguments/parameters, a body, and possibly a closure.---- The value of an application of a lambda is always the last value in the- -- body. The only reason to have multiple values is thus only for (local)- -- "define"s.+ -- body. The only reason to have multiple values is for effects.| Lambda [Ident] (NonEmpty (Value r)) (Maybe (Env (Value r)))deriving (Eq, Show, Read, Generic, Functor, Foldable, Traversable)@@ -180,7 +175,13 @@ data Bindings m = Bindings} deriving (Generic)instance Semigroup (Bindings m) where- (<>) = mappend+ a <> b = Bindings+ { bindingsEnv = bindingsEnv a <> bindingsEnv b+ , bindingsPrimops = bindingsPrimops a <> bindingsPrimops b+ , bindingsRefs = bindingsRefs a+ <> IntMap.mapKeys (+ bindingsNextRef a) (bindingsRefs b)+ , bindingsNextRef = bindingsNextRef a + bindingsNextRef b+ }instance Monoid (Bindings m) wheremempty = Bindings@@ -189,13 +190,7 @@ instance Monoid (Bindings m) where, bindingsRefs = mempty, bindingsNextRef = 0}- a `mappend` b = Bindings- { bindingsEnv = bindingsEnv a <> bindingsEnv b- , bindingsPrimops = bindingsPrimops a <> bindingsPrimops b- , bindingsRefs = bindingsRefs a- <> IntMap.mapKeys (+ bindingsNextRef a) (bindingsRefs b)- , bindingsNextRef = bindingsNextRef a + bindingsNextRef b- }+ mappend = (<>)-- | The environment in which expressions are evaluated.newtype LangT r m a = LangT@@ -209,8 +204,7 @@ instance MonadTrans (LangT r) where lift = LangT . lift . lifttype Lang m = LangT (Bindings m) mrunLang- :: Monad m- => Bindings m+ :: Bindings m-> Lang m a-> m (Either (LangError (Value Reference)) a, Bindings m)runLang e l = runStateT (runExceptT $ fromLangT l) e@@ -226,13 +220,7 @@ withEnv modifier action = do-- * Functions--- | A Bindings with an Env containing only 'eval' and only pure primops.-pureEnv :: (Monad m) => Bindings m-pureEnv = Bindings e purePrimops mempty 0- where- e = fromList [(toIdent "eval", Primop $ toIdent "base-eval")]--addBinding :: Monad m => Ident -> Value Reference -> Bindings m -> Bindings m+addBinding :: Ident -> Value Reference -> Bindings m -> Bindings maddBinding i v b = b{ bindingsEnv = Env . Map.insert i v . fromEnv $ bindingsEnv b }@@ -254,171 +242,6 @@ defineAtom i v = modify $ addBinding i vquote :: Value r -> Value rquote v = List [Primop (Ident "quote"), v]--- | The universal primops. These are available in chain evaluation, and are--- not shadowable via 'define'.-purePrimops :: forall m. (Monad m) => Primops m-purePrimops = Map.fromList $ first Ident <$>- [ ("base-eval", evalArgs $ \args -> case args of- [x] -> baseEval x- xs -> throwError $ WrongNumberOfArgs "base-eval" 1 (length xs))- , ("list", evalArgs $ \args -> pure $ List args)- , ("quote", \args -> case args of- [v] -> pure v- xs -> throwError $ WrongNumberOfArgs "quote" 1 (length xs))- , ("define", \args -> case args of- [Atom name, val] -> do- val' <- baseEval val- defineAtom name val'- pure nil- [_, _] -> throwError $ OtherError "define expects atom for first arg"- xs -> throwError $ WrongNumberOfArgs "define" 2 (length xs))- , ("catch", \args -> case args of- [l, form, handler] -> do- mlabel <- baseEval l- case mlabel of- Atom label -> baseEval form `catchError` \e -> do- (thrownLabel, thrownValue) <- errorToValue e- if thrownLabel == label || label == Ident "any"- then handler $$ [thrownValue]- else baseEval form- _ -> throwError $ TypeError "catch: first argument must be atom"- xs -> throwError $ WrongNumberOfArgs "catch" 3 (length xs))- , ("throw", evalArgs $ \args -> case args of- [Atom label, exc] -> throwError $ ThrownError label exc- [_, _] -> throwError $ TypeError "throw: first argument must be atom"- xs -> throwError $ WrongNumberOfArgs "throw" 2 (length xs))- , ("eq?", evalArgs $ \args -> case args of- [a, b] -> pure $ Boolean (a == b)- xs -> throwError $ WrongNumberOfArgs "eq?" 2 (length xs))- , ("cons", evalArgs $ \args -> case args of- [x, List xs] -> pure $ List (x:xs)- [_, _] -> throwError $ TypeError "cons: second argument must be list"- xs -> throwError $ WrongNumberOfArgs "cons" 2 (length xs))- , ("head", evalArgs $ \args -> case args of- [List (x:_)] -> pure x- [List []] -> throwError $ OtherError "head: empty list"- [_] -> throwError $ TypeError "head: expects list argument"- xs -> throwError $ WrongNumberOfArgs "head" 1 (length xs))- , ("tail", evalArgs $ \args -> case args of- [List (_:xs)] -> pure $ List xs- [List []] -> throwError $ OtherError "tail: empty list"- [_] -> throwError $ TypeError "tail: expects list argument"- xs -> throwError $ WrongNumberOfArgs "tail" 1 (length xs))- , ("lookup", evalArgs $ \args -> case args of- [Atom a, Dict m] -> pure $ case Map.lookup a m of- Just v -> v- -- Probably an exception is better, but that seems cruel- -- when you have no exception handling facilities.- Nothing -> nil- [Atom _, _] -> throwError- $ TypeError "lookup: second argument must be map"- [_, Dict _] -> throwError- $ TypeError "lookup: first argument must be atom"- xs -> throwError $ WrongNumberOfArgs "lookup" 2 (length xs))- , ("string-append", evalArgs $ \args ->- let fromStr (String s) = Just s- fromStr _ = Nothing- ss = fromStr <$> args- in if all isJust ss- then pure . String . mconcat $ catMaybes ss- else throwError $ TypeError "string-append: non-string argument")- , ("insert", evalArgs $ \args -> case args of- [Atom k, v, Dict m] -> pure . Dict $ Map.insert k v m- [Atom _, _, _] -> throwError- $ TypeError "insert: third argument must be map"- [_, _, _] -> throwError- $ TypeError "insert: first argument must be an atom"- xs -> throwError $ WrongNumberOfArgs "insert" 3 (length xs))- -- The semantics of + and - in Scheme is a little messed up. (+ 3)- -- evaluates to 3, and of (- 3) to -3. That's pretty intuitive.- -- But while (+ 3 2 1) evaluates to 6, (- 3 2 1) evaluates to 0. So with -- -- it is *not* correct to say that it's a foldl (-) 0. Instead, it- -- special-cases on one-argument application. (Similarly with * and /.)- --- -- In order to avoid this sort of thing, we don't allow +,*,- and / to be- -- applied to a single argument.- , numBinop (+) "+"- , numBinop (*) "*"- , numBinop (-) "-"- , ("<", evalArgs $ \args -> case args of- [Number x, Number y] -> pure $ Boolean (x < y)- [_, _] -> throwError $ TypeError "<: expecting number"- xs -> throwError $ WrongNumberOfArgs "<" 2 (length xs))- , (">", evalArgs $ \args -> case args of- [Number x, Number y] -> pure $ Boolean (x > y)- [_, _] -> throwError $ TypeError ">: expecting number"- xs -> throwError $ WrongNumberOfArgs ">" 2 (length xs))- , ("foldl", evalArgs $ \args -> case args of- [fn, init', List ls] -> foldlM (\b a -> (fn $$) [b,a]) init' ls- [_, _, _] -> throwError- $ TypeError "foldl: third argument should be a list"- xs -> throwError $ WrongNumberOfArgs "foldl" 3 (length xs))- , ("foldr", evalArgs $ \args -> case args of- [fn, init', List ls] -> foldrM (\b a -> (fn $$) [b,a]) init' ls- [_, _, _] -> throwError- $ TypeError "foldr: third argument should be a list"- xs -> throwError $ WrongNumberOfArgs "foldr" 3 (length xs))- , ("map", evalArgs $ \args -> case args of- [fn, List ls] -> List <$> traverse (fn $$) (pure <$> ls)- [_, _] -> throwError $ TypeError "map: second argument should be a list"- xs -> throwError $ WrongNumberOfArgs "map" 3 (length xs))- , ("string?", evalArgs $ \args -> case args of- [String _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "string?" 1 (length xs))- , ("boolean?", evalArgs $ \args -> case args of- [Boolean _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "boolean?" 1 (length xs))- , ("number?", evalArgs $ \args -> case args of- [Number _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "number?" 1 (length xs))- , ("member?", evalArgs $ \args -> case args of- [x, List xs] -> pure . Boolean $ elem x xs- [_, _] -> throwError- $ TypeError "member?: second argument must be list"- xs -> throwError $ WrongNumberOfArgs "eq?" 2 (length xs))- , ("if", \args -> case args of- [cond, t, f] -> do- b <- baseEval cond- -- I hate this as much as everyone that might ever read Haskell, but- -- in Lisps a lot of things that one might object to are True...- if b == Boolean False then baseEval f else baseEval t- xs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))- , ("read-ref", evalArgs $ \args -> case args of- [Ref (Reference x)] -> gets bindingsRefs >>= \m -> case IntMap.lookup x m of- Nothing -> throwError $ Impossible "undefined reference"- Just v -> pure v- [_] -> throwError $ TypeError "read-ref: argument must be a ref"- xs -> throwError $ WrongNumberOfArgs "read-ref" 1 (length xs))- , ("write-ref", evalArgs $ \args -> case args of- [Ref (Reference x), v] -> do- st <- get- put $ st { bindingsRefs = IntMap.insert x v $ bindingsRefs st }- pure nil- [_, _] -> throwError- $ TypeError "write-ref: first argument must be a ref"- xs -> throwError- $ WrongNumberOfArgs "write-ref" 2 (length xs))- ]- where- -- Many primops evaluate their arguments just as normal functions do.- evalArgs f args = traverse baseEval args >>= f-- numBinop :: (Scientific -> Scientific -> Scientific)- -> Text- -> (Text, [Value Reference] -> Lang m (Value Reference))- numBinop fn name = (name, evalArgs $ \args -> case args of- Number x:x':xs -> foldM go (Number x) (x':xs)- where- go (Number a) (Number b) = pure . Number $ fn a b- go _ _ = throwError . TypeError- $ name <> ": expecting number"- [Number _] -> throwError- $ OtherError $ name <> ": expects at least 2 arguments"- _ -> throwError $ TypeError $ name <> ": expecting number")--- * Eval-- | The buck-passing eval. Uses whatever 'eval' is in scope.diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 5acaf8b..4fb933b 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -1,4 +1,3 @@-{-# LANGUAGE ScopedTypeVariables #-}module Radicle.Internal.Parse whereimport Control.Applicative (many, (<|>))@@ -26,6 +25,7 @@ import qualified Text.Megaparsec.Char.Lexer as Limport qualified Text.Megaparsec.Error as Parimport Radicle.Internal.Core+import Radicle.Internal.Primops-- * The parserdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex a6400cd..9f89e75 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -20,7 +20,6 @@ instance Pretty Reference whereinstance Pretty Ident wherepretty (Ident i) = pretty i-instance Pretty r => Pretty (Value r) wherepretty v = case v ofAtom i -> pretty i@@ -29,7 +28,7 @@ instance Pretty r => Pretty (Value r) whereNumber n -> pretty $ show nBoolean True -> "#t"Boolean False -> "#f"- List vs -> parens $ sep (pretty <$> vs)+ List vs -> parens (sep (pretty <$> vs))Primop i -> pretty iDict mp -> parens $"dict" <+> sep [ pretty k <+> pretty valdiff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsnew file mode 100644index 0000000..573e23a--- /dev/null+++ b/src/Radicle/Internal/Primops.hs@@ -0,0 +1,195 @@+module Radicle.Internal.Primops+ ( pureEnv+ , purePrimops+ ) where++import Control.Monad.Except (catchError, throwError)+import Control.Monad.State+import Data.Bifunctor (first)+import Data.Foldable (foldlM, foldrM)+import qualified Data.IntMap as IntMap+import qualified Data.Map as Map+import Data.Maybe (catMaybes, isJust)+import Data.Scientific (Scientific)+import Data.Semigroup ((<>))+import Data.Text (Text)+import GHC.Exts (IsList(..))++import Radicle.Internal.Core+import Radicle.Internal.Pretty++-- | A Bindings with an Env containing only 'eval' and only pure primops.+pureEnv :: (Monad m) => Bindings m+pureEnv = Bindings e purePrimops mempty 0+ where+ e = fromList [(toIdent "eval", Primop $ toIdent "base-eval")]++-- | The universal primops. These are available in chain evaluation, and are+-- not shadowable via 'define'.+purePrimops :: forall m. (Monad m) => Primops m+purePrimops = Map.fromList $ first Ident <$>+ [ ("base-eval", evalArgs $ \args -> case args of+ [x] -> baseEval x+ xs -> throwError $ WrongNumberOfArgs "base-eval" 1 (length xs))+ , ("list", evalArgs $ \args -> pure $ List args)+ , ("quote", \args -> case args of+ [v] -> pure v+ xs -> throwError $ WrongNumberOfArgs "quote" 1 (length xs))+ , ("define", \args -> case args of+ [Atom name, val] -> do+ val' <- baseEval val+ defineAtom name val'+ pure nil+ [_, _] -> throwError $ OtherError "define expects atom for first arg"+ xs -> throwError $ WrongNumberOfArgs "define" 2 (length xs))+ , ("catch", \args -> case args of+ [l, form, handler] -> do+ mlabel <- baseEval l+ case mlabel of+ Atom label -> baseEval form `catchError` \e -> do+ (thrownLabel, thrownValue) <- errorToValue e+ if thrownLabel == label || label == Ident "any"+ then handler $$ [thrownValue]+ else baseEval form+ _ -> throwError $ TypeError "catch: first argument must be atom"+ xs -> throwError $ WrongNumberOfArgs "catch" 3 (length xs))+ , ("throw", evalArgs $ \args -> case args of+ [Atom label, exc] -> throwError $ ThrownError label exc+ [_, _] -> throwError $ TypeError "throw: first argument must be atom"+ xs -> throwError $ WrongNumberOfArgs "throw" 2 (length xs))+ , ("eq?", evalArgs $ \args -> case args of+ [a, b] -> pure $ Boolean (a == b)+ xs -> throwError $ WrongNumberOfArgs "eq?" 2 (length xs))+ , ("cons", evalArgs $ \args -> case args of+ [x, List xs] -> pure $ List (x:xs)+ [_, _] -> throwError $ TypeError "cons: second argument must be list"+ xs -> throwError $ WrongNumberOfArgs "cons" 2 (length xs))+ , ("head", evalArgs $ \args -> case args of+ [List (x:_)] -> pure x+ [List []] -> throwError $ OtherError "head: empty list"+ [_] -> throwError $ TypeError "head: expects list argument"+ xs -> throwError $ WrongNumberOfArgs "head" 1 (length xs))+ , ("tail", evalArgs $ \args -> case args of+ [List (_:xs)] -> pure $ List xs+ [List []] -> throwError $ OtherError "tail: empty list"+ [_] -> throwError $ TypeError "tail: expects list argument"+ xs -> throwError $ WrongNumberOfArgs "tail" 1 (length xs))+ , ("lookup", evalArgs $ \args -> case args of+ [Atom a, Dict m] -> pure $ case Map.lookup a m of+ Just v -> v+ -- Probably an exception is better, but that seems cruel+ -- when you have no exception handling facilities.+ Nothing -> nil+ [Atom _, _] -> throwError+ $ TypeError "lookup: second argument must be map"+ [_, Dict _] -> throwError+ $ TypeError "lookup: first argument must be atom"+ xs -> throwError $ WrongNumberOfArgs "lookup" 2 (length xs))+ , ("string-append", evalArgs $ \args ->+ let fromStr (String s) = Just s+ fromStr _ = Nothing+ ss = fromStr <$> args+ in if all isJust ss+ then pure . String . mconcat $ catMaybes ss+ else throwError $ TypeError "string-append: non-string argument")+ , ("insert", evalArgs $ \args -> case args of+ [Atom k, v, Dict m] -> pure . Dict $ Map.insert k v m+ [Atom _, _, _] -> throwError+ $ TypeError "insert: third argument must be map"+ [_, _, _] -> throwError+ $ TypeError "insert: first argument must be an atom"+ xs -> throwError $ WrongNumberOfArgs "insert" 3 (length xs))+ -- The semantics of + and - in Scheme is a little messed up. (+ 3)+ -- evaluates to 3, and of (- 3) to -3. That's pretty intuitive.+ -- But while (+ 3 2 1) evaluates to 6, (- 3 2 1) evaluates to 0. So with -+ -- it is *not* correct to say that it's a foldl (-) 0. Instead, it+ -- special-cases on one-argument application. (Similarly with * and /.)+ --+ -- In order to avoid this sort of thing, we don't allow +,*,- and / to be+ -- applied to a single argument.+ , numBinop (+) "+"+ , numBinop (*) "*"+ , numBinop (-) "-"+ , ("<", evalArgs $ \args -> case args of+ [Number x, Number y] -> pure $ Boolean (x < y)+ [_, _] -> throwError $ TypeError "<: expecting number"+ xs -> throwError $ WrongNumberOfArgs "<" 2 (length xs))+ , (">", evalArgs $ \args -> case args of+ [Number x, Number y] -> pure $ Boolean (x > y)+ [_, _] -> throwError $ TypeError ">: expecting number"+ xs -> throwError $ WrongNumberOfArgs ">" 2 (length xs))+ , ("foldl", evalArgs $ \args -> case args of+ [fn, init', List ls] -> foldlM (\b a -> (fn $$) [b,a]) init' ls+ [_, _, _] -> throwError+ $ TypeError "foldl: third argument should be a list"+ xs -> throwError $ WrongNumberOfArgs "foldl" 3 (length xs))+ , ("foldr", evalArgs $ \args -> case args of+ [fn, init', List ls] -> foldrM (\b a -> (fn $$) [b,a]) init' ls+ [_, _, _] -> throwError+ $ TypeError "foldr: third argument should be a list"+ xs -> throwError $ WrongNumberOfArgs "foldr" 3 (length xs))+ , ("map", evalArgs $ \args -> case args of+ [fn, List ls] -> List <$> traverse (fn $$) (pure <$> ls)+ [_, _] -> throwError $ TypeError "map: second argument should be a list"+ xs -> throwError $ WrongNumberOfArgs "map" 3 (length xs))+ , ("string?", evalArgs $ \args -> case args of+ [String _] -> pure $ Boolean True+ [_] -> pure $ Boolean False+ xs -> throwError $ WrongNumberOfArgs "string?" 1 (length xs))+ , ("boolean?", evalArgs $ \args -> case args of+ [Boolean _] -> pure $ Boolean True+ [_] -> pure $ Boolean False+ xs -> throwError $ WrongNumberOfArgs "boolean?" 1 (length xs))+ , ("number?", evalArgs $ \args -> case args of+ [Number _] -> pure $ Boolean True+ [_] -> pure $ Boolean False+ xs -> throwError $ WrongNumberOfArgs "number?" 1 (length xs))+ , ("member?", evalArgs $ \args -> case args of+ [x, List xs] -> pure . Boolean $ elem x xs+ [_, _] -> throwError+ $ TypeError "member?: second argument must be list"+ xs -> throwError $ WrongNumberOfArgs "eq?" 2 (length xs))+ , ("if", \args -> case args of+ [cond, t, f] -> do+ b <- baseEval cond+ -- I hate this as much as everyone that might ever read Haskell, but+ -- in Lisps a lot of things that one might object to are True...+ if b == Boolean False then baseEval f else baseEval t+ xs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))+ , ("read-ref", evalArgs $ \args -> case args of+ [Ref (Reference x)] -> gets bindingsRefs >>= \m -> case IntMap.lookup x m of+ Nothing -> throwError $ Impossible "undefined reference"+ Just v -> pure v+ [_] -> throwError $ TypeError "read-ref: argument must be a ref"+ xs -> throwError $ WrongNumberOfArgs "read-ref" 1 (length xs))+ , ("write-ref", evalArgs $ \args -> case args of+ [Ref (Reference x), v] -> do+ st <- get+ put $ st { bindingsRefs = IntMap.insert x v $ bindingsRefs st }+ pure nil+ [_, _] -> throwError+ $ TypeError "write-ref: first argument must be a ref"+ xs -> throwError+ $ WrongNumberOfArgs "write-ref" 2 (length xs))+ , ( "show"+ , evalArgs $ \args -> case args of+ [x] -> pure (String (renderPrettyDef x))+ xs -> throwError $ WrongNumberOfArgs "show" 1 (length xs)+ )+ ]+ where+ -- Many primops evaluate their arguments just as normal functions do.+ evalArgs f args = traverse baseEval args >>= f++ numBinop :: (Scientific -> Scientific -> Scientific)+ -> Text+ -> (Text, [Value Reference] -> Lang m (Value Reference))+ numBinop fn name = (name, evalArgs $ \args -> case args of+ Number x:x':xs -> foldM go (Number x) (x':xs)+ where+ go (Number a) (Number b) = pure . Number $ fn a b+ go _ _ = throwError . TypeError+ $ name <> ": expecting number"+ [Number _] -> throwError+ $ OtherError $ name <> ": expects at least 2 arguments"+ _ -> throwError $ TypeError $ name <> ": expecting number")diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hsindex cce962e..ee1551a 100644--- a/src/Radicle/Internal/Subscriber.hs+++ b/src/Radicle/Internal/Subscriber.hs@@ -1,9 +1,8 @@-{-# LANGUAGE ScopedTypeVariables #-}module Radicle.Internal.Subscriber where-import Control.Monad (void, forever)+import Control.Monad (forever, void)import Control.Monad.Except (throwError)-import Control.Monad.State (gets, get)+import Control.Monad.State (get, gets)import Data.Bifunctor (first)import Data.List (isPrefixOf)import qualified Data.Map as Map@@ -20,6 +19,7 @@ import System.Console.Haskeline (CompletionFunc, InputT, completeWord,import Radicle.Internal.Coreimport Radicle.Internal.Parseimport Radicle.Internal.Pretty+import Radicle.Internal.Primopsimport Radicle.Internal.Subscriber.Capabilitiesdiff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hsindex 87a87f7..649b669 100644--- a/test/spec/Radicle/Internal/Arbitrary.hs+++ b/test/spec/Radicle/Internal/Arbitrary.hs@@ -14,8 +14,8 @@ import Test.QuickCheckimport Test.QuickCheck.Instances ()import Radicle-import Radicle.Internal.Core (purePrimops)import Radicle.Internal.Parse (isValidIdentFirst, isValidIdentRest)+import Radicle.Internal.Primops (purePrimops)instance Arbitrary r => Arbitrary (Env r) wherearbitrary = Env <$> arbitrarydiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex a3d5b7c..ba3d957 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -1,5 +1,4 @@{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE ScopedTypeVariables #-}module Radicle.Tests whereimport Data.Either (isLeft)@@ -277,6 +276,18 @@ test_eval =info = "Expected:\n" <> T.unpack (prettyEither orig)<> "\nGot:\n" <> T.unpack (prettyEither derefed)counterexample info $ derefed == orig++ , testCase "'show' works" $ do+ runTest' "(show 'a)" @?= Right (String "a")+ runTest' "(show ''a)" @?= Right (String "(quote a)")+ runTest' "(show \"hello\")" @?= Right (String "\"hello\"")+ runTest' "(show 42)" @?= Right (String "42.0")+ runTest' "(show #t)" @?= Right (String "#t")+ runTest' "(show #f)" @?= Right (String "#f")+ runTest' "(show (list 'a 1 \"foo\" (list 'b ''x 2 \"bar\")))" @?= Right (String "(a 1.0 \"foo\" (b (quote x) 2.0 \"bar\"))")+ runTest' "eval" @?= Right (Primop (toIdent "base-eval"))+ runTest' "(show (dict 'a 1))" @?= Right (String "(dict 'a 1)")+ runTest' "(show (lambda (x) x))" @?= Right (String "(lambda (x) x)")]wherefailsWith src err = runTest' src @?= Left err@@ -331,7 +342,7 @@ test_binding =test_pretty :: [TestTree]test_pretty =- [ testProperty "pretty . parse == identity" $ \(val :: Value (Fix Value)) ->+ [ testProperty "parse . pretty == identity" $ \(val :: Value (Fix Value)) ->let rendered = renderPrettyDef valactual = parseTest renderedoriginal = removeEnv' val