Skip to content

Commit cafa94f

Browse files
committed
sanitization test
1 parent a8317f9 commit cafa94f

File tree

8 files changed

+57
-21
lines changed

8 files changed

+57
-21
lines changed

agda2lambox.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ executable agda2lambox
4949
directory,
5050
filepath,
5151
text,
52-
mtl
52+
mtl,
53+
unicode-data
5354
default-language: Haskell2010
5455
default-extensions:
5556
LambdaCase RecordWildCards PatternSynonyms TupleSections
File renamed without changes.
File renamed without changes.

src/Agda2Lambox/Compile.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ compileDefinition target defn@Defn{..} = setCurrentRange defName do
116116

117117
-- otherwise, compiling it as an axiom
118118
_ -> do
119-
reportSDoc "agda2lambox.compile" 5 $
120-
"Compiling it to an axiom."
119+
reportSDoc "agda2lambox.compile" 5 $ "Compiling it to an axiom."
121120
typ <- whenTyped target $ compileTopLevelType defType
122121
pure $ Just $ ConstantDecl $ ConstantBody typ Nothing
123122

src/Agda2Lambox/Compile/Inductive.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Agda.Syntax.Common (Arg)
2727
import Agda.Syntax.Internal
2828
import Agda.Utils.Monad ( unlessM )
2929

30-
import Agda.Utils ( isDataOrRecDef, isArity )
30+
import Agda.Utils
3131
import Agda2Lambox.Compile.Target
3232
import Agda2Lambox.Compile.Utils
3333
import Agda2Lambox.Compile.Monad
@@ -54,7 +54,7 @@ compileInductive t defn@Defn{defName} = do
5454
let items = fromMaybe (NEL.singleton defName) $ NEL.nonEmpty mutuals
5555

5656
-- ensure that all mutuals get compiled, eventually
57-
mapM requireDef items
57+
mapM_ requireDef items
5858

