1 {-# LANGUAGE PolyKinds #-}
2 {-# LANGUAGE TypeInType #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.Symantic.Typing.Document where
6 import Data.Function (id)
7 import Data.Map.Strict (Map)
8 import Data.Maybe (fromMaybe)
9 import Data.Semigroup (Semigroup(..))
12 import qualified Data.List as L
13 import qualified Data.Map.Strict as Map
14 import qualified Data.Set as Set
15 import qualified Data.Text as T
17 import qualified Language.Symantic.Document.Sym as Doc
18 import Language.Symantic.Grammar
19 import Language.Symantic.Typing.Kind
20 import Language.Symantic.Typing.Variable
21 import Language.Symantic.Typing.Module
22 import Language.Symantic.Typing.Type
24 -- * Type 'Config_Doc_Type'
27 { config_Doc_Type_vars_numbering :: Bool
28 -- ^ Style of /type variables/:
30 -- * if 'True': use name and position (as in @a0@ then @a1@)
31 -- * if 'False': use name, and a counter when names collide (as in @a@ then @a1@)
33 -- NOTE: if the name is empty, a 'freshName' is taken from 'poolNames'.
34 , config_Doc_Type_imports :: Imports NameTy
37 config_Doc_Type :: Config_Doc_Type
40 { config_Doc_Type_vars_numbering = True
41 , config_Doc_Type_imports = mempty
53 docType conf@Config_Doc_Type{config_Doc_Type_imports=imps} pr ty =
54 let (v2n, _) = var2Name conf mempty ty in
55 go v2n (infixB SideL pr, SideL) ty
59 Map IndexVar Name -> -- names of variables
63 go v2n _po (TyVar _src _n v) =
64 let iv = indexVar v in
65 case Map.lookup iv v2n of
66 Nothing -> error "[BUG] docType: variable name missing"
69 go _v2n _po (TyConst _src _vs c@Const{}) =
70 (if isNameTyOp c then docParen else id) $
73 go v2n _po (TyApp _ (TyConst _ _ f@Const{}) a)
74 | Just HRefl <- proj_ConstKi @(K []) @[] f =
75 "[" <> go v2n (infixB SideL 0, SideL) a <> "]"
77 go v2n po (TyApp _ (TyApp _ (TyConst _ _ f@Const{}) a) b)
79 | Just HRefl <- proj_ConstKiTy @Constraint @(()::Constraint) a
80 , Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f =
82 | Just (Fixity2 op) <- fixityOf f =
83 (if needsParenInfix po op then docParen else id) $
84 go v2n (op, SideL) a <>
89 prettyConst :: forall k c. Const src (c::k) -> d
90 prettyConst c | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c = Doc.space <> d_op "=>" <> Doc.space
91 prettyConst c | Just HRefl <- proj_ConstKi @(K (#)) @(#) c = d_op "," <> Doc.space
92 prettyConst c | Just HRefl <- proj_ConstKi @(K (,)) @(,) c = d_op "," <> Doc.space
94 | r <- typeRepTyCon (typeRep c)
96 , tyConModule r =="Language.Symantic.Typing.Type"
97 -- XXX: module name must be in sync with where (#~) is defined.
98 -- NOTE: cannot use 'proj_ConstKi' here
99 -- because (#~) has a polymorphic kind.
100 = Doc.space <> d_op "~" <> Doc.space
101 | otherwise = Doc.space <> d_op (docConst imps c) <> Doc.space
103 go v2n po (TyApp _src f a) =
104 let op = infixL 11 in
105 (if needsParenInfix po op then docParen else id) $
106 go v2n (op, SideL) f <>
110 go v2n po (TyFam _src _len fam args) =
111 let op = infixL 11 in
112 (if needsParenInfix po op then docParen else id) $
115 Doc.space <> go v2n (op, SideL) t <> acc
118 -- | Return a 'Map' associating a distinct 'Name'
119 -- for all the variables of the given 'Type'.
122 (Map IndexVar Name, Names) ->
124 (Map IndexVar Name, Names)
125 var2Name _cfg st TyConst{} = st
126 var2Name cfg st@(v2n, ns) (TyVar _src (NameVar n) v) =
127 let iv = indexVar v in
128 case Map.lookup iv v2n of
132 if config_Doc_Type_vars_numbering cfg && not (T.null n)
133 then n <> T.pack (show iv)
134 else freshifyName ns n in
135 let v2n' = Map.insert iv n' v2n in
136 let ns' = Set.insert n' ns in
138 var2Name cfg st (TyApp _src f a) = var2Name cfg (var2Name cfg st f) a
139 var2Name cfg st (TyFam _src _len _fam as) = foldlTys (flip $ var2Name cfg) as st
142 type Names = Set Name
144 -- | Return given 'Name' renamed a bit to avoid
145 -- conflicting with any given 'Names'.
146 freshifyName :: Names -> Name -> Name
147 freshifyName ns "" = freshName ns
149 let ints = [1..] :: [Int] in
152 | suffix <- "" : (show <$> ints)
153 , fresh <- [n <> T.pack suffix]
154 , fresh `Set.notMember` ns
157 freshName :: Names -> Name
158 freshName ns = L.head $ poolNames L.\\ Set.toList ns
160 -- | Infinite list of unique 'Name's:
161 -- @a, b, .., z, a1, b1 .., z1, a2, ..@
164 [ T.singleton n | n <- ['a'..'z'] ] <>
165 [ T.pack (n:show i) | n <- ['a'..'z']
166 , i <- [1 :: Int ..] ]
168 -- * Document 'Types'
177 d_op (Doc.charH '[') <> go tys <> d_op (Doc.charH ']')
180 go :: forall xs. Types src vs xs -> d
181 go TypesZ = Doc.empty
182 go (TypesS t0 (TypesS t1 ts)) =
183 docType conf 10 t0 <>
184 d_op (Doc.charH ',') <> Doc.space <>
185 docType conf 10 t1 <>
187 go (TypesS t ts) = docType conf 10 t <> go ts
189 -- * Document 'Const'
190 docConst :: Doc.Textable d => Imports NameTy -> Const src c -> d
191 docConst imps c@Const{} =
194 revlookupImports f (m `Mod` n) imps
196 f = fixyOfFixity $ Fixity2 infixN5 `fromMaybe` fixityOf c
197 mn@(m `Mod` n) = nameTyOf c
199 -- * Document 'NameTy'
200 docNameTy :: Doc.Textable d => NameTy -> d
201 docNameTy (NameTy t) = Doc.textH t
204 docMod :: Doc.Textable d => (a -> d) -> Mod a -> d
205 docMod a2d ([] `Mod` a) = a2d a
206 docMod a2d (m `Mod` a) = docPathMod m <> (Doc.charH '.') <> a2d a
208 -- * Document 'PathMod'
209 docPathMod :: Doc.Textable d => PathMod -> d
210 docPathMod (p::PathMod) =
212 L.intersperse (Doc.charH '.') $
213 (\(NameMod n) -> Doc.textH n) <$> p
215 docParen :: Doc.Textable d => d -> d
216 docParen = Doc.between "(" ")"
223 Doc.Decorationable d =>
225 Sym.Modules src ss -> d
226 docModules (Sym.Modules mods) =
228 (\p m doc -> docModule p m <> doc)
237 Doc.Decorationable d =>
239 Sym.PathMod -> Sym.Module src ss -> d
240 docModule m Sym.Module
245 go docFixityInfix module_infix <>
246 go docFixityPrefix module_prefix <>
247 go docFixityPostfix module_postfix
249 go :: (fixy -> d) -> ModuleFixy src ss fixy -> d
257 docFixy token_fixity <>
258 Doc.space <> Doc.bold (Doc.yellower "::") <> Doc.space <>
259 docTokenTerm (t Sym.noSource) <>
269 Sym.Token_Term src ss -> d
271 let n2t = name2typeInj @ss in
272 case Sym.readTerm n2t CtxTyZ (G.BinTree0 t) of
273 Left{} -> error "[BUG] docTokenTerm: readTerm failed"
274 Right (Sym.TermVT te) ->
275 Sym.docType Sym.config_doc_type
276 { config_Doc_Type_vars_numbering = False
277 } 0 $ Sym.typeOfTerm te
279 docFixityInfix :: (Doc.Decorationable t, Doc.Colorable t, Doc.Textable t) => Infix -> t
280 docFixityInfix = \case
281 Sym.Infix Nothing 5 -> Doc.empty
286 Sym.AssocB Sym.SideL -> "l"
287 Sym.AssocB Sym.SideR -> "r" in
288 Doc.magenta $ " infix" <> maybe Doc.empty docAssoc a <>
289 Doc.space <> Doc.bold (Doc.bluer (Doc.int p))
290 docFixityPrefix :: (Doc.Decorationable t, Doc.Colorable t, Doc.Textable t) => Unifix -> t
291 docFixityPrefix p = Doc.magenta $ " prefix " <> Doc.bold (Doc.bluer (Doc.int $ Sym.unifix_prece p))
292 docFixityPostfix :: (Doc.Decorationable t, Doc.Colorable t, Doc.Textable t) => Unifix -> t
293 docFixityPostfix p = Doc.magenta $ " postfix " <> Doc.bold (Doc.bluer (Doc.int $ Sym.unifix_prece p))
298 Sym.PathMod -> Sym.NameTe -> d
299 docPathTe (ms::Sym.PathMod) (Sym.NameTe n) =
301 L.intersperse (Doc.charH '.') $
302 ((\(Sym.NameMod m) -> Doc.textH m) <$> ms) <>
303 [(if isOp n then id else Doc.yellower) $ Doc.text n]
305 isOp = T.all $ \case '_' -> True; '\'' -> True; c -> Char.isAlphaNum c