Skip to content

Commit

Permalink
disambiguate toplevel names
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 11, 2024
1 parent 41d4654 commit 821473a
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 18 deletions.
12 changes: 7 additions & 5 deletions src/Juvix/Compiler/Core/Data/InfoTable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ typeName' :: InfoTable -> Symbol -> Text
typeName' tab sym = lookupTabInductiveInfo tab sym ^. inductiveName

identNames' :: InfoTable -> HashSet Text
identNames' tab =
HashSet.fromList $
map (^. identifierName) (HashMap.elems (tab ^. infoIdentifiers))
++ map (^. constructorName) (HashMap.elems (tab ^. infoConstructors))
++ map (^. inductiveName) (HashMap.elems (tab ^. infoInductives))
identNames' = HashSet.fromList . identNamesList'

identNamesList' :: InfoTable -> [Text]
identNamesList' tab =
map (^. identifierName) (HashMap.elems (tab ^. infoIdentifiers))
++ map (^. constructorName) (HashMap.elems (tab ^. infoConstructors))
++ map (^. inductiveName) (HashMap.elems (tab ^. infoInductives))

freshIdentName' :: InfoTable -> Text -> Text
freshIdentName' tab = freshName (identNames' tab)
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Core/Data/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ constrName md tag = lookupConstructorInfo md tag ^. constructorName
identNames :: Module -> HashSet Text
identNames m = identNames' (computeCombinedInfoTable m)

identNamesList :: Module -> [Text]
identNamesList m = identNamesList' (computeCombinedInfoTable m)

freshIdentName :: Module -> Text -> Text
freshIdentName m = freshName (identNames m)

Expand Down
21 changes: 16 additions & 5 deletions src/Juvix/Compiler/Core/Language/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,47 @@ instance Show Symbol where
defaultSymbol :: Word -> Symbol
defaultSymbol = Symbol defaultModuleId

uniqueName :: Text -> Symbol -> Text
uniqueName txt sym = txt <> "_" <> show sym

data TagUser = TagUser
{ _tagUserModuleId :: ModuleId,
_tagUserWord :: Word
}
deriving stock (Eq, Generic, Ord, Show)
deriving stock (Eq, Generic, Ord)

instance Hashable TagUser

instance Serialize TagUser

instance NFData TagUser

instance Pretty TagUser where
pretty TagUser {..} = pretty _tagUserWord <> "@" <> pretty _tagUserModuleId

instance Show TagUser where
show = show . pretty

-- | Tag of a constructor, uniquely identifying it. Tag values are consecutive
-- and separate from symbol IDs. We might need fixed special tags in Core for
-- common "builtin" constructors, e.g., unit, nat, so that the code generator
-- can treat them specially.
data Tag
= BuiltinTag BuiltinDataTag
| UserTag TagUser
deriving stock (Eq, Generic, Ord, Show)
deriving stock (Eq, Generic, Ord)

instance Hashable Tag

instance Serialize Tag

instance NFData Tag

instance Pretty Tag where
pretty = \case
BuiltinTag b -> pretty b
UserTag u -> pretty u

instance Show Tag where
show = show . pretty

isBuiltinTag :: Tag -> Bool
isBuiltinTag = \case
BuiltinTag {} -> True
Expand Down
17 changes: 16 additions & 1 deletion src/Juvix/Compiler/Core/Language/Builtins.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module Juvix.Compiler.Core.Language.Builtins where

import GHC.Show qualified as Show
import Juvix.Extra.Serialize
import Juvix.Extra.Strings qualified as Str
import Juvix.Prelude
import Prettyprinter

-- Builtin operations which the evaluator and the code generator treat
-- specially and non-uniformly.
Expand Down Expand Up @@ -56,14 +59,26 @@ data BuiltinDataTag
| TagBind
| TagWrite
| TagReadLn
deriving stock (Eq, Generic, Ord, Show)
deriving stock (Eq, Generic, Ord)

instance Hashable BuiltinDataTag

instance Serialize BuiltinDataTag

instance NFData BuiltinDataTag

instance Pretty BuiltinDataTag where
pretty = \case
TagTrue -> Str.true_
TagFalse -> Str.false_
TagReturn -> Str.return
TagBind -> Str.bind
TagWrite -> Str.write
TagReadLn -> Str.readLn

instance Show BuiltinDataTag where
show = show . pretty

