A peer-to-peer stack for code collaboration
Support Haskell constructors with symbols. (#136)
James Haydond20061d1f420
diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hsindex bb3df9b..6a4f2bb 100644--- a/src/Radicle/Internal.hs+++ b/src/Radicle/Internal.hs@@ -13,6 +13,7 @@ import Radicle.Internal.Annotation as Ximport Radicle.Internal.CLI as Ximport Radicle.Internal.Core as Ximport Radicle.Internal.Effects as X+import Radicle.Internal.Identifier as Ximport Radicle.Internal.Interpret as Ximport Radicle.Internal.Parse as Ximport Radicle.Internal.Pretty as Xdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 7bb0463..40be7a2 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -29,6 +29,7 @@ import qualified Text.Megaparsec.Error as Parimport Radicle.Internal.Annotation (Annotated)import qualified Radicle.Internal.Annotation as Ann+import qualified Radicle.Internal.Identifier as Identifierimport Radicle.Internal.Orphans ()@@ -131,7 +132,7 @@ data ValueF r =| NumberF Scientific| BooleanF Bool| ListF [r]- | VecF (Seq.Seq r)+ | VecF (Seq r)| PrimFnF Ident-- | Map from *pure* Values -- annotations shouldn't change lookup semantics.| DictF (Map.Map Value r)@@ -179,7 +180,7 @@ pattern List vs <- (Ann.match -> ListF vs)whereList = Ann.annotate . ListF-pattern Vec :: ValueConC t => Seq.Seq (Annotated t ValueF) -> Annotated t ValueF+pattern Vec :: ValueConC t => Seq (Annotated t ValueF) -> Annotated t ValueFpattern Vec vs <- (Ann.match -> VecF vs)whereVec = Ann.annotate . VecF@@ -480,46 +481,48 @@ specialForms = Map.fromList $ first Ident <$>-- * From/ToRadicle-class FromRad a where- fromRad :: Value -> Either Text a- default fromRad :: (HasEot a, FromRadG (Eot a)) => Value -> Either Text a+type CPA t = (Ann.Annotation t, Copointed t)++class FromRad t a where+ fromRad :: Annotated t ValueF -> Either Text a+ default fromRad :: (CPA t, HasEot a, FromRadG t (Eot a)) => Annotated t ValueF -> Either Text afromRad = fromRadG-instance FromRad () where+instance CPA t => FromRad t () wherefromRad (Vec Seq.Empty) = pure ()fromRad _ = Left "Expecting an empty vector"-instance (FromRad a, FromRad b) => FromRad (a,b) where+instance (CPA t, FromRad t a, FromRad t b) => FromRad t (a,b) wherefromRad (Vec (x :<| y :<| Seq.Empty)) = (,) <$> fromRad x <*> fromRad yfromRad _ = Left "Expecting a vector of length 2"-instance FromRad Value where+instance FromRad Ann.WithPos Value wherefromRad = pure-instance FromRad Scientific where+instance CPA t => FromRad t Scientific wherefromRad x = case x ofNumber n -> pure n_ -> Left "Expecting number"-instance FromRad Integer where+instance CPA t => FromRad t Integer wherefromRad = \caseNumber s -> case floatingOrInteger s ofLeft (_ :: Double) -> Left "Expecting whole number"Right i -> pure i_ -> Left "Expecting number"-instance FromRad Text where+instance CPA t => FromRad t Text wherefromRad x = case x ofString n -> pure n_ -> Left "Expecting string"-instance FromRad a => FromRad [a] where+instance (CPA t, FromRad t a) => FromRad t [a] wherefromRad x = case x ofList xs -> traverse fromRad xsVec xs -> traverse fromRad (toList xs)_ -> Left "Expecting list"-instance FromRad (Env Value) where+instance FromRad Ann.WithPos (Env Value) wherefromRad x = case x ofDict d -> fmap (Env . Map.fromList)$ forM (Map.toList d) $ \(k, v) -> case k ofAtom i -> pure (i, v)k' -> Left $ "Expecting atom keys. Got: " <> show k'_ -> Left "Expecting dict"-instance FromRad (Bindings ()) where+instance FromRad Ann.WithPos (Bindings ()) wherefromRad x = case x ofDict d -> doenv' <- kwLookup "env" d ?? "Expecting 'env' key"@@ -534,30 +537,30 @@ instance FromRad (Bindings ()) where_ -> throwError $ "Expecting dict"-class ToRad a where- toRad :: a -> Value- default toRad :: (HasEot a, ToRadG (Eot a)) => a -> Value+class ToRad t a where+ toRad :: a -> Annotated t ValueF+ default toRad :: (HasEot a, ToRadG t (Eot a)) => a -> Annotated t ValueFtoRad = toRadG-instance ToRad () where+instance CPA t => ToRad t () wheretoRad _ = Vec Empty-instance (ToRad a, ToRad b) => ToRad (a,b) where+instance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) wheretoRad (x,y) = Vec $ toRad x :<| toRad y :<| Empty-instance ToRad Int where+instance CPA t => ToRad t Int wheretoRad = Number . fromIntegral-instance ToRad Integer where+instance CPA t => ToRad t Integer wheretoRad = Number . fromIntegral-instance ToRad Scientific where+instance CPA t => ToRad t Scientific wheretoRad = Number-instance ToRad Text where+instance CPA t => ToRad t Text wheretoRad = String-instance ToRad a => ToRad [a] where+instance (CPA t, ToRad t a) => ToRad t [a] wheretoRad xs = List $ toRad <$> xs-instance ToRad a => ToRad (Map.Map Text a) where+instance (CPA t, ToRad t a) => ToRad t (Map.Map Text a) wheretoRad xs = Dict $ Map.mapKeys String $ toRad <$> xs-instance ToRad (Env Value) where+instance ToRad Ann.WithPos (Env Value) wheretoRad x = Dict . Map.mapKeys Atom $ fromEnv x-instance ToRad (Bindings m) where+instance ToRad Ann.WithPos (Bindings m) wheretoRad x = Dict $ Map.fromList[ (Keyword $ Ident "env", toRad $ bindingsEnv x), (Keyword $ Ident "refs", List $ IntMap.elems (bindingsRefs x))@@ -607,7 +610,7 @@ quote v = List [Atom (Ident "quote"), v]list :: [Value] -> Valuelist vs = List (Atom (Ident "list") : vs)-kwLookup :: Text -> Map Value Value -> Maybe Value+kwLookup :: Text -> Map Value (Annotated t ValueF) -> Maybe (Annotated t ValueF)kwLookup key = Map.lookup (Keyword $ Ident key)(??) :: MonadError e m => Maybe a -> e -> m a@@ -631,13 +634,13 @@ evenArgs name = \case-- * Generic encoding/decoding of Radicle values.-toRadG :: forall a. (HasEot a, ToRadG (Eot a)) => a -> Value+toRadG :: forall a t. (HasEot a, ToRadG t (Eot a)) => a -> Annotated t ValueFtoRadG x = toRadConss (constructors (datatype (Proxy :: Proxy a))) (toEot x)-class ToRadG a where- toRadConss :: [Constructor] -> a -> Value+class ToRadG t a where+ toRadConss :: [Constructor] -> a -> Annotated t ValueF-instance (ToRadFields a, ToRadG b) => ToRadG (Either a b) where+instance (CPA t, ToRadFields t a, ToRadG t b) => ToRadG t (Either a b) wheretoRadConss (Constructor name fieldMeta : _) (Left fields) =case fieldMeta ofSelectors names ->@@ -648,52 +651,55 @@ instance (ToRadFields a, ToRadG b) => ToRadG (Either a b) wheretoRadConss (_ : r) (Right next) = toRadConss r nexttoRadConss [] _ = panic "impossible"-radCons :: Text -> [Value] -> Value-radCons name args = List ( Keyword (Ident name) : args )+radCons :: CPA t => Text -> [Annotated t ValueF] -> Annotated t ValueF+radCons name args = case args of+ [] -> consKw+ _ -> Vec ( consKw :<| Seq.fromList args )+ where+ consKw = Keyword (Ident (Identifier.keywordWord name))-instance ToRadG Void where+instance ToRadG t Void wheretoRadConss _ = absurd-class ToRadFields a where- toRadFields :: a -> [Value]+class ToRadFields t a where+ toRadFields :: a -> [Annotated t ValueF]-instance (ToRad a, ToRadFields as) => ToRadFields (a, as) where+instance (ToRad t a, ToRadFields t as) => ToRadFields t (a, as) wheretoRadFields (x, xs) = toRad x : toRadFields xs-instance ToRadFields () where+instance ToRadFields t () wheretoRadFields () = []--- Generic decoding of Radicle values to Haskell values--fromRadG :: forall a. (HasEot a, FromRadG (Eot a)) => Value -> Either Text a+fromRadG :: forall a t. (CPA t, HasEot a, FromRadG t (Eot a)) => Annotated t ValueF -> Either Text afromRadG v = do(name, args) <- isRadCons v ?? gDecodeErr "expecting constructor"fromEot <$> fromRadConss (constructors (datatype (Proxy :: Proxy a))) name args-class FromRadG a where- fromRadConss :: [Constructor] -> Text -> [Value] -> Either Text a+class FromRadG t a where+ fromRadConss :: [Constructor] -> Text -> [Annotated t ValueF] -> Either Text a-isRadCons :: Value -> Maybe (Text, [Value])-isRadCons (List (Keyword (Ident name) : args)) = pure (name, args)-isRadCons _ = Nothing+isRadCons :: CPA t => Annotated t ValueF -> Maybe (Text, [Annotated t ValueF])+isRadCons (Keyword (Ident name)) = pure (name, [])+isRadCons (Vec (Keyword (Ident name) :<| args)) = pure (name, toList args)+isRadCons _ = NothinggDecodeErr :: Text -> TextgDecodeErr e = "Couldn't generically decode radicle value: " <> e-instance (FromRadFields a, FromRadG b) => FromRadG (Either a b) where+instance (FromRadFields t a, FromRadG t b) => FromRadG t (Either a b) wherefromRadConss (Constructor name fieldMeta : r) name' args = do- if toS name /= name'+ if Identifier.keywordWord (toS name) /= name'then Right <$> fromRadConss r name' argselse Left <$> fromRadFields fieldMeta argsfromRadConss [] _ _ = panic "impossible"-instance FromRadG Void where+instance FromRadG t Void wherefromRadConss _ name _ = Left (gDecodeErr "unknown constructor '" <> name <> "'")-class FromRadFields a where- fromRadFields :: Fields -> [Value] -> Either Text a+class FromRadFields t a where+ fromRadFields :: Fields -> [Annotated t ValueF] -> Either Text a-instance (FromRad a, FromRadFields as) => FromRadFields (a, as) where+instance (CPA t, FromRad t a, FromRadFields t as) => FromRadFields t (a, as) wherefromRadFields fields args = case fields ofNoSelectors _ -> case args ofv:vs -> do@@ -710,5 +716,5 @@ instance (FromRad a, FromRadFields as) => FromRadFields (a, as) where_ -> Left . gDecodeErr $ "expecting a dict"_ -> panic "impossible"-instance FromRadFields () where+instance FromRadFields t () wherefromRadFields _ _ = pure ()diff --git a/src/Radicle/Internal/Crypto.hs b/src/Radicle/Internal/Crypto.hsindex fbc7e37..7d16aaa 100644--- a/src/Radicle/Internal/Crypto.hs+++ b/src/Radicle/Internal/Crypto.hs@@ -21,39 +21,40 @@ import Crypto.PubKey.ECC.Typesimport Crypto.Randomimport Crypto.Random.Types (MonadRandom)+import qualified Radicle.Internal.Annotation as Annimport Radicle.Internal.Corederiving instance Generic PublicKey-instance FromRad PublicKey-instance ToRad PublicKey+instance FromRad Ann.WithPos PublicKey+instance ToRad Ann.WithPos PublicKeyderiving instance Generic PrivateKey-instance FromRad PrivateKey-instance ToRad PrivateKey+instance FromRad Ann.WithPos PrivateKey+instance ToRad Ann.WithPos PrivateKeyderiving instance Generic Curve-instance FromRad Curve-instance ToRad Curve+instance FromRad Ann.WithPos Curve+instance ToRad Ann.WithPos Curvederiving instance Generic Point-instance FromRad Point-instance ToRad Point+instance FromRad Ann.WithPos Point+instance ToRad Ann.WithPos Pointderiving instance Generic CurveBinary-instance FromRad CurveBinary-instance ToRad CurveBinary+instance FromRad Ann.WithPos CurveBinary+instance ToRad Ann.WithPos CurveBinaryderiving instance Generic CurvePrime-instance FromRad CurvePrime-instance ToRad CurvePrime+instance FromRad Ann.WithPos CurvePrime+instance ToRad Ann.WithPos CurvePrimederiving instance Generic CurveCommon-instance FromRad CurveCommon-instance ToRad CurveCommon+instance FromRad Ann.WithPos CurveCommon+instance ToRad Ann.WithPos CurveCommonderiving instance Generic Signature-instance FromRad Signature-instance ToRad Signature+instance FromRad Ann.WithPos Signature+instance ToRad Ann.WithPos Signaturehasher :: Blake2b_256hasher = Blake2b_256diff --git a/src/Radicle/Internal/Identifier.hs b/src/Radicle/Internal/Identifier.hsnew file mode 100644index 0000000..e3b7713--- /dev/null+++ b/src/Radicle/Internal/Identifier.hs@@ -0,0 +1,46 @@+module Radicle.Internal.Identifier where++import qualified Prelude+import Protolude++import Data.Char (isAlphaNum, isLetter)+import qualified Data.Text as T++-- These are made top-level so construction of arbitrary instances that matches+-- parsing is easier. Note that additionally an identifier must not be a valid+-- number (in parsing numbers are parsed first).++-- | A predicate which returns true if the character is valid as the first+-- character of an identifier.+isValidIdentFirst :: Char -> Bool+isValidIdentFirst x = x /= ':' && (isLetter x || x `elem` extendedChar)++-- | A predicate which returns true if the character is valid as the second or+-- later character of an identifier.+isValidIdentRest :: Char -> Bool+isValidIdentRest x = isAlphaNum x || x `elem` extendedChar++extendedChar :: Prelude.String+extendedChar = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<' , '=', '>'+ , '?', '@', '^', '_', '~']++-- | Expands characters into strings that are safe to use in keywords.+keywordChar :: Char -> Text+keywordChar x = case x of+ _ | isValidIdentRest x -> toS [x]+ '{' -> "_lbrace_"+ '}' -> "_rbrace_"+ '[' -> "_lbrack_"+ ']' -> "_rbrack_"+ '(' -> "_lparen_"+ ')' -> "_rparen_"+ '|' -> "_pipe_"+ ' ' -> "_space_"+ '\'' -> "_prime_"+ ',' -> "_comma_"+ '#' -> "_hash_"+ _ -> "_" <> show (fromEnum x) <> "_"++-- | Makes a string safe to use in a keyword.+keywordWord :: Text -> Text+keywordWord x = T.concat $ keywordChar <$> T.unpack xdiff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 7533c4f..07df571 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -2,7 +2,6 @@ module Radicle.Internal.Parse whereimport Protolude hiding (SrcLoc, try)-import Data.Char (isAlphaNum, isLetter)import Data.List.NonEmpty (NonEmpty((:|)))import qualified Data.Map as Mapimport qualified Data.Sequence as Seq@@ -30,6 +29,7 @@ import qualified Text.Megaparsec.Error as Parimport Radicle.Internal.Annotation as Annimport Radicle.Internal.Core+import Radicle.Internal.Identifier-- * The parser@@ -175,22 +175,3 @@ parse file src = docase res ofLeft err -> throwError . toS $ M.parseErrorPretty' src errRight v -> pure v---- ** Valid identifiers--- These are made top-level so construction of arbitrary instances that matches--- parsing is easier. Note that additionally an identifier must not be a valid--- number (in parsing numbers are parsed first).---- | A predicate which returns true if the character is valid as the first--- character of an identifier.-isValidIdentFirst :: Char -> Bool-isValidIdentFirst x = x /= ':' && (isLetter x || x `elem` extendedChar)---- | A predicate which returns true if the character is valid as the second or--- later character of an identifier.-isValidIdentRest :: Char -> Bool-isValidIdentRest x = isAlphaNum x || x `elem` extendedChar--extendedChar :: [Char]-extendedChar = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<' , '=', '>'- , '?', '@', '^', '_', '~']diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hsindex 9bea0f6..bbbeb63 100644--- a/test/spec/Radicle/Internal/Arbitrary.hs+++ b/test/spec/Radicle/Internal/Arbitrary.hs@@ -10,7 +10,8 @@ import Test.QuickCheckimport Test.QuickCheck.Instances ()import Radicle-import Radicle.Internal.Parse (isValidIdentFirst, isValidIdentRest)+import Radicle.Internal.Identifier+ (isValidIdentFirst, isValidIdentRest)import Radicle.Internal.PrimFns (purePrimFns)instance Arbitrary r => Arbitrary (Env r) wherediff --git a/test/spec/Radicle/Internal/Foo.hs b/test/spec/Radicle/Internal/Foo.hsindex 20b56d1..0808950 100644--- a/test/spec/Radicle/Internal/Foo.hs+++ b/test/spec/Radicle/Internal/Foo.hs@@ -9,6 +9,7 @@ import Test.QuickCheckimport Test.QuickCheck.Instances ()import Radicle+import Radicle.Internal.Coredata Foo= FooA { a1 :: Text, a2 :: Scientific }@@ -17,8 +18,8 @@ data Foo| FooDderiving (Eq, Show, Generic)-instance FromRad Foo-instance ToRad Foo+instance CPA t => FromRad t Foo+instance CPA t => ToRad t Fooinstance Arbitrary Foo wherearbitrary = oneof[ FooA <$> arbitrary <*> arbitrarydiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 38e01d6..563edbe 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -680,12 +680,12 @@ test_from_to_radicle =]wheretestForType- :: forall a. (Arbitrary a, Show a, ToRad a, FromRad a, Eq a)+ :: forall a. (Arbitrary a, Show a, ToRad Ann.WithPos a, FromRad Ann.WithPos a, Eq a)=> Proxy a -> TestTreetestForType _ =testProperty "fromRadicle . toRadicle == id" $ \(v :: a ) -> dolet expected = Right v- got = fromRad (toRad v)+ got = fromRad (toRad v :: Value)info = "Expected\n\t" <> show expected<> "\nGot\n\t" <> show gotcounterexample info $ got == expected