A peer-to-peer stack for code collaboration
Documentation and API improvements.
Julian K. Arni8f3c640edfab
diff --git a/src/Radicle.hs b/src/Radicle.hsindex 045ae6a..5d900ae 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -1,6 +1,11 @@+-- | radicle - A LISP for blocktrees.+--+-- This is the only module you should need to import.module Radicle(-- * Language+ --+ -- | The definition of the core language.Value(..), interpret, LangError(..)@@ -28,8 +33,13 @@ module Radicle, parseTest-- * Pretty-printing- , compactPretty+ , renderPretty+ , renderPrettyDef+ , renderCompactPretty+ -- ** Re-exports+ , PageWidth(..)) where-import Radicle.Internal+import Data.Text.Prettyprint.Doc+import Radicle.Internaldiff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hsindex ebd85b8..cee031e 100644--- a/src/Radicle/Internal.hs+++ b/src/Radicle/Internal.hs@@ -10,7 +10,7 @@ import Radicle.Internal.Core as Ximport Radicle.Internal.Parse as Ximport Radicle.Internal.Pretty as X-import Control.Monad.Except+import Control.Monad.Exceptimport Data.Text (Text)import qualified Text.Megaparsec as Mdiff --git a/src/Radicle/Internal/Chain.hs b/src/Radicle/Internal/Chain.hsindex 429c385..18ea292 100644--- a/src/Radicle/Internal/Chain.hs+++ b/src/Radicle/Internal/Chain.hs@@ -34,9 +34,11 @@ instance (Semigroup (m a), Monoid (m a)) => Monoid (Subscriber m a) wheremempty = Subscriber . ReaderT $ const memptymappend = (<>)+-- | A helper for running subscribers.runSubscriber :: Subscriber m a -> Value -> m arunSubscriber = runReaderT . fromSubscriber+-- | A helper for making subscribers.makeSubscriber :: (Value -> m a) -> Subscriber m amakeSubscriber = Subscriber . ReaderT@@ -83,8 +85,8 @@ genesisChain = ChainupdateEnv :: Value -> Env -> EnvupdateEnv v = case v of- List [Atom "set!", Atom a, val] -> setEnv a val- _ -> id+ List [Atom (Ident "set!"), Atom a, val] -> setEnv a val+ _ -> id-- | Fold a chain. All effects are accumulated with a monoid instance.diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex cdf575d..ae361b2 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -5,6 +5,7 @@ import Control.Monad.Except (ExceptT, MonadError, runExceptT,throwError)import Control.Monad.Reader (MonadReader, Reader, ask, local,runReader)+import Data.Bifunctor (first)import Data.Data (Data)import Data.Map (Map)import qualified Data.Map as Map@@ -12,7 +13,7 @@ import Data.Semigroup (Semigroup, (<>))import Data.Text (Text)import qualified Data.Text as Timport Data.Void (Void)-import GHC.Exts (IsList(..), IsString)+import GHC.Exts (IsList(..), fromString)import GHC.Generics (Generic)import qualified Text.Megaparsec.Error as Par@@ -23,21 +24,23 @@ data LangError =UnknownIdentifier Ident| Impossible Text| TypeError Text- | WrongNumberOfArgs- Text -- ^ function name- Int -- ^ expected- Int -- ^ actual+ -- | Takes the function name, expected number of args, and actual number of+ -- args+ | WrongNumberOfArgs Text Int Int| OtherError Text| ParseError (Par.ParseError Char Void)deriving (Eq, Show, Read, Generic)-- | An identifier in the language.----- Not all Texts are valid identifiers, so you should not be using the Ident--- constructor directly. Instead, use makeIdent from Oscoin.Language+-- Not all `Text`s are valid identifiers, so we do not export the constructor.+-- Instead, use `makeIdent`.newtype Ident = Ident { fromIdent :: Text }- deriving (Eq, Show, Read, Ord, Generic, IsString, Data)+ deriving (Eq, Show, Read, Ord, Generic, Data)+-- Unsafe!+identFromString :: String -> Ident+identFromString = Ident . fromString-- | An expression or value in the language.data Value =@@ -50,10 +53,9 @@ data Value =| SortedMap (Map.Map Ident Value)-- | Since there are no side-effects, there is no point having a list of-- values for the body of a lambda.- | Lambda- [Ident] -- ^ parameters- Value -- ^ body.- (Maybe Env) -- ^ closure+ --+ -- Takes the arguments/parameters, a body, and possibly a closure.+ | Lambda [Ident] Value (Maybe Env)deriving (Eq, Ord, Show, Read, Generic, Data)-- | The environment, which keeps all known bindings.@@ -92,9 +94,9 @@ lookupPrimop i = case Map.lookup i primops ofJust v -> pure v-- | The universal primops. These are available in chain evaluation, and are--- not shadowable.+-- not shadowable via 'define'.primops :: Map Ident ([Value] -> LangM Value)-primops = Map.fromList+primops = Map.fromList $ first identFromString <$>[ ("eval", \args -> case args of[List (v:vs)] -> eval (Apply v vs)[Apply fn v ] -> eval =<< Apply <$> eval fn <*> traverse eval v@@ -107,7 +109,7 @@ primops = Map.fromList, ("define", \args -> case args of[Atom name, val] -> doval' <- eval val- pure $ List [Atom "set!", Atom name, val']+ pure $ List [Atom $ identFromString "set!", Atom name, val'][_, _] -> throwError $ OtherError "define expects atom for first arg"xs -> throwError $ WrongNumberOfArgs "define" 2 (length xs)), ("string?", \args -> case args ofdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 71c547e..458f26c 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -30,5 +30,14 @@ instance Pretty Value wherewhereescapeStr = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"-compactPretty :: Value -> Text-compactPretty = renderStrict . layoutCompact . pretty+-- | A fast and compact layout. Primarily intended for testing.+renderCompactPretty :: Value -> Text+renderCompactPretty = renderStrict . layoutCompact . pretty++-- | Render prettily into text, with specified width.+renderPretty :: PageWidth -> Value -> Text+renderPretty pg = renderStrict . layoutSmart (LayoutOptions pg) . pretty++-- | 'renderPretty', but with default layout options (80 chars, 1.0 ribbon)+renderPrettyDef :: Value -> Text+renderPrettyDef = renderStrict . layoutSmart defaultLayoutOptions . prettydiff --git a/test/Radicle/Tests.hs b/test/Radicle/Tests.hsindex cc8038e..fedabe9 100644--- a/test/Radicle/Tests.hs+++ b/test/Radicle/Tests.hs@@ -14,13 +14,14 @@ import Test.Tasty.QuickCheck (testProperty, counterexample)import Control.Monad.Morph (generalize, hoist)import Radicle.Internal.Arbitrary ()+import Radicle.Internal.Core (identFromString)import Radicletest_eval :: [TestTree]test_eval =[ testCase "Fails for undefined atoms" $- [s|blah|] `failsWith` UnknownIdentifier "blah"+ [s|blah|] `failsWith` UnknownIdentifier (i "blah"), testCase "Succeeds for defined atoms" $ dolet prog = [s|@@ -31,7 +32,7 @@ test_eval =, testCase "'sorted-map' creates a SortedMap with given key/vals" $ dolet prog = [s|(sorted-map why "not")|]- prog `succeedsWith` SortedMap (fromList [("why", String "not")])+ prog `succeedsWith` SortedMap (fromList [(i "why", String "not")]), testCase "'eval' evaluates the list" $ dolet prog = [s|(eval (quote #t))|]@@ -103,16 +104,16 @@ test_parser ="#f" ==> Boolean False, testCase "parses primops" $ do- "boolean?" ==> Primop "boolean?"- "eval" ==> Primop "eval"+ "boolean?" ==> Primop (i "boolean?")+ "eval" ==> Primop (i "eval"), testCase "parses identifiers" $ do- "++" ==> Atom "++"- "what?crazy!" ==> Atom "what?crazy!"+ "++" ==> Atom (i "++")+ "what?crazy!" ==> Atom (i "what?crazy!"), testCase "parses function application" $ do- "(++)" ==> (Atom "++" $$ [])- "(++ \"merge\" \"d\")" ==> (Atom "++" $$ [String "merge", String "d"])+ "(++)" ==> (Atom (i "++") $$ [])+ "(++ \"merge\" \"d\")" ==> (Atom (i "++") $$ [String "merge", String "d"])]wherex ==> y = parseTest x @?= Right y@@ -144,7 +145,7 @@ test_subscriber =test_pretty :: [TestTree]test_pretty =[ testProperty "pretty . parse == identity" $ \val ->- let rendered = compactPretty val+ let rendered = renderCompactPretty valactual = parseTest renderedoriginal = everywhere removeEnv valinfo = case actual of@@ -170,3 +171,6 @@ test_env =[ testProperty "fromList . toList == identity" $ \(env :: Env) ->fromList (toList env) == env]++i :: String -> Ident+i = identFromString