@@ -14,6 +14,7 @@ import qualified Data.Map as M
14import Data.Maybe (fromMaybe, mapMaybe)
14import Data.Maybe (fromMaybe, mapMaybe)
15import Data.Ord (comparing)
15import Data.Ord (comparing)
16import Data.Text (Text)
16import Data.Text (Text)
17+import qualified Data.Text as T
17import Language.PureScript.AST
18import Language.PureScript.AST
18import qualified Language.PureScript.Constants as C
19import qualified Language.PureScript.Constants as C
19import Language.PureScript.Crash
20import Language.PureScript.Crash
@@ -22,6 +23,8 @@ import Language.PureScript.Errors
22import Language.PureScript.Externs
23import Language.PureScript.Externs
23import Language.PureScript.Kinds
24import Language.PureScript.Kinds
24import Language.PureScript.Names
25import Language.PureScript.Names
26+import Language.PureScript.Label (Label(..))
27+import Language.PureScript.PSString (mkString, codePoints)
25import Language.PureScript.Types
28import Language.PureScript.Types
26import Language.PureScript.TypeChecker (checkNewtype)
29import Language.PureScript.TypeChecker (checkNewtype)
27import Language.PureScript.TypeChecker.Synonyms (SynonymMap, replaceAllTypeSynonymsM)
30import Language.PureScript.TypeChecker.Synonyms (SynonymMap, replaceAllTypeSynonymsM)
@@ -207,7 +210,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
207 idents <- replicateM (length tys) freshIdent'
210 idents <- replicateM (length tys) freshIdent'
208 tys' <- mapM (replaceAllTypeSynonymsM syns) tys
211 tys' <- mapM (replaceAllTypeSynonymsM syns) tys
209 let caseResult =
212 let caseResult =
210โ App (prodConstructor (Literal . StringLiteral . showQualified runProperName $ Qualified (Just mn) ctorName))
213+ App (prodConstructor (Literal . StringLiteral . mkString . showQualified runProperName $ Qualified (Just mn) ctorName))
211 . Literal . ArrayLiteral
214 . Literal . ArrayLiteral
212 $ zipWith toSpineFun (map (Var . Qualified Nothing) idents) tys'
215 $ zipWith toSpineFun (map (Var . Qualified Nothing) idents) tys'
213 return $ CaseAlternative [ConstructorBinder (Qualified (Just mn) ctorName) (map VarBinder idents)] (Right caseResult)
216 return $ CaseAlternative [ConstructorBinder (Qualified (Just mn) ctorName) (map VarBinder idents)] (Right caseResult)
@@ -216,7 +219,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
216 toSpineFun i r | Just rec <- objectType r =
219 toSpineFun i r | Just rec <- objectType r =
217 lamNull . recordConstructor . Literal . ArrayLiteral
220 lamNull . recordConstructor . Literal . ArrayLiteral
219โ (\(str,typ) ->
222+ (\((Label str),typ) ->
220 Literal $ ObjectLiteral
223 Literal $ ObjectLiteral
221 [ ("recLabel", Literal (StringLiteral str))
224 [ ("recLabel", Literal (StringLiteral str))
222 , ("recValue", toSpineFun (Accessor str i) typ)
225 , ("recValue", toSpineFun (Accessor str i) typ)
@@ -235,7 +238,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
237 (Constructor (Qualified (Just dataGeneric) (ProperName "SigProd")))
240 (Constructor (Qualified (Just dataGeneric) (ProperName "SigProd")))
238โ (Literal (StringLiteral (showQualified runProperName (Qualified (Just mn) name))))
241+ (Literal (StringLiteral $ mkString (showQualified runProperName (Qualified (Just mn) name))))
240 . Literal
243 . Literal
241 . ArrayLiteral
244 . ArrayLiteral
@@ -249,7 +252,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
249 mkProdClause :: (ProperName 'ConstructorName, [Type]) -> Expr
252 mkProdClause :: (ProperName 'ConstructorName, [Type]) -> Expr
250 mkProdClause (ctorName, tys) =
253 mkProdClause (ctorName, tys) =
251 Literal $ ObjectLiteral
254 Literal $ ObjectLiteral
252โ [ ("sigConstructor", Literal (StringLiteral (showQualified runProperName (Qualified (Just mn) ctorName))))
255+ [ ("sigConstructor", Literal (StringLiteral $ mkString (showQualified runProperName (Qualified (Just mn) ctorName))))
253 , ("sigValues", Literal . ArrayLiteral . map (mkProductSignature . instantiate) $ tys)
256 , ("sigValues", Literal . ArrayLiteral . map (mkProductSignature . instantiate) $ tys)
@@ -260,7 +263,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
260 [ ("recLabel", Literal (StringLiteral str))
263 [ ("recLabel", Literal (StringLiteral str))
261 , ("recValue", mkProductSignature typ)
264 , ("recValue", mkProductSignature typ)
263โ | (str, typ) <- decomposeRec rec
266+ | ((Label str), typ) <- decomposeRec rec
265 mkProductSignature typ = lamNull $ App (mkGenVar (Ident C.toSignature))
268 mkProductSignature typ = lamNull $ App (mkGenVar (Ident C.toSignature))
266 (TypedValue False (mkGenVar (Ident "anyProxy")) (proxy typ))
269 (TypedValue False (mkGenVar (Ident "anyProxy")) (proxy typ))
@@ -291,7 +294,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
292 CaseAlternative
295 CaseAlternative
293 [ prodBinder
296 [ prodBinder
294โ [ LiteralBinder (StringLiteral (showQualified runProperName (Qualified (Just mn) ctorName)))
297+ [ LiteralBinder (StringLiteral $ mkString (showQualified runProperName (Qualified (Just mn) ctorName)))
295 , LiteralBinder (ArrayLiteral (map VarBinder idents))
298 , LiteralBinder (ArrayLiteral (map VarBinder idents))
@@ -314,16 +317,16 @@ deriveGeneric mn syns ds tyConNm dargs = do
314 (App e unitVal)
317 (App e unitVal)
315 fromSpineFun e _ = App (mkGenVar (Ident C.fromSpine)) (App e unitVal)
318 fromSpineFun e _ = App (mkGenVar (Ident C.fromSpine)) (App e unitVal)
317โ mkRecCase :: [(Text, Type)] -> CaseAlternative
320+ mkRecCase :: [(Label, Type)] -> CaseAlternative
318 mkRecCase rs =
321 mkRecCase rs =
319 CaseAlternative
322 CaseAlternative
320โ [ recordBinder [ LiteralBinder (ArrayLiteral (map (VarBinder . Ident . fst) rs)) ] ]
323+ [ recordBinder [ LiteralBinder (ArrayLiteral (map (VarBinder . labelToIdent . fst) rs)) ] ]
322โ $ liftApplicative (mkRecFun rs) (map (\(x, y) -> fromSpineFun (Accessor "recValue" (mkVar (Ident x))) y) rs)
325+ $ liftApplicative (mkRecFun rs) (map (\(x, y) -> fromSpineFun (Accessor "recValue" (mkVar $ labelToIdent x)) y) rs)
324โ mkRecFun :: [(Text, Type)] -> Expr
327+ mkRecFun :: [(Label, Type)] -> Expr
325โ mkRecFun xs = mkJust $ foldr (lam . Ident . fst) recLiteral xs
328+ mkRecFun xs = mkJust $ foldr (lam . labelToIdent . fst) recLiteral xs
326โ where recLiteral = Literal . ObjectLiteral $ map (\(s,_) -> (s, mkVar (Ident s))) xs
329+ where recLiteral = Literal . ObjectLiteral $ map (\(l@(Label s), _) -> (s, mkVar $ labelToIdent l)) xs
327 mkFromSpineFunction (PositionedDeclaration _ _ d) = mkFromSpineFunction d
330 mkFromSpineFunction (PositionedDeclaration _ _ d) = mkFromSpineFunction d
328 mkFromSpineFunction _ = internalError "mkFromSpineFunction: expected DataDeclaration"
331 mkFromSpineFunction _ = internalError "mkFromSpineFunction: expected DataDeclaration"
@@ -405,7 +408,7 @@ deriveGenericRep mn syns ds tyConNm tyConArgs repTy = do
405 args' <- mapM (replaceAllTypeSynonymsM syns) args
408 args' <- mapM (replaceAllTypeSynonymsM syns) args
406 (ctorTy, matchProduct, ctorArgs, matchCtor, mkProduct) <- makeProduct args'
409 (ctorTy, matchProduct, ctorArgs, matchCtor, mkProduct) <- makeProduct args'
407 return ( TypeApp (TypeApp (TypeConstructor constructor)
410 return ( TypeApp (TypeApp (TypeConstructor constructor)
408โ (TypeLevelString (runProperName ctorName)))
411+ (TypeLevelString $ mkString (runProperName ctorName)))
410 , CaseAlternative [ ConstructorBinder constructor [matchProduct] ]
413 , CaseAlternative [ ConstructorBinder constructor [matchProduct] ]
411 (Right (foldl App (Constructor (Qualified (Just mn) ctorName)) ctorArgs))
414 (Right (foldl App (Constructor (Qualified (Just mn) ctorName)) ctorArgs))
@@ -430,19 +433,19 @@ deriveGenericRep mn syns ds tyConNm tyConArgs repTy = do
430 makeArg :: Type -> m (Type, Binder, Expr, Binder, Expr)
433 makeArg :: Type -> m (Type, Binder, Expr, Binder, Expr)
431 makeArg arg | Just rec <- objectType arg = do
434 makeArg arg | Just rec <- objectType arg = do
432 let fields = decomposeRec rec
435 let fields = decomposeRec rec
433โ fieldNames <- traverse freshIdent (map fst fields)
436+ fieldNames <- traverse freshIdent (map ((\(Label s) -> either T.pack id $ codePoints s) . fst) fields)
434 pure ( TypeApp (TypeConstructor record)
437 pure ( TypeApp (TypeConstructor record)
435 (foldr1 (\f -> TypeApp (TypeApp (TypeConstructor productName) f))
438 (foldr1 (\f -> TypeApp (TypeApp (TypeConstructor productName) f))
436โ (map (\(name, ty) ->
439+ (map (\((Label name), ty) ->
437 TypeApp (TypeApp (TypeConstructor field) (TypeLevelString name)) ty) fields))
440 TypeApp (TypeApp (TypeConstructor field) (TypeLevelString name)) ty) fields))
438 , ConstructorBinder record
441 , ConstructorBinder record
439 [ foldr1 (\b1 b2 -> ConstructorBinder productName [b1, b2])
442 [ foldr1 (\b1 b2 -> ConstructorBinder productName [b1, b2])
440 (map (\ident -> ConstructorBinder field [VarBinder ident]) fieldNames)
443 (map (\ident -> ConstructorBinder field [VarBinder ident]) fieldNames)
442 , Literal . ObjectLiteral $
445 , Literal . ObjectLiteral $
443โ zipWith (\(name, _) ident -> (name, Var (Qualified Nothing ident))) fields fieldNames
446+ zipWith (\((Label name), _) ident -> (name, Var (Qualified Nothing ident))) fields fieldNames
444 , LiteralBinder . ObjectLiteral $
447 , LiteralBinder . ObjectLiteral $
445โ zipWith (\(name, _) ident -> (name, VarBinder ident)) fields fieldNames
448+ zipWith (\((Label name), _) ident -> (name, VarBinder ident)) fields fieldNames
446 , record' $
449 , record' $
447 foldr1 (\e1 -> App (App (Constructor productName) e1))
450 foldr1 (\e1 -> App (App (Constructor productName) e1))
448 (map (field' . Var . Qualified Nothing) fieldNames)
451 (map (field' . Var . Qualified Nothing) fieldNames)
@@ -574,7 +577,7 @@ deriveEq mn syns ds tyConNm = do
574 toEqTest :: Expr -> Expr -> Type -> Expr
577 toEqTest :: Expr -> Expr -> Type -> Expr
575 toEqTest l r ty | Just rec <- objectType ty =
578 toEqTest l r ty | Just rec <- objectType ty =
577โ . map (\(str, typ) -> toEqTest (Accessor str l) (Accessor str r) typ)
580+ . map (\((Label str), typ) -> toEqTest (Accessor str l) (Accessor str r) typ)
578 $ decomposeRec rec
581 $ decomposeRec rec
579 toEqTest l r _ = preludeEq l r
582 toEqTest l r _ = preludeEq l r
@@ -661,7 +664,7 @@ deriveOrd mn syns ds tyConNm = do
661 toOrdering :: Expr -> Expr -> Type -> Expr
664 toOrdering :: Expr -> Expr -> Type -> Expr
662 toOrdering l r ty | Just rec <- objectType ty =
665 toOrdering l r ty | Just rec <- objectType ty =
663 appendAll
666 appendAll
664โ . map (\(str, typ) -> toOrdering (Accessor str l) (Accessor str r) typ)
667+ . map (\((Label str), typ) -> toOrdering (Accessor str l) (Accessor str r) typ)
665 $ decomposeRec rec
668 $ decomposeRec rec
666 toOrdering l r _ = ordCompare l r
669 toOrdering l r _ = ordCompare l r
@@ -733,11 +736,14 @@ mkVarMn mn = Var . Qualified mn
733mkVar :: Ident -> Expr
736mkVar :: Ident -> Expr
734mkVar = mkVarMn Nothing
737mkVar = mkVarMn Nothing
739+labelToIdent :: Label -> Ident
740+labelToIdent (Label l) = Ident $ either T.pack id $ codePoints l
736objectType :: Type -> Maybe Type
742objectType :: Type -> Maybe Type
737objectType (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Record"))) rec) = Just rec
743objectType (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Record"))) rec) = Just rec
738objectType _ = Nothing
744objectType _ = Nothing
740โdecomposeRec :: Type -> [(Text, Type)]
746+decomposeRec :: Type -> [(Label, Type)]
741decomposeRec = sortBy (comparing fst) . go
747decomposeRec = sortBy (comparing fst) . go
742 where go (RCons str typ typs) = (str, typ) : decomposeRec typs
748 where go (RCons str typ typs) = (str, typ) : decomposeRec typs
743 go _ = []
749 go _ = []
@@ -790,17 +796,17 @@ deriveFunctor mn syns ds tyConNm = do
790 goType recTy | Just row <- objectType recTy =
796 goType recTy | Just row <- objectType recTy =
791 traverse buildUpdate (decomposeRec row) >>= (traverse buildRecord . justUpdates)
797 traverse buildUpdate (decomposeRec row) >>= (traverse buildRecord . justUpdates)
793โ justUpdates :: [Maybe (Text, Expr)] -> Maybe [(Text, Expr)]
799+ justUpdates :: [Maybe (Label, Expr)] -> Maybe [(Label, Expr)]
794 justUpdates = foldMap (fmap return)
800 justUpdates = foldMap (fmap return)
796โ buildUpdate :: (Text, Type) -> m (Maybe (Text, Expr))
802+ buildUpdate :: (Label, Type) -> m (Maybe (Label, Expr))
797 buildUpdate (lbl, ty) = do upd <- goType ty
803 buildUpdate (lbl, ty) = do upd <- goType ty
798 return ((lbl,) <$> upd)
804 return ((lbl,) <$> upd)
800โ buildRecord :: [(Text, Expr)] -> m Expr
806+ buildRecord :: [(Label, Expr)] -> m Expr
801 buildRecord updates = do arg <- freshIdent "o"
807 buildRecord updates = do arg <- freshIdent "o"
802 let argVar = mkVar arg
808 let argVar = mkVar arg
803โ mkAssignment (l, x) = (l, App x (Accessor l argVar))
809+ mkAssignment ((Label l), x) = (l, App x (Accessor l argVar))
804 return (lam arg (ObjectUpdate argVar (mkAssignment <$> updates)))
810 return (lam arg (ObjectUpdate argVar (mkAssignment <$> updates)))
806 -- under a `* -> *`, just assume functor for now
812 -- under a `* -> *`, just assume functor for now