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 as D
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 D.paren 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 D.paren 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 = D.space <> d_op "=>" <> D.space
91 prettyConst c | Just HRefl <- proj_ConstKi @(K (#)) @(#) c = d_op "," <> D.space
92 prettyConst c | Just HRefl <- proj_ConstKi @(K (,)) @(,) c = d_op "," <> D.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 = D.space <> d_op "~" <> D.space
101 | otherwise = D.space <> d_op (docConst imps c) <> D.space
103 go v2n po (TyApp _src f a) =
104 let op = infixL 11 in
105 (if needsParenInfix po op then D.paren 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 D.paren else id) $
115 D.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 (D.charH '[') <> go tys <> d_op (D.charH ']')
180 go :: forall xs. Types src vs xs -> d
182 go (TypesS t0 (TypesS t1 ts)) =
183 docType conf 10 t0 <>
184 d_op (D.charH ',') <> D.space <>
185 docType conf 10 t1 <>
187 go (TypesS t ts) = docType conf 10 t <> go ts
189 -- * Document 'Const'
190 docConst :: D.Doc_Text 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 :: D.Doc_Text d => NameTy -> d
201 docNameTy (NameTy t) = D.textH t
204 docMod :: D.Doc_Text d => (a -> d) -> Mod a -> d
205 docMod a2d ([] `Mod` a) = a2d a
206 docMod a2d (m `Mod` a) = docPathMod m <> (D.charH '.') <> a2d a
208 -- * Document 'PathMod'
209 docPathMod :: D.Doc_Text d => PathMod -> d
210 docPathMod (p::PathMod) =
212 L.intersperse (D.charH '.') $
213 (\(NameMod n) -> D.textH n) <$> p
221 D.Doc_Decoration d =>
223 Sym.Modules src ss -> d
224 docModules (Sym.Modules mods) =
226 (\p m doc -> docModule p m <> doc)
235 D.Doc_Decoration d =>
237 Sym.PathMod -> Sym.Module src ss -> d
238 docModule m Sym.Module
243 go docFixityInfix module_infix <>
244 go docFixityPrefix module_prefix <>
245 go docFixityPostfix module_postfix
247 go :: (fixy -> d) -> ModuleFixy src ss fixy -> d
255 docFixy token_fixity <>
256 D.space <> D.bold (D.yellower "::") <> D.space <>
257 docTokenTerm (t Sym.noSource) <>
267 Sym.Token_Term src ss -> d
269 let n2t = name2typeInj @ss in
270 case Sym.readTerm n2t CtxTyZ (G.BinTree0 t) of
271 Left{} -> error "[BUG] docTokenTerm: readTerm failed"
272 Right (Sym.TermVT te) ->
273 Sym.docType Sym.config_doc_type
274 { config_Doc_Type_vars_numbering = False
275 } 0 $ Sym.typeOfTerm te
277 docFixityInfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Infix -> t
278 docFixityInfix = \case
279 Sym.Infix Nothing 5 -> D.empty
284 Sym.AssocB Sym.SideL -> "l"
285 Sym.AssocB Sym.SideR -> "r" in
286 D.magenta $ " infix" <> maybe D.empty docAssoc a <>
287 D.space <> D.bold (D.bluer (D.int p))
288 docFixityPrefix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
289 docFixityPrefix p = D.magenta $ " prefix " <> D.bold (D.bluer (D.int $ Sym.unifix_prece p))
290 docFixityPostfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
291 docFixityPostfix p = D.magenta $ " postfix " <> D.bold (D.bluer (D.int $ Sym.unifix_prece p))
296 Sym.PathMod -> Sym.NameTe -> d
297 docPathTe (ms::Sym.PathMod) (Sym.NameTe n) =
299 L.intersperse (D.charH '.') $
300 ((\(Sym.NameMod m) -> D.textH m) <$> ms) <>
301 [(if isOp n then id else D.yellower) $ D.text n]
303 isOp = T.all $ \case '_' -> True; '\'' -> True; c -> Char.isAlphaNum c