A peer-to-peer stack for code collaboration
Improve documentation (#102)
Julian Arnif42aac544548
diff --git a/README.md b/README.mdindex 7f87d50..5418544 100644--- a/README.md+++ b/README.md@@ -19,3 +19,23 @@ stack build```stack test```++## How to run executables++Running scripts and a REPL are the same thing - some scripts (in particular+`rad/repl.rad`) just *define* a REPL.++You should probably run the client against radicle.xyz for fuller+functionality.++```+stack exec client -- --config rad/repl.rad --url radicle.xyz+```++## The language++The language currently lacking good documentation. A good place to get a sense+for it is in the rad/ directory, which contains a number of radicle files.++Variables defined there are usually documented. This means you can type `(doc+'ident)` to retrieve documentation for the identifier `ident`.diff --git a/src/Radicle.hs b/src/Radicle.hsindex 25bed53..18f6b98 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -12,24 +12,26 @@ module Radicle-- * Language---- | The definition of the core language.- Value(..)- , maybeJson- , interpret+ -- ** Evaluation+ interpret, interpretMany+ , eval+ -- ** Datatypes+ --+ -- *** Value+ , Value(..)+ , maybeJson+ -- *** LangError, LangError(..)- , ($$), Ident(..), Reference(..), mkIdent- , quote, Env(..), pureEnv, Lang, runLang, Bindings(..)- , addBinding, replBindings- , eval-- ** Re-exports, runIdentity@@ -60,6 +62,8 @@ module Radicle, getHistory-- * Helpers+ , quote+ , ($$), kwLookup, makeBindings, unmakeBindingsdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 59c87f1..73cb371 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -1,3 +1,4 @@+-- | The core radicle datatypes and functionality.module Radicle.Internal.Core whereimport Protolude hiding (TypeError, (<>))@@ -131,6 +132,23 @@ instance A.FromJSON Value wherevs <- traverse parseJSON (snd <$> kvs)pure . Dict . Map.fromList $ zip (String . fst <$> kvs) vs+-- | Convert a radicle `Value` into an 'aeson' value, if possible.+--+-- >>> import Data.Aeson (encode)+-- >>> encode $ maybeJson $ List [Number 3, String "hi"]+-- "[3,\"hi\"]"+--+-- >>> import Data.Aeson (encode)+-- >>> encode $ maybeJson $ Dict $ Map.fromList [(String "foo", String "bar")]+-- "{\"foo\":\"bar\"}"+--+-- This fails for lambdas, since lambdas capture an entire environment+-- (possibly recursively). It also fails for dictionaries with non-string key+-- non-string keys.+--+-- >>> import Data.Aeson (encode)+-- >>> encode $ maybeJson $ Dict $ Map.fromList [(Number 3, String "bar")]+-- "null"maybeJson :: Value -> Maybe A.ValuemaybeJson = \caseNumber n -> pure $ A.Number n@@ -149,8 +167,8 @@ maybeJson = \case-- | An identifier in the language.----- Not all `Text`s are valid identifiers, so use Ident at your own risk.--- `makeIdent` is the safe version.+-- Not all `Text`s are valid identifiers, so use 'Ident' at your own risk.+-- `mkIdent` is the safe version.newtype Ident = Ident { fromIdent :: Text }deriving (Eq, Show, Read, Ord, Generic, Data, Serialise)@@ -190,6 +208,8 @@ instance MonadTrans (LangT r) where lift = LangT . lift . lift-- with appropriate underlying monad.type Lang m = LangT (Bindings m) m+-- | Run a `Lang` computation with the provided bindings. Returns the result as+-- well as the updated bindings.runLang:: Bindings m-> Lang m adiff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 9aa9205..1507066 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -110,7 +110,7 @@ valueP = do-- * Utilities--- | Parse and evaluate a Text. Replaces refs with a number representing them.+-- | Parse and evaluate a Text.---- Examples:--@@ -124,9 +124,9 @@ valueP = do-- Left (TypeError "Trying to apply a non-function")interpret:: Monad m- => Text- -> Text- -> Bindings m+ => Text -- ^ Name of source file (for error reporting)+ -> Text -- ^ Source code to be interpreted+ -> Bindings m -- ^ Bindings to be used-> m (Either (LangError Value) Value)interpret sourceName expr bnds = dolet primopNames = Map.keys (bindingsPrimops bnds)@@ -137,12 +137,19 @@ interpret sourceName expr bnds = do-- | Parse and evaluate a Text as multiple expressions.--+-- The first argument is the name of the source file to be used for error+-- reporting.+---- Examples:---- >>> import Radicle.Internal.Primops-- >>> fmap fst <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"-- Right (Boolean True)-interpretMany :: Monad m => Text -> Text -> Lang m Value+interpretMany+ :: Monad m+ => Text -- ^ Name of source file (for error reporting)+ -> Text -- ^ Source code to be interpreted+ -> Lang m ValueinterpretMany sourceName src = doprimopNames <- gets $ Map.keys . bindingsPrimopslet parsed = parseValues sourceName src primopNames@@ -156,10 +163,13 @@ interpretMany sourceName src = do(e:_, _) -> throwError $ ParseError e-- | Parse a Text as a series of values.--- 'sourceName' is used for error reporting. 'prims' are the primop names.---- Note that parsing continues even if one value fails to parse.-parseValues :: Text -> Text -> [Ident] -> [Either (Par.ParseError Char Void) Value]+parseValues+ :: Text -- ^ Name of source file (for error reporting)+ -> Text -- ^ Source code to be parsed+ -> [Ident] -- ^ Primop identifiers+ -> [Either (Par.ParseError Char Void) Value]parseValues sourceName srcCode prims = withoutLeadingSpaceswhereinitial = State@@ -174,8 +184,7 @@ parseValues sourceName srcCode prims = withoutLeadingSpacesgo s = let (s', v) = runReader (runParserT' valueP s) primsin if T.null (stateInput s') then [v] else v:go s'--- | Parse a value, using the String as source name, and the identifier list as--- the primops.+-- | Parse a single value.---- Examples:--@@ -187,7 +196,11 @@ parseValues sourceName srcCode prims = withoutLeadingSpaces---- >>> parse "test" "hi" [] :: Either Text Value-- Right (Atom (Ident {fromIdent = "hi"}))-parse :: MonadError Text m => Text -> Text -> [Ident] -> m Value+parse :: MonadError Text m+ => Text -- ^ Name of source file (for error reporting)+ -> Text -- ^ Source code to be parsed+ -> [Ident] -- ^ Primop identifiers+ -> m Valueparse file src ids = dolet res = runReader (M.runParserT (spaceConsumer *> valueP <* eof) (toS file) src) idscase res of