quox/lib/Quox/Name.idr
2023-02-28 20:51:54 +01:00

62 lines
1.1 KiB
Idris

module Quox.Name
import public Data.SnocList
import Data.List
import Generics.Derive
%hide TT.Name
%default total
%language ElabReflection
public export
data BaseName
= UN String -- user-given name
%runElab derive "BaseName" [Generic, Meta, Eq, Ord, DecEq]
export
Show BaseName where
show (UN x) = x
export
baseStr : BaseName -> String
baseStr (UN x) = x
export
FromString BaseName where
fromString = UN
public export
record Name where
constructor MakeName
mods : SnocList String
base : BaseName
%runElab derive "Name" [Generic, Meta, Eq, Ord, DecEq]
export
Show Name where
show (MakeName mods base) =
concat $ intersperse "." $ toList $ mods :< show base
export
FromString Name where
fromString x = MakeName [<] (fromString x)
public export %inline
unq : BaseName -> Name
unq = MakeName [<]
export
toDots : Name -> String
toDots x = fastConcat $ cast $ map (<+> ".") x.mods :< baseStr x.base
export
fromList : List1 String -> Name
fromList (x ::: xs) = go [<] x xs where
go : SnocList String -> String -> List String -> Name
go mods x [] = MakeName mods (UN x)
go mods x (y :: ys) = go (mods :< x) y ys