Skip to content

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.hs
index bb3df9b..6a4f2bb 100644
--- a/src/Radicle/Internal.hs
+++ b/src/Radicle/Internal.hs
@@ -13,6 +13,7 @@ import Radicle.Internal.Annotation as X
import Radicle.Internal.CLI as X
import Radicle.Internal.Core as X
import Radicle.Internal.Effects as X
+import Radicle.Internal.Identifier as X
import Radicle.Internal.Interpret as X
import Radicle.Internal.Parse as X
import Radicle.Internal.Pretty as X
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 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 Par
import Radicle.Internal.Annotation (Annotated)
import qualified Radicle.Internal.Annotation as Ann
+import qualified Radicle.Internal.Identifier as Identifier
import 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)
where
List = 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 ValueF
pattern Vec vs <- (Ann.match -> VecF vs)
where
Vec = 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 a
fromRad = fromRadG
-instance FromRad () where
+instance CPA t => FromRad t () where
fromRad (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) where
fromRad (Vec (x :<| y :<| Seq.Empty)) = (,) <$> fromRad x <*> fromRad y
fromRad _ = Left "Expecting a vector of length 2"
-instance FromRad Value where
+instance FromRad Ann.WithPos Value where
fromRad = pure
-instance FromRad Scientific where
+instance CPA t => FromRad t Scientific where
fromRad x = case x of
Number n -> pure n
_ -> Left "Expecting number"
-instance FromRad Integer where
+instance CPA t => FromRad t Integer where
fromRad = \case
Number s -> case floatingOrInteger s of
Left (_ :: Double) -> Left "Expecting whole number"
Right i -> pure i
_ -> Left "Expecting number"
-instance FromRad Text where
+instance CPA t => FromRad t Text where
fromRad x = case x of
String n -> pure n
_ -> Left "Expecting string"
-instance FromRad a => FromRad [a] where
+instance (CPA t, FromRad t a) => FromRad t [a] where
fromRad x = case x of
List xs -> traverse fromRad xs
Vec xs -> traverse fromRad (toList xs)
_ -> Left "Expecting list"
-instance FromRad (Env Value) where
+instance FromRad Ann.WithPos (Env Value) where
fromRad x = case x of
Dict d -> fmap (Env . Map.fromList)
$ forM (Map.toList d) $ \(k, v) -> case k of
Atom i -> pure (i, v)
k' -> Left $ "Expecting atom keys. Got: " <> show k'
_ -> Left "Expecting dict"
-instance FromRad (Bindings ()) where
+instance FromRad Ann.WithPos (Bindings ()) where
fromRad x = case x of
Dict d -> do
env' <- 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 ValueF
toRad = toRadG
-instance ToRad () where
+instance CPA t => ToRad t () where
toRad _ = Vec Empty
-instance (ToRad a, ToRad b) => ToRad (a,b) where
+instance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) where
toRad (x,y) = Vec $ toRad x :<| toRad y :<| Empty
-instance ToRad Int where
+instance CPA t => ToRad t Int where
toRad = Number . fromIntegral
-instance ToRad Integer where
+instance CPA t => ToRad t Integer where
toRad = Number . fromIntegral
-instance ToRad Scientific where
+instance CPA t => ToRad t Scientific where
toRad = Number
-instance ToRad Text where
+instance CPA t => ToRad t Text where
toRad = String
-instance ToRad a => ToRad [a] where
+instance (CPA t, ToRad t a) => ToRad t [a] where
toRad 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) where
toRad xs = Dict $ Map.mapKeys String $ toRad <$> xs
-instance ToRad (Env Value) where
+instance ToRad Ann.WithPos (Env Value) where
toRad x = Dict . Map.mapKeys Atom $ fromEnv x
-instance ToRad (Bindings m) where
+instance ToRad Ann.WithPos (Bindings m) where
toRad 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] -> Value
list 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 ValueF
toRadG 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) where
toRadConss (Constructor name fieldMeta : _) (Left fields) =
case fieldMeta of
Selectors names ->
@@ -648,52 +651,55 @@ instance (ToRadFields a, ToRadG b) => ToRadG (Either a b) where
toRadConss (_ : r) (Right next) = toRadConss r next
toRadConss [] _ = 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 where
toRadConss _ = 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) where
toRadFields (x, xs) = toRad x : toRadFields xs
-instance ToRadFields () where
+instance ToRadFields t () where
toRadFields () = []
--- 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 a
fromRadG 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 _ = Nothing
gDecodeErr :: Text -> Text
gDecodeErr 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) where
fromRadConss (Constructor name fieldMeta : r) name' args = do
- if toS name /= name'
+ if Identifier.keywordWord (toS name) /= name'
then Right <$> fromRadConss r name' args
else Left <$> fromRadFields fieldMeta args
fromRadConss [] _ _ = panic "impossible"
-instance FromRadG Void where
+instance FromRadG t Void where
fromRadConss _ 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) where
fromRadFields fields args = case fields of
NoSelectors _ -> case args of
v: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 () where
fromRadFields _ _ = pure ()
diff --git a/src/Radicle/Internal/Crypto.hs b/src/Radicle/Internal/Crypto.hs
index fbc7e37..7d16aaa 100644
--- a/src/Radicle/Internal/Crypto.hs
+++ b/src/Radicle/Internal/Crypto.hs
@@ -21,39 +21,40 @@ import Crypto.PubKey.ECC.Types
import Crypto.Random
import Crypto.Random.Types (MonadRandom)
+import qualified Radicle.Internal.Annotation as Ann
import Radicle.Internal.Core
deriving instance Generic PublicKey
-instance FromRad PublicKey
-instance ToRad PublicKey
+instance FromRad Ann.WithPos PublicKey
+instance ToRad Ann.WithPos PublicKey
deriving instance Generic PrivateKey
-instance FromRad PrivateKey
-instance ToRad PrivateKey
+instance FromRad Ann.WithPos PrivateKey
+instance ToRad Ann.WithPos PrivateKey
deriving instance Generic Curve
-instance FromRad Curve
-instance ToRad Curve
+instance FromRad Ann.WithPos Curve
+instance ToRad Ann.WithPos Curve
deriving instance Generic Point
-instance FromRad Point
-instance ToRad Point
+instance FromRad Ann.WithPos Point
+instance ToRad Ann.WithPos Point
deriving instance Generic CurveBinary
-instance FromRad CurveBinary
-instance ToRad CurveBinary
+instance FromRad Ann.WithPos CurveBinary
+instance ToRad Ann.WithPos CurveBinary
deriving instance Generic CurvePrime
-instance FromRad CurvePrime
-instance ToRad CurvePrime
+instance FromRad Ann.WithPos CurvePrime
+instance ToRad Ann.WithPos CurvePrime
deriving instance Generic CurveCommon
-instance FromRad CurveCommon
-instance ToRad CurveCommon
+instance FromRad Ann.WithPos CurveCommon
+instance ToRad Ann.WithPos CurveCommon
deriving instance Generic Signature
-instance FromRad Signature
-instance ToRad Signature
+instance FromRad Ann.WithPos Signature
+instance ToRad Ann.WithPos Signature
hasher :: Blake2b_256
hasher = Blake2b_256
diff --git a/src/Radicle/Internal/Identifier.hs b/src/Radicle/Internal/Identifier.hs
new file mode 100644
index 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 x
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 7533c4f..07df571 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -2,7 +2,6 @@ module Radicle.Internal.Parse where
import Protolude hiding (SrcLoc, try)
-import Data.Char (isAlphaNum, isLetter)
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.Map as Map
import qualified Data.Sequence as Seq
@@ -30,6 +29,7 @@ import qualified Text.Megaparsec.Error as Par
import Radicle.Internal.Annotation as Ann
import Radicle.Internal.Core
+import Radicle.Internal.Identifier
-- * The parser
@@ -175,22 +175,3 @@ parse file src = do
case res of
Left err -> throwError . toS $ M.parseErrorPretty' src err
Right 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.hs
index 9bea0f6..bbbeb63 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -10,7 +10,8 @@ import Test.QuickCheck
import 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) where
diff --git a/test/spec/Radicle/Internal/Foo.hs b/test/spec/Radicle/Internal/Foo.hs
index 20b56d1..0808950 100644
--- a/test/spec/Radicle/Internal/Foo.hs
+++ b/test/spec/Radicle/Internal/Foo.hs
@@ -9,6 +9,7 @@ import Test.QuickCheck
import Test.QuickCheck.Instances ()
import Radicle
+import Radicle.Internal.Core
data Foo
= FooA { a1 :: Text, a2 :: Scientific }
@@ -17,8 +18,8 @@ data Foo
| FooD
deriving (Eq, Show, Generic)
-instance FromRad Foo
-instance ToRad Foo
+instance CPA t => FromRad t Foo
+instance CPA t => ToRad t Foo
instance Arbitrary Foo where
arbitrary = oneof
[ FooA <$> arbitrary <*> arbitrary
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 38e01d6..563edbe 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -680,12 +680,12 @@ test_from_to_radicle =
]
where
testForType
- :: 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 -> TestTree
testForType _ =
testProperty "fromRadicle . toRadicle == id" $ \(v :: a ) -> do
let expected = Right v
- got = fromRad (toRad v)
+ got = fromRad (toRad v :: Value)
info = "Expected\n\t" <> show expected
<> "\nGot\n\t" <> show got
counterexample info $ got == expected