{-# LANGUAGE Safe #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
module Cryptol.TypeCheck.PP
( NameMap, WithNames(..)
, emptyNameMap
, ppWithNamesPrec, ppWithNames
, nameList
, dump
, module Cryptol.Utils.PP
) where
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.List(transpose)
import Cryptol.Utils.PP
type NameMap = IntMap String
emptyNameMap :: NameMap
emptyNameMap = IntMap.empty
data WithNames a = WithNames a NameMap
ppWithNamesPrec :: PP (WithNames a) => NameMap -> Int -> a -> Doc
ppWithNamesPrec names prec t = ppPrec prec (WithNames t names)
ppWithNames :: PP (WithNames a) => NameMap -> a -> Doc
ppWithNames names t = ppWithNamesPrec names 0 t
dump :: PP (WithNames a) => a -> String
dump x = show (ppWithNames IntMap.empty x)
nameVariant :: Int -> String -> String
nameVariant 0 x = x
nameVariant n x = x ++ show n
nameVariants :: String -> [String]
nameVariants x = map (`nameVariant` x) [ 0 .. ]
nameList :: [String] -> [String]
nameList names = concat $ transpose $ map nameVariants baseNames
where
baseNames | null names = map (:[]) [ 'a' .. 'z' ]
| otherwise = names