@@ -14,6 +14,7 @@ import qualified Data.Map as M
1414import Data.Maybe (fromMaybe, mapMaybe)
1515import Data.Ord (comparing)
1616import Data.Text (Text)
17+import qualified Data.Text as T
1718import Language.PureScript.AST
1819import qualified Language.PureScript.Constants as C
1920import Language.PureScript.Crash
@@ -22,6 +23,8 @@ import Language.PureScript.Errors
2223import Language.PureScript.Externs
2324import Language.PureScript.Kinds
2425import Language.PureScript.Names
26+import Language.PureScript.Label (Label(..))
27+import Language.PureScript.PSString (mkString, codePoints)
2528import Language.PureScript.Types
2629import Language.PureScript.TypeChecker (checkNewtype)
2730import Language.PureScript.TypeChecker.Synonyms (SynonymMap, replaceAllTypeSynonymsM)
@@ -207,7 +210,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
207210 idents <- replicateM (length tys) freshIdent'
208211 tys' <- mapM (replaceAllTypeSynonymsM syns) tys
209212 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))
211214 . Literal . ArrayLiteral
212215 $ zipWith toSpineFun (map (Var . Qualified Nothing) idents) tys'
213216 return $ CaseAlternative [ConstructorBinder (Qualified (Just mn) ctorName) (map VarBinder idents)] (Right caseResult)
@@ -216,7 +219,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
216219 toSpineFun i r | Just rec <- objectType r =
217220 lamNull . recordConstructor . Literal . ArrayLiteral
218221 . map
219− (\(str,typ) ->
222+ (\((Label str),typ) ->
220223 Literal $ ObjectLiteral
221224 [ ("recLabel", Literal (StringLiteral str))
222225 , ("recValue", toSpineFun (Accessor str i) typ)
@@ -235,7 +238,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
235238 App
236239 (App
237240 (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))))
239242 )
240243 . Literal
241244 . ArrayLiteral
@@ -249,7 +252,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
249252 mkProdClause :: (ProperName 'ConstructorName, [Type]) -> Expr
250253 mkProdClause (ctorName, tys) =
251254 Literal $ ObjectLiteral
252− [ ("sigConstructor", Literal (StringLiteral (showQualified runProperName (Qualified (Just mn) ctorName))))
255+ [ ("sigConstructor", Literal (StringLiteral $ mkString (showQualified runProperName (Qualified (Just mn) ctorName))))
253256 , ("sigValues", Literal . ArrayLiteral . map (mkProductSignature . instantiate) $ tys)
254257 ]
255258
@@ -260,7 +263,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
260263 [ ("recLabel", Literal (StringLiteral str))
261264 , ("recValue", mkProductSignature typ)
262265 ]
263− | (str, typ) <- decomposeRec rec
266+ | ((Label str), typ) <- decomposeRec rec
264267 ]
265268 mkProductSignature typ = lamNull $ App (mkGenVar (Ident C.toSignature))
266269 (TypedValue False (mkGenVar (Ident "anyProxy")) (proxy typ))
@@ -291,7 +294,7 @@ deriveGeneric mn syns ds tyConNm dargs = do
291294 return $
292295 CaseAlternative
293296 [ prodBinder
294− [ LiteralBinder (StringLiteral (showQualified runProperName (Qualified (Just mn) ctorName)))
297+ [ LiteralBinder (StringLiteral $ mkString (showQualified runProperName (Qualified (Just mn) ctorName)))
295298 , LiteralBinder (ArrayLiteral (map VarBinder idents))
296299 ]
297300 ]
@@ -314,16 +317,16 @@ deriveGeneric mn syns ds tyConNm dargs = do
314317 (App e unitVal)
315318 fromSpineFun e _ = App (mkGenVar (Ident C.fromSpine)) (App e unitVal)
316319
317− mkRecCase :: [(Text, Type)] -> CaseAlternative
320+ mkRecCase :: [(Label, Type)] -> CaseAlternative
318321 mkRecCase rs =
319322 CaseAlternative
320− [ recordBinder [ LiteralBinder (ArrayLiteral (map (VarBinder . Ident . fst) rs)) ] ]
323+ [ recordBinder [ LiteralBinder (ArrayLiteral (map (VarBinder . labelToIdent . fst) rs)) ] ]
321324 . Right
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)
323326
324− mkRecFun :: [(Text, Type)] -> Expr
325− mkRecFun xs = mkJust $ foldr (lam . Ident . fst) recLiteral xs
326− where recLiteral = Literal . ObjectLiteral $ map (\(s,_) -> (s, mkVar (Ident s))) xs
327+ mkRecFun :: [(Label, Type)] -> Expr
328+ mkRecFun xs = mkJust $ foldr (lam . labelToIdent . fst) recLiteral xs
329+ where recLiteral = Literal . ObjectLiteral $ map (\(l@(Label s), _) -> (s, mkVar $ labelToIdent l)) xs
327330 mkFromSpineFunction (PositionedDeclaration _ _ d) = mkFromSpineFunction d
328331 mkFromSpineFunction _ = internalError "mkFromSpineFunction: expected DataDeclaration"
329332
@@ -405,7 +408,7 @@ deriveGenericRep mn syns ds tyConNm tyConArgs repTy = do
405408 args' <- mapM (replaceAllTypeSynonymsM syns) args
406409 (ctorTy, matchProduct, ctorArgs, matchCtor, mkProduct) <- makeProduct args'
407410 return ( TypeApp (TypeApp (TypeConstructor constructor)
408− (TypeLevelString (runProperName ctorName)))
411+ (TypeLevelString $ mkString (runProperName ctorName)))
409412 ctorTy
410413 , CaseAlternative [ ConstructorBinder constructor [matchProduct] ]
411414 (Right (foldl App (Constructor (Qualified (Just mn) ctorName)) ctorArgs))
@@ -430,19 +433,19 @@ deriveGenericRep mn syns ds tyConNm tyConArgs repTy = do
430433 makeArg :: Type -> m (Type, Binder, Expr, Binder, Expr)
431434 makeArg arg | Just rec <- objectType arg = do
432435 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)
434437 pure ( TypeApp (TypeConstructor record)
435438 (foldr1 (\f -> TypeApp (TypeApp (TypeConstructor productName) f))
436− (map (\(name, ty) ->
439+ (map (\((Label name), ty) ->
437440 TypeApp (TypeApp (TypeConstructor field) (TypeLevelString name)) ty) fields))
438441 , ConstructorBinder record
439442 [ foldr1 (\b1 b2 -> ConstructorBinder productName [b1, b2])
440443 (map (\ident -> ConstructorBinder field [VarBinder ident]) fieldNames)
441444 ]
442445 , 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
444447 , LiteralBinder . ObjectLiteral $
445− zipWith (\(name, _) ident -> (name, VarBinder ident)) fields fieldNames
448+ zipWith (\((Label name), _) ident -> (name, VarBinder ident)) fields fieldNames
446449 , record' $
447450 foldr1 (\e1 -> App (App (Constructor productName) e1))
448451 (map (field' . Var . Qualified Nothing) fieldNames)
@@ -574,7 +577,7 @@ deriveEq mn syns ds tyConNm = do
574577 toEqTest :: Expr -> Expr -> Type -> Expr
575578 toEqTest l r ty | Just rec <- objectType ty =
576579 conjAll
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)
578581 $ decomposeRec rec
579582 toEqTest l r _ = preludeEq l r
580583
@@ -661,7 +664,7 @@ deriveOrd mn syns ds tyConNm = do
661664 toOrdering :: Expr -> Expr -> Type -> Expr
662665 toOrdering l r ty | Just rec <- objectType ty =
663666 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)
665668 $ decomposeRec rec
666669 toOrdering l r _ = ordCompare l r
667670
@@ -733,11 +736,14 @@ mkVarMn mn = Var . Qualified mn
733736mkVar :: Ident -> Expr
734737mkVar = mkVarMn Nothing
735738
739+labelToIdent :: Label -> Ident
740+labelToIdent (Label l) = Ident $ either T.pack id $ codePoints l
741+
736742objectType :: Type -> Maybe Type
737743objectType (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Record"))) rec) = Just rec
738744objectType _ = Nothing
739745
740−decomposeRec :: Type -> [(Text, Type)]
746+decomposeRec :: Type -> [(Label, Type)]
741747decomposeRec = sortBy (comparing fst) . go
742748 where go (RCons str typ typs) = (str, typ) : decomposeRec typs
743749 go _ = []
@@ -790,17 +796,17 @@ deriveFunctor mn syns ds tyConNm = do
790796 goType recTy | Just row <- objectType recTy =
791797 traverse buildUpdate (decomposeRec row) >>= (traverse buildRecord . justUpdates)
792798 where
793− justUpdates :: [Maybe (Text, Expr)] -> Maybe [(Text, Expr)]
799+ justUpdates :: [Maybe (Label, Expr)] -> Maybe [(Label, Expr)]
794800 justUpdates = foldMap (fmap return)
795801
796− buildUpdate :: (Text, Type) -> m (Maybe (Text, Expr))
802+ buildUpdate :: (Label, Type) -> m (Maybe (Label, Expr))
797803 buildUpdate (lbl, ty) = do upd <- goType ty
798804 return ((lbl,) <$> upd)
799805
800− buildRecord :: [(Text, Expr)] -> m Expr
806+ buildRecord :: [(Label, Expr)] -> m Expr
801807 buildRecord updates = do arg <- freshIdent "o"
802808 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))
804810 return (lam arg (ObjectUpdate argVar (mkAssignment <$> updates)))
805811
806812 -- under a `* -> *`, just assume functor for now