Skip to content

Commit e5fd026

Browse files
committed
add basic name sanitization
1 parent 56dbdf8 commit e5fd026

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/Agda2Lambox/Compile/Function.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ compileFunction (t :: Target t) defn@Defn{defType} = do
8787
projTel <- telViewUpTo recPars defType
8888
projType <- theCore <$> telViewUpTo recPars defType
8989
let names = map domName $ toList $ theTel projTel
90-
pnames = map (maybe LBox.Anon (LBox.Named . prettyShow)) names
90+
pnames = map (maybe LBox.Anon (LBox.Named . sanitize . prettyShow)) names
9191
(pnames,) <$> compileType recPars projType
9292

9393
-- TODO(flupe): ^ take care of projection-like functions
@@ -108,7 +108,7 @@ compileFunction (t :: Target t) defn@Defn{defType} = do
108108
forM mdefs \def@Defn{defName} -> do
109109
body <- compileFunctionBody mnames def
110110
return LBox.Def
111-
{ dName = LBox.Named $ prettyShow defName
111+
{ dName = qnameToName defName
112112
, dBody = body
113113
, dArgs = 0
114114
}

src/Agda2Lambox/Compile/Inductive.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ actuallyConvertInductive t defn = do
125125
isParamArity <- liftTCM $ isArity domType
126126
let isParamSort = isJust $ isSort $ unEl $ domType
127127
pure LBox.TypeVarInfo
128-
{ tvarName = maybe LBox.Anon (LBox.Named . prettyShow) $ domName pdom
128+
{ tvarName = maybe LBox.Anon (LBox.Named . sanitize . prettyShow) $ domName pdom
129129
, tvarIsLogical = isParamLogical
130130
, tvarIsArity = isParamArity
131131
, tvarIsSort = isParamSort
@@ -143,13 +143,13 @@ actuallyConvertInductive t defn = do
143143
compileArgs indPars conTel
144144

145145
pure LBox.Constructor
146-
{ cstrName = prettyShow $ qnameName cname
146+
{ cstrName = sanitize $ prettyShow $ qnameName cname
147147
, cstrArgs = arity
148148
, cstrTypes = conTypeInfo
149149
}
150150

151151
pure LBox.OneInductive
152-
{ indName = prettyShow $ qnameName indName
152+
{ indName = sanitize $ prettyShow $ qnameName indName
153153
, indPropositional = False -- TODO(flupe)
154154
, indKElim = LBox.IntoAny -- TODO(flupe)
155155
, indCtors = ctors

src/Agda2Lambox/Compile/Utils.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
module 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
2729
import Agda.TypeChecking.Substitute (TelV(TelV))
2830
import Agda.TypeChecking.Telescope (telView)
2931
import 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
3942
qnameToKName 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

4450
dataOrRecDefMutuals :: Definition -> TCM [QName]
4551
dataOrRecDefMutuals 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.
110131
instance MayBeLogical a => MayBeLogical (Dom a) where
111132
isLogical dom | not (usableModality dom) = pure True

0 commit comments

Comments
 (0)