Add colorable and decorable.
[haskell/symantic.git] / symantic / Language / Symantic / Typing / Show.hs
index b134e1e12a36f26b43fcd0a521b6485d5f285c8a..cbd360d534d1180ff549ae2b75f2975f038d07c2 100644 (file)
@@ -2,98 +2,32 @@
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 module Language.Symantic.Typing.Show where
 
-import Data.Map.Strict (Map)
-import Data.Semigroup ((<>))
-import Data.Set (Set)
-import Data.Text (Text)
-import Data.Typeable
-import qualified Data.List as List
-import qualified Data.Map.Strict as Map
-import qualified Data.Set as Set
-import qualified Data.Text as Text
+import qualified Data.Text.Lazy as TL
+import qualified Data.Text.Lazy.Builder as TLB
 
+import qualified Language.Symantic.Document as D
 import Language.Symantic.Grammar
-import Language.Symantic.Typing.Kind
-import Language.Symantic.Typing.Variable
 import Language.Symantic.Typing.Type
+import Language.Symantic.Typing.Module
+import Language.Symantic.Typing.Document
+
+showType :: Config_Doc_Type -> Type src vs t -> String
+showType conf ty = TL.unpack $ TLB.toLazyText $ D.plain $ docType conf 0 ty
+
+showTypeS :: Config_Doc_Type -> Precedence -> Type src vs t -> ShowS
+showTypeS conf pr ty = showString $ TL.unpack (TLB.toLazyText $ D.plain $ docType conf pr ty)
+
+showTypes :: Config_Doc_Type -> Types src vs ts -> String
+showTypes conf tys = TL.unpack (TLB.toLazyText $ D.plain $ docTypes conf tys)
+
+showTypesS :: Config_Doc_Type -> Types src vs ts -> ShowS
+showTypesS conf tys = showString $ TL.unpack (TLB.toLazyText $ D.plain $ docTypes conf tys)
+
+instance NameTyOf c => Show (Const src c) where
+       showsPrec _p = showString . TL.unpack . TLB.toLazyText . D.plain . docConst mempty
 
 instance Source src => Show (Type src vs t) where