5959
{- also note that we assume the list of mutuals will be the same
6060
for every record/datatype in the list (especially the order),
@@ -123,9 +123,9 @@ actuallyConvertInductive t defn = do
123123
let domType = unDom pdom
124124
isParamLogical <- liftTCM $ isLogical pdom
125125
isParamArity <- liftTCM $ isArity domType
126-
let isParamSort = isJust $ isSort $ unEl $ domType
126+
let isParamSort = isJust $ isSort $ unEl domType
127127
pure LBox.TypeVarInfo
128-
{ tvarName = maybe LBox.Anon (LBox.Named . sanitize . prettyShow) $ domName pdom
128+
{ tvarName = domToName pdom
129129
, tvarIsLogical = isParamLogical
130130
, tvarIsArity = isParamArity
131131
, tvarIsSort = isParamSort
@@ -138,18 +138,18 @@ actuallyConvertInductive t defn = do
138138
RecordCon _ _ arity _ -> arity
139139

140140
conTypeInfo <- whenTyped t do
141-
conType <- liftTCM $ (`piApplyM` pvars) =<< defType <$> getConstInfo cname
141+
conType <- liftTCM $ (`piApplyM` pvars) . defType =<< getConstInfo cname
142142
conTel <- toList . theTel <$> telView conType
143143
compileArgs indPars conTel
144144

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

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

src/Agda2Lambox/Compile/Type.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import Data.Function ( applyWhen )
1818
import Agda.Syntax.Common ( unArg, Arg (Arg) )
1919
import Agda.Syntax.Internal
2020
import Agda.TypeChecking.Monad.Base ( TCM, MonadTCM (liftTCM), Definition(..))
21-
import Agda.Syntax.Common.Pretty ( prettyShow )
2221
import Agda.Utils.Monad (ifM)
2322

2423
import qualified LambdaBox as LBox
25-
import Agda2Lambox.Compile.Utils ( qnameToKName, isLogical, toInductive )
24+
import Agda2Lambox.Compile.Utils
2625
import Agda2Lambox.Compile.Monad
2726
import Agda.Compiler.Backend (HasConstInfo(getConstInfo), Definition(Defn), AddContext (addContext))
2827
import Agda.Utils (isDataOrRecDef, getInductiveParams, isArity, maybeUnfoldCopy)
@@ -91,7 +90,7 @@ compileArgs tvars = runC tvars . compileArgsC
9190
compileArgsC :: [Dom Type] -> C [(LBox.Name, LBox.Type)]
9291
compileArgsC [] = pure []
9392
compileArgsC (dom:args) = do
94-
let name = maybe LBox.Anon (LBox.Named . prettyShow) $ domName dom
93+
let name = domToName dom
9594
typ <- ifM (liftTCM $ isLogical dom) (pure LBox.TBox) (compileTypeC $ unDom dom)
9695
((name, typ):) <$> underBinder dom (compileArgsC args)
9796

@@ -172,7 +171,7 @@ compileTypeTerm = \case
172171

173172
else do
174173
(vars, codomType') <- underTypeVar dom $ compileTopLevelTypeC codomType
175-
let name = maybe LBox.Anon (LBox.Named . prettyShow) $ domName dom
174+
let name = domToName dom
176175
pure (name : vars, LBox.TArr LBox.TBox codomType')
177176

178177
_ -> pure ([], LBox.TAny)

src/Agda2Lambox/Compile/Utils.hs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Agda2Lambox.Compile.Utils
44
( modNameToModPath
55
, qnameToKName
66
, qnameToName
7+
, qnameToIdent
8+
, domToName
79
, dataOrRecDefMutuals
810
, dataOrRecMutuals
911
, toInductive
@@ -18,6 +20,7 @@ import Data.Char
1820
import Data.List ( elemIndex, foldl' )
1921
import Data.Maybe ( fromMaybe, listToMaybe, isJust )
2022

23+
import Unicode.Char.Identifiers
2124
import Agda.Compiler.Backend
2225
import Agda.Syntax.Internal
2326
import Agda.Syntax.Abstract.Name
@@ -34,22 +37,26 @@ import Agda.Utils.Monad (orM)
3437
import Agda2Lambox.Compile.Monad
3538
import LambdaBox qualified as LBox
3639

37-
3840
-- | Convert and Agda module name to its "equivalent" λ□ module path.
3941
modNameToModPath :: ModuleName -> LBox.ModPath
4042
modNameToModPath =
4143
LBox.MPFile . map (sanitize . prettyShow) . mnameToList
4244

43-
4445
-- | Convert and Agda definition name to a λ□ kernel name.
4546
qnameToKName :: QName -> LBox.KerName
4647
qnameToKName qn =
4748
LBox.KerName
4849
(modNameToModPath $ qnameModule qn)
49-
(sanitize $ prettyShow $ qnameName qn)
50+
(qnameToIdent qn)
51+
52+
domToName :: Dom' a b -> LBox.Name
53+
domToName = maybe LBox.Anon (LBox.Named . sanitize . prettyShow) . domName
54+
55+
qnameToIdent :: QName -> LBox.Ident
56+
qnameToIdent = sanitize . prettyShow . qnameName
5057

5158
qnameToName :: QName -> LBox.Name
52-
qnameToName q = LBox.Named (sanitize $ prettyShow $ qnameName q)
59+
qnameToName = LBox.Named . qnameToIdent
5360

5461
dataOrRecDefMutuals :: Definition -> TCM [QName]
5562
dataOrRecDefMutuals d = do
@@ -135,7 +142,21 @@ instance MayBeLogical a => MayBeLogical (Arg a) where
135142
-- Must be injective.
136143
-- We may require a smarter transformation later on for other targets.
137144
sanitize :: String -> String
138-
sanitize s = concatMap encode s
145+
sanitize xs | xs `elem` ["true", "false"] = "r#" ++ xs
146+
sanitize [] = []
147+
sanitize (x:xs) = toXIDStart x <> concatMap toXIDContinue xs
148+
where
149+
toXIDStart, toXIDContinue :: Char -> String
150+
toXIDStart c
151+
| isXIDStart c || c == '_' = [c]
152+
| isXIDContinue c = "_" <> [c]
153+
| otherwise = toXIDContinue c
154+
155+
toXIDContinue c
156+
| isXIDContinue c = [c]
157+
| otherwise = "y" <> show (ord c) <> "y"
158+
{-
159+
sanitize q = "agda" ++ concatMap encode q
139160
where
140161
encode '$' = "$$"
141162
encode c
@@ -144,5 +165,21 @@ sanitize s = concatMap encode s
144165
|| isUpper c
145166
|| c == '_'
146167
|| generalCategory c == DecimalNumber = [c]
147-
| otherwise = "$" ++ show (fromEnum c)
168+
| otherwise = "$" ++ show (fromEnum c) ++ "$"
148169
170+
-- | Sanitize an agda name to something Rust-compliant.
171+
-- Must be injective.
172+
sanitize :: String -> String
173+
sanitize [] = []
174+
sanitize (c:cs) = toXIDStart c <> concatMap toXIDContinue cs
175+
where
176+
toXIDStart, toXIDContinue :: Char -> String
177+
toXIDStart c
178+
| isXIDStart c || c == '_' = [c]
179+
| isXIDContinue c = "_" <> [c]
180+
| otherwise = toXIDContinue c
181+
182+
toXIDContinue c
183+
| isXIDContinue c = [c]
184+
| otherwise = "Ֆ" <> show (ord c) <> "Ֆ"
185+
-}

src/LambdaBox/Names.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data Name = Anon | Named Ident
4040
instance Pretty ModPath where
4141
pretty = \case
4242
MPFile dp -> cat $ punctuate "." (map pretty dp)
43-
MPBound dp id i -> "MPBound??" -- don't know what this corresponds to
43+
MPBound dp id i -> "MPBound??" -- NOTE: don't know what this corresponds to
4444
MPDot mp i -> pretty mp <> "." <> pretty i
4545

4646
instance Pretty KerName where

0 commit comments

Comments
 (0)