0 | module Core.Name.Namespace
4 | import Decidable.Equality
5 | import Libraries.Data.String.Extra
6 | import Libraries.Text.PrettyPrint.Prettyprinter
7 | import Libraries.Utils.Path
21 | data Namespace : Type where
22 | MkNS : List String -> Namespace
28 | data ModuleIdent : Type where
29 | MkMI : List String -> ModuleIdent
38 | miAsNamespace : ModuleIdent -> Namespace
39 | miAsNamespace (MkMI mi) = MkNS mi
46 | nsAsModuleIdent : Namespace -> ModuleIdent
47 | nsAsModuleIdent (MkNS ns) = MkMI ns
54 | mkNamespacedIdent : String -> (Maybe Namespace, String)
55 | mkNamespacedIdent str
56 | = let nns = reverse (split (== '.') str)
60 | [] => (Nothing, name)
61 | _ => (Just (MkNS ns), name)
64 | mkNestedNamespace : Maybe Namespace -> String -> Namespace
65 | mkNestedNamespace Nothing n = MkNS [n]
66 | mkNestedNamespace (Just (MkNS ns)) n = MkNS (n :: ns)
69 | mkNamespace : String -> Namespace
70 | mkNamespace "" = MkNS []
71 | mkNamespace str = uncurry mkNestedNamespace (mkNamespacedIdent str)
74 | mkModuleIdent : Maybe Namespace -> String -> ModuleIdent
75 | mkModuleIdent Nothing n = MkMI [n]
76 | mkModuleIdent (Just (MkNS ns)) n = MkMI (n :: ns)
85 | (<.>) : (existing, local : Namespace) -> Namespace
86 | (MkNS existing) <.> (MkNS local)
89 | = MkNS (local ++ existing)
92 | replace : (old : ModuleIdent) -> (new, ns : Namespace) -> Namespace
93 | replace (MkMI old) (MkNS new) (MkNS ns) = MkNS (go ns) where
95 | go : List String -> List String
98 | = if old == (m :: ms)
104 | unsafeUnfoldNamespace : Namespace -> List String
105 | unsafeUnfoldNamespace (MkNS ns) = ns
108 | unsafeFoldNamespace : List String -> Namespace
109 | unsafeFoldNamespace = MkNS
112 | unsafeUnfoldModuleIdent : ModuleIdent -> List String
113 | unsafeUnfoldModuleIdent (MkMI ns) = ns
116 | unsafeFoldModuleIdent : List String -> ModuleIdent
117 | unsafeFoldModuleIdent = MkMI
119 | namespace ModuleIdent
122 | toPath : ModuleIdent -> String
123 | toPath = joinPath . reverse . unsafeUnfoldModuleIdent
126 | parent : ModuleIdent -> Maybe ModuleIdent
127 | parent (MkMI (_::rest)) = Just $
MkMI rest
143 | allParents : Namespace -> List Namespace
144 | allParents (MkNS ns) = go ns where
146 | go : List String -> List Namespace
148 | go (n :: ns) = MkNS (n :: ns) :: go ns
154 | isParentOf : (given, candidate : Namespace) -> Bool
155 | isParentOf (MkNS ms) (MkNS ns)
168 | isApproximationOf : (given, candidate : Namespace) -> Bool
169 | isApproximationOf (MkNS ms) (MkNS ns)
178 | isInPathOf : (i : String) -> (candidate : Namespace) -> Bool
179 | isInPathOf i (MkNS ns) = i `elem` ns
187 | (MkNS ms) == (MkNS ns) = ms == ns
190 | Eq ModuleIdent where
191 | (MkMI ms) == (MkMI ns) = ms == ns
194 | Ord Namespace where
195 | compare (MkNS ms) (MkNS ns) = compare ms ns
198 | Ord ModuleIdent where
199 | compare (MkMI ms) (MkMI ns) = compare ms ns
201 | Injective MkNS where
202 | injective Refl = Refl
205 | DecEq Namespace where
206 | decEq (MkNS ms) (MkNS ns) = decEqCong (decEq ms ns)
210 | showSep : String -> List String -> String
211 | showSep sep = Libraries.Data.String.Extra.join sep
214 | showNSWithSep : String -> Namespace -> String
215 | showNSWithSep sep (MkNS ns) = showSep sep (reverse ns)
218 | Show Namespace where
219 | show = showNSWithSep "."
222 | Show ModuleIdent where
223 | show = showNSWithSep "." . miAsNamespace
226 | Pretty Void Namespace where
227 | pretty (MkNS ns) = concatWith (surround dot) (pretty <$> reverse ns)
230 | Pretty Void ModuleIdent where
231 | pretty = pretty . miAsNamespace
240 | emptyNS : Namespace
241 | emptyNS = mkNamespace ""
245 | mainNS = mkNamespace "Main"
248 | partialEvalNS : Namespace
249 | partialEvalNS = mkNamespace "_PE"
252 | builtinNS : Namespace
253 | builtinNS = mkNamespace "Builtin"
256 | preludeNS : Namespace
257 | preludeNS = mkNamespace "Prelude"
261 | numNS = mkNamespace "Prelude.Num"
264 | typesNS : Namespace
265 | typesNS = mkNamespace "Prelude.Types"
268 | basicsNS : Namespace
269 | basicsNS = mkNamespace "Prelude.Basics"
272 | eqOrdNS : Namespace
273 | eqOrdNS = mkNamespace "Prelude.EqOrd"
276 | primIONS : Namespace
277 | primIONS = mkNamespace "PrimIO"
281 | ioNS = mkNamespace "Prelude.IO"
284 | reflectionNS : Namespace
285 | reflectionNS = mkNamespace "Language.Reflection"
288 | reflectionTTNS : Namespace
289 | reflectionTTNS = mkNamespace "Language.Reflection.TT"
292 | reflectionTTImpNS : Namespace
293 | reflectionTTImpNS = mkNamespace "Language.Reflection.TTImp"
296 | dpairNS : Namespace
297 | dpairNS = mkNamespace "Builtin.DPair"
301 | natNS = mkNamespace "Data.Nat"