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.hsindex d5a9056..008e108 100644--- a/exe/ReferenceDoc.hs+++ b/exe/ReferenceDoc.hs@@ -110,9 +110,15 @@ valueDoc name docString value = case value of<> parseMarkdownBlocks docStringwherelambdaDoc 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.hsindex 213e2bf..7a82f88 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -23,6 +23,7 @@ module Radicle---- *** Value, ValueF(..)+ , LambdaArgs(..), type Value, Type(..), pattern Atomdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 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 Statederiving (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.TypevalType = \caseAtom _ -> TAtom@@ -377,12 +384,12 @@ pattern ProcHandle i <- (Ann.match -> ProcHandleF i)whereProcHandle = 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 ValueFpattern Lambda vs exps env <- (Ann.match -> LambdaF vs exps env)whereLambda 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 ValueFpattern LambdaRec self vs exps env <- (Ann.match -> LambdaRecF self vs exps env)whereLambdaRec self vs exps env = Ann.annotate $ LambdaRecF self vs exps envdiff --git a/src/Radicle/Internal/Eval.hs b/src/Radicle/Internal/Eval.hsindex 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 ofVec atoms_ -> doatoms <- 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 ofevalManyWithEnv :: Env Value -> NonEmpty Value -> Lang m ValueevalManyWithEnv 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 argumentsthen 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. Otherwisediff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 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 Vwhere-- 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 idin parens $annotate TAtom "fn" <+>align (sep [ prettyV argsdiff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hsindex 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 whereprims = purePrimFnsisPrimop 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 wherearbitrary = untag <$> (arbitrary :: Gen Value)diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex d72169c..3222ff5 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -219,10 +219,14 @@ test_eval =<> "\nGot:\n" <> prettyEither res1counterexample (toS info) $ res1 == res2- , testCase "lambdas work" $ do+ , testCase "lambdas with explicit arguments work" $ dolet 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)" $ dolet prog = [s|(def y "a")