Skip to content

A peer-to-peer stack for code collaboration

feat(lang): Varargs--support lambda definition with an atom representing an arguments vector (#606)

Max Hallinance3b6c982808
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index d5a9056..008e108 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -110,9 +110,15 @@ valueDoc name docString value = case value of
<> parseMarkdownBlocks docString
where
lambdaDoc args =
- let callExample = "(" <> T.intercalate " " (name : map fromIdent args) <> ")"
- in [ Header 3 nullAttr [Code nullAttr (toS callExample) ] ]
- <> parseMarkdownBlocks docString
+ let callExample = "(" <> T.intercalate " " (name : lambdaArgsDoc args) <> ")"
+ in [ Header 3 nullAttr [Code nullAttr (toS callExample) ] ]
+ <> parseMarkdownBlocks docString
+ lambdaArgsDoc args =
+ case args of
+ PosArgs argNames ->
+ map fromIdent argNames
+ VarArgs _ ->
+ pure "arg1 ..."
-- | Generate documentation for primitive functions defined by 'replBindings'
docForPrimFns :: Content -> [Block]
diff --git a/src/Radicle.hs b/src/Radicle.hs
index 213e2bf..7a82f88 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -23,6 +23,7 @@ module Radicle
--
-- *** Value
, ValueF(..)
+ , LambdaArgs(..)
, type Value
, Type(..)
, pattern Atom
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 488ebe8..ddc3fee 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -255,19 +255,26 @@ data ValueF r =
--
-- The value of an application of a lambda is always the last value in the
-- body. The only reason to have multiple values is for effects.
- | LambdaF [Ident] (NonEmpty r) (Env r)
+ | LambdaF LambdaArgs (NonEmpty r) (Env r)
-- | Like 'LambdaF' but indicates a function that can call itself
-- recursively.
--
-- The first argument is the name for recursive calls to the
-- function in the body.
- | LambdaRecF Ident [Ident] (NonEmpty r) (Env r)
+ | LambdaRecF Ident LambdaArgs (NonEmpty r) (Env r)
| VEnvF (Env r)
| VStateF State
deriving (Eq, Ord, Read, Show, Generic, Functor)
instance Serialise r => Serialise (ValueF r)
+data LambdaArgs =
+ PosArgs [Ident]
+ | VarArgs Ident
+ deriving (Eq, Ord, Read, Show, Generic)
+
+instance Serialise LambdaArgs
+
valType :: (CPA t) => Annotated t ValueF -> Type.Type
valType = \case
Atom _ -> TAtom
@@ -377,12 +384,12 @@ pattern ProcHandle i <- (Ann.match -> ProcHandleF i)
where
ProcHandle = Ann.annotate . ProcHandleF
-pattern Lambda :: ValueConC t => [Ident] -> NonEmpty (Annotated t ValueF) -> Env (Annotated t ValueF) -> Annotated t ValueF
+pattern Lambda :: ValueConC t => LambdaArgs -> NonEmpty (Annotated t ValueF) -> Env (Annotated t ValueF) -> Annotated t ValueF
pattern Lambda vs exps env <- (Ann.match -> LambdaF vs exps env)
where
Lambda vs exps env = Ann.annotate $ LambdaF vs exps env
-pattern LambdaRec :: ValueConC t => Ident -> [Ident] -> NonEmpty (Annotated t ValueF) -> Env (Annotated t ValueF) -> Annotated t ValueF
+pattern LambdaRec :: ValueConC t => Ident -> LambdaArgs -> NonEmpty (Annotated t ValueF) -> Env (Annotated t ValueF) -> Annotated t ValueF
pattern LambdaRec self vs exps env <- (Ann.match -> LambdaRecF self vs exps env)
where
LambdaRec self vs exps env = Ann.annotate $ LambdaRecF self vs exps env
diff --git a/src/Radicle/Internal/Eval.hs b/src/Radicle/Internal/Eval.hs
index 4f9f0b5..be7511b 100644
--- a/src/Radicle/Internal/Eval.hs
+++ b/src/Radicle/Internal/Eval.hs
@@ -57,14 +57,17 @@ specialForms :: forall m. (Monad m) => Map Ident ([Value] -> Lang m Value)
specialForms = Map.fromList $ first Ident <$>
[ ( "fn"
, \case
- args : b : bs ->
+ args : b : bs -> do
+ e <- gets bindingsEnv
+ let toLambda argNames = pure (Lambda argNames (b :| bs) e)
case args of
Vec atoms_ -> do
atoms <- traverse isAtom (toList atoms_) ?? toLangError (SpecialForm "fn" "One of the arguments was not an atom")
- e <- gets bindingsEnv
- pure (Lambda atoms (b :| bs) e)
- _ -> throwErrorHere $ SpecialForm "fn" "First argument must be a vector of argument atoms"
- _ -> throwErrorHere $ SpecialForm "fn" "Need an argument vector and a body"
+ toLambda (PosArgs atoms)
+ Atom name -> do
+ toLambda (VarArgs name)
+ _ -> throwErrorHere $ SpecialForm "fn" "Function parameters must be one atom or a vector of atoms"
+ _ -> throwErrorHere $ SpecialForm "fn" "Function needs parameters (one atom or a vector of atoms) and a body"
)
, ( "module", createModule )
, ("quote", \case
@@ -222,11 +225,15 @@ callFn f arguments = case f of
evalManyWithEnv :: Env Value -> NonEmpty Value -> Lang m Value
evalManyWithEnv env exprs =
NonEmpty.last <$> withEnv (const env) (traverse baseEval exprs)
- argumentBindings :: [Ident] -> Lang m (Env Value)
- argumentBindings names =
+ argumentBindings :: LambdaArgs -> Lang m (Env Value)
+ argumentBindings argNames = case argNames of
+ PosArgs names ->
if length names /= length arguments
then throwErrorHere $ WrongNumberOfArgs "lambda" (length names) (length arguments)
- else pure $ GhcExts.fromList (Doc.noDocs $ zip names arguments)
+ else toArgs $ zip names arguments
+ VarArgs name ->
+ toArgs [(name, Vec $ Seq.fromList arguments)]
+ where toArgs = pure . GhcExts.fromList . Doc.noDocs
-- | Process special forms or call function. If @f@ is an atom that
-- indicates a special form that special form is processed. Otherwise
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index 1f01f6a..6f8cdb3 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -74,8 +74,12 @@ instance forall t. (Copointed t, Ann.Annotation t) => PrettyV (Ann.Annotated t V
where
-- We print string literals escaped just like Haskell does.
escapeStr = T.init . T.tail . show
- prettyLambda ids vals =
- let args :: Ann.Annotated t ValueF = Vec $ Seq.fromList (Atom <$> ids)
+ prettyLambda lArgs vals =
+ let args :: Ann.Annotated t ValueF = case lArgs of
+ PosArgs ids ->
+ Vec $ Seq.fromList (Atom <$> ids)
+ VarArgs id ->
+ Atom id
in parens $
annotate TAtom "fn" <+>
align (sep [ prettyV args
diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hs
index c28b258..044f1f1 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -32,7 +32,7 @@ instance Arbitrary Value where
, (3, Number <$> arbitrary)
, (1, List <$> sizedList)
, (6, PrimFn <$> elements (Map.keys $ getPrimFns prims))
- , (1, Lambda <$> sizedList
+ , (1, Lambda <$> lambdaArgs
<*> scale (`div` 3) arbitrary
<*> scale (`div` 3) arbitrary)
]
@@ -46,6 +46,7 @@ instance Arbitrary Value where
prims = purePrimFns
isPrimop x = x `elem` Map.keys (getPrimFns prims)
isNum x = isJust (readMaybe (toS $ fromIdent x) :: Maybe Scientific)
+ lambdaArgs = oneof [ PosArgs <$> sizedList, VarArgs <$> arbitrary ]
instance Arbitrary UntaggedValue where
arbitrary = untag <$> (arbitrary :: Gen Value)
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index d72169c..3222ff5 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -219,10 +219,14 @@ test_eval =
<> "\nGot:\n" <> prettyEither res1
counterexample (toS info) $ res1 == res2
- , testCase "lambdas work" $ do
+ , testCase "lambdas with explicit arguments work" $ do
let prog = [s|((fn [x] x) #t)|]
prog `succeedsWith` Boolean True
+ , testCase "lambdas with a vector of arguments work" $ do
+ let prog = [s|((fn x (first x)) #t)|]
+ prog `succeedsWith` Boolean True
+
, testCase "lambda does not have access to future definitions (hyper static)" $ do
let prog = [s|
(def y "a")