builtinOpArgsNum :: BuiltinOp -> Int
builtinOpArgsNum = \case
OpIntAdd -> 2
Expand Down
27 changes: 25 additions & 2 deletions src/Juvix/Compiler/Core/Transformation/DisambiguateNames.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ disambiguateNodeNames md = disambiguateNodeNames' disambiguate md
names :: HashSet Text
names = identNames md

disambiguateTopNames :: Module -> Module
disambiguateTopNames md =
mapInductives (\i -> over inductiveName (renameDuplicated (i ^. inductiveSymbol)) i)
. mapConstructors (\i -> over constructorName (renameDuplicated (i ^. constructorTag)) i)
. mapIdents (\i -> over identifierName (renameDuplicated (i ^. identifierSymbol)) i)
$ md
where
duplicatedNames :: HashSet Text
duplicatedNames =
HashSet.fromList
. map head
. filter (\x -> length x > 1)
. NonEmpty.group
. sort
. identNamesList
$ md

renameDuplicated :: (Show a) => a -> Text -> Text
renameDuplicated sym name
| HashSet.member name duplicatedNames = uniqueName name sym
| otherwise = name

setArgNames :: Module -> Symbol -> Node -> Node
setArgNames md sym node = reLambdas lhs' body
where
Expand All @@ -135,8 +157,9 @@ setArgNames md sym node = reLambdas lhs' body

disambiguateNames :: Module -> Module
disambiguateNames md =
let md' = mapT (setArgNames md) md
in mapAllNodes (disambiguateNodeNames md') md'
let md1 = disambiguateTopNames md
md2 = mapT (setArgNames md1) md1
in mapAllNodes (disambiguateNodeNames md2) md2

disambiguateNames' :: InfoTable -> InfoTable
disambiguateNames' = withInfoTable disambiguateNames
9 changes: 8 additions & 1 deletion src/Juvix/Compiler/Tree/Pretty/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ quoteName txt =
[ ("$", "__dollar__"),
(":", "__colon__"),
("@", "__at__"),
(".", "__dot__"),
(",", "__comma__"),
(";", "__semicolon__"),
("arg", "__arg__"),
("tmp", "__tmp__")
("tmp", "__tmp__"),
("sub", "__sub__"),
("add", "__add__"),
("mul", "__mul__"),
("div", "__div__")
]

quoteFunName :: Text -> Text
Expand Down
8 changes: 4 additions & 4 deletions src/Juvix/Compiler/Tree/Translation/FromSource/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ declareBuiltins = do
sym <- lift $ freshSymbol' @t @e
let tyio = mkTypeInductive sym
constrs =
[ createBuiltinConstr sym TagReturn "return" (mkTypeFun [TyDynamic] tyio) i,
createBuiltinConstr sym TagBind "bind" (mkTypeFun [tyio, mkTypeFun [TyDynamic] tyio] tyio) i,
createBuiltinConstr sym TagWrite "write" (mkTypeFun [TyDynamic] tyio) i,
createBuiltinConstr sym TagReadLn "readLn" tyio i
[ createBuiltinConstr sym TagReturn (show TagReturn) (mkTypeFun [TyDynamic] tyio) i,
createBuiltinConstr sym TagBind (show TagBind) (mkTypeFun [tyio, mkTypeFun [TyDynamic] tyio] tyio) i,
createBuiltinConstr sym TagWrite (show TagWrite) (mkTypeFun [TyDynamic] tyio) i,
createBuiltinConstr sym TagReadLn (show TagReadLn) tyio i
]
lift $
registerInductive' @t @e
Expand Down
12 changes: 12 additions & 0 deletions src/Juvix/Extra/Strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,18 @@ false_ = "false"
default_ :: (IsString s) => s
default_ = "default"

return :: (IsString s) => s
return = "return"

bind :: (IsString s) => s
bind = "bind"

write :: (IsString s) => s
write = "write"

readLn :: (IsString s) => s
readLn = "readLn"

plus :: (IsString s) => s
plus = "+"

Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Prelude/Base/Foundation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ toUpperFirst :: String -> String
toUpperFirst [] = []
toUpperFirst (x : xs) = Char.toUpper x : xs

uniqueName :: (Show a) => Text -> a -> Text
uniqueName txt sym = txt <> "_" <> show sym

--------------------------------------------------------------------------------
-- Text
--------------------------------------------------------------------------------
Expand Down

0 comments on commit 821473a

Please sign in to comment.