{-# 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 :: NameMap
emptyNameMap = NameMap
forall a. IntMap a
IntMap.empty
data WithNames a = WithNames a NameMap
ppWithNamesPrec :: PP (WithNames a) => NameMap -> Int -> a -> Doc
ppWithNamesPrec :: NameMap -> Int -> a -> Doc
ppWithNamesPrec NameMap
names Int
prec a
t = Int -> WithNames a -> Doc
forall a. PP a => Int -> a -> Doc
ppPrec Int
prec (a -> NameMap -> WithNames a
forall a. a -> NameMap -> WithNames a
WithNames a
t NameMap
names)
ppWithNames :: PP (WithNames a) => NameMap -> a -> Doc
ppWithNames :: NameMap -> a -> Doc
ppWithNames NameMap
names a
t = NameMap -> Int -> a -> Doc
forall a. PP (WithNames a) => NameMap -> Int -> a -> Doc
ppWithNamesPrec NameMap
names Int
0 a
t
dump :: PP (WithNames a) => a -> String
dump :: a -> String
dump a
x = Doc -> String
forall a. Show a => a -> String
show (NameMap -> a -> Doc
forall a. PP (WithNames a) => NameMap -> a -> Doc
ppWithNames NameMap
forall a. IntMap a
IntMap.empty a
x)
nameVariant :: Int -> String -> String
nameVariant :: Int -> String -> String
nameVariant Int
0 String
x = String
x
nameVariant Int
n String
x = String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
nameVariants :: String -> [String]
nameVariants :: String -> [String]
nameVariants String
x = (Int -> String) -> [Int] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> String -> String
`nameVariant` String
x) [ Int
0 .. ]
nameList :: [String] -> [String]
nameList :: [String] -> [String]
nameList [String]
names = [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[String]] -> [String]) -> [[String]] -> [String]
forall a b. (a -> b) -> a -> b
$ [[String]] -> [[String]]
forall a. [[a]] -> [[a]]
transpose ([[String]] -> [[String]]) -> [[String]] -> [[String]]
forall a b. (a -> b) -> a -> b
$ (String -> [String]) -> [String] -> [[String]]
forall a b. (a -> b) -> [a] -> [b]
map String -> [String]
nameVariants [String]
baseNames
where
baseNames :: [String]
baseNames | [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
names = (Char -> String) -> String -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Char -> String -> String
forall a. a -> [a] -> [a]
:[]) [ Char
'a' .. Char
'z' ]
| Bool
otherwise = [String]
names