-       showsPrec pr typ =
-               let (v2n, _) = varNames mempty typ in
-               go v2n (infixB SideL pr, SideL) typ
-               where
-               go ::
-                forall x.
-                (Map IndexVar Name) -> -- names of variables
-                (Infix, Side) ->
-                Type src vs x ->
-                ShowS
-               go _v2n _po c
-                | Just HRefl <- proj_ConstKiTy @Constraint @() c = showString "()"
-               go _v2n _po (TyConst _src _vs c) = showsPrec 11 c
-               go v2n _po (TyVar _src _n v) =
-                       let iv = indexVar v in
-                       case Map.lookup iv v2n of
-                        Nothing -> error "[BUG] showsPrec @Type: variable name missing"
-                        Just n -> showString $ Text.unpack n
-               go v2n po (TyApp _ (TyApp _ (TyConst _ _ f@Const{}) a) b)
-                | Just HRefl <- proj_ConstKiTy @Constraint @(()::Constraint) a
-                , Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f =
-                       go v2n po b
-                | Fixity2 op <- fixityOf f =
-                       showParen (needsParenInfix po op) $
-                       go v2n (op, SideL) a .
-                       showString (prettyConst f) .
-                       go v2n (op, SideR) b
-                       where
-                       prettyConst :: forall k c. Const src (c::k) -> String
-                       prettyConst c | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c = " => "
-                       prettyConst c | Just HRefl <- proj_ConstKi @(K (#)) @(#) c = ", "
-                       prettyConst c@Const{}
-                        | r <- typeRepTyCon (typeRep c)
-                        , tyConName   r =="#~"
-                        , tyConModule r =="Language.Symantic.Typing.Type"
-                        -- XXX: module name must be in sync with where (#~) is defined.
-                        -- NOTE: cannot use 'proj_ConstKi' here
-                        -- because (#~) has a polymorphic kind.
-                        = " ~ "
-                        | otherwise = ' ' : unParen (show c) ++ " "
-                       unParen ('(':s) | ')':s' <- reverse s = reverse s'
-                       unParen s = s
-               go v2n po (TyApp _src f a) =
-                       let op = infixR 11 in
-                       showParen (needsParenInfix po op) $
-                       go v2n (op, SideL) f .
-                       showChar ' ' .
-                       go v2n (op, SideR) a
-               go v2n po (TyFam _src _len fam args) =
-                       let op = infixL 11 in
-                       showParen (needsParenInfix po op) $
-                       showsPrec 11 fam .
-                       foldlTys (\t acc ->
-                               showChar ' ' . go v2n (op, SideL) t . acc
-                        ) args id
-               
-               -- | Return a 'Map' associating a distinct 'Name'
-               -- for all the variables of the given 'Type'.
-               varNames ::
-                forall x.
-                (Map IndexVar Name, Names) ->
-                Type src vs x ->
-                (Map IndexVar Name, Names)
-               varNames st TyConst{} = st
-               varNames st@(v2n, ns) (TyVar _src (NameVar n) v) =
-                       let iv = indexVar v in
-                       case Map.lookup iv v2n of
-                        Just{} -> st
-                        Nothing ->
-                               -- let n' = freshifyName ns n in
-                               let n'   = n <> Text.pack (show iv) in
-                               let v2n' = Map.insert iv n' v2n in
-                               let ns'  = Set.insert n' ns in
-                               (v2n', ns')
-               varNames st (TyApp _src f a) = varNames (varNames st f) a
-               varNames st (TyFam _src _len _fam as) = foldlTys (flip varNames) as st
+       showsPrec = showTypeS config_Doc_Type
 instance Source src => Show (TypeK src vs kt) where
        showsPrec p (TypeK t) = showsPrec p t
 instance Source src => Show (TypeVT src) where
@@ -101,41 +35,4 @@ instance Source src => Show (TypeVT src) where
 instance Source src => Show (TypeT src vs) where
        showsPrec p (TypeT t) = showsPrec p t
 instance Source src => Show (Types src vs ts) where
-       showsPrec _p tys = showString "[" . go tys . showString "]"
-               where
-               go :: forall xs. Types src vs xs -> ShowS
-               go TypesZ = showString ""
-               go (TypesS t0 (TypesS t1 ts)) = showsPrec 10 t0 . showString ", " . showsPrec 10 t1 . go ts
-               go (TypesS t ts) = showsPrec 10 t . go ts
-
-showBracket :: Bool -> ShowS -> ShowS
-showBracket b p =  if b then showChar '{' . p . showChar '}' else p
-
--- ** Type 'Name'
-type Name     = Text
-type NameHint = Name
-type Names    = Set Name
-
--- | Return given 'Name' renamed a bit to avoid
--- conflicting with any given 'Names'.
-freshifyName :: Names -> Name -> Name
-freshifyName ns "" = freshName ns
-freshifyName ns n =
-       let ints = [1..] :: [Int] in
-       List.head
-        [ fresh
-        | suffix <- "" : (show <$> ints)
-        , fresh <- [n <> Text.pack suffix]
-        , fresh `Set.notMember` ns
-        ]
-
-freshName :: Names -> Name
-freshName ns = List.head $ poolNames List.\\ Set.toList ns
-
--- | Infinite list of unique 'Name's:
--- @a, b, .., z, a1, b1 .., z1, a2, ..@
-poolNames :: [Name]
-poolNames =
-       [ Text.singleton n     | n <- ['a'..'z'] ] <>
-       [ Text.pack (n:show i) | n <- ['a'..'z']
-                              , i <- [1 :: Int ..] ]
+       showsPrec _ = showTypesS config_Doc_Type