quox/lib/Quox/Name.idr

63 lines
1.1 KiB
Idris

module Quox.Name
import public Data.SnocList
import Data.List
%default total
public export
data BaseName =
UN String -- user-given name
private BRepr : Type
BRepr = String
private %inline brepr : BaseName -> BRepr
brepr (UN x) = x
export Eq BaseName where (==) = (==) `on` brepr
export Ord BaseName where compare = compare `on` brepr
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
private Repr : Type
Repr = (SnocList String, BRepr)
private %inline repr : Name -> Repr
repr x = (x.mods, brepr x.base)
export Eq Name where (==) = (==) `on` repr
export Ord Name where compare = compare `on` repr
export
Show Name where
show (MakeName mods base) =
concat $ intersperse "." $ toList $ mods :< show base
export
FromString Name where
fromString x = MakeName [<] (fromString x)
export
toDots : Name -> String
toDots x = fastConcat $ cast $ map (<+> ".") x.mods :< baseStr x.base