22module Agda2Lambox.Compile.Utils
33 ( modNameToModPath
44 , qnameToKName
5+ , qnameToName
56 , dataOrRecDefMutuals
67 , dataOrRecMutuals
78 , toInductive
89 , toConApp
10+ , sanitize
911 , MayBeLogical (isLogical )
1012 ) where
1113
@@ -27,6 +29,7 @@ import LambdaBox qualified as LBox
2729import Agda.TypeChecking.Substitute (TelV (TelV ))
2830import Agda.TypeChecking.Telescope (telView )
2931import Agda.Utils.Monad (orM )
32+ import Data.Char (isLower , isUpper , GeneralCategory (DecimalNumber ), generalCategory )
3033
3134
3235-- | Convert and Agda module name to its "equivalent" λ□ module path.
@@ -39,7 +42,10 @@ qnameToKName :: QName -> LBox.KerName
3942qnameToKName qn =
4043 LBox. KerName
4144 (modNameToModPath $ qnameModule qn)
42- (prettyShow $ qnameName qn)
45+ (sanitize $ prettyShow $ qnameName qn)
46+
47+ qnameToName :: QName -> LBox. Name
48+ qnameToName q = LBox. Named (sanitize $ prettyShow $ qnameName q)
4349
4450dataOrRecDefMutuals :: Definition -> TCM [QName ]
4551dataOrRecDefMutuals d = do
@@ -106,6 +112,21 @@ instance MayBeLogical Type where
106112 LevelUniv {} -> True -- LevelUniv
107113 _ -> False
108114
115+
116+ -- | Sanitize an agda name to something without unicode.
117+ -- Must be injective.
118+ -- We may require a smarter transformation later on for other targets.
119+ sanitize :: String -> String
120+ sanitize s = concatMap encode s
121+ where
122+ encode ' $' = " $$"
123+ encode c
124+ | isLower c || isUpper c || c == ' _' ||
125+ generalCategory c == DecimalNumber =
126+ [c]
127+ | otherwise = " $" ++ show (fromEnum c)
128+
129+
109130-- | Additionally, we consider erased domains logical.
110131instance MayBeLogical a => MayBeLogical (Dom a ) where
111132 isLogical dom | not (usableModality dom) = pure True
0 commit comments