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.Monoid (Monoid(..))
10 import Data.Semigroup (Semigroup(..))
12 import Data.Text (Text)
14 import qualified Data.List as L
15 import qualified Data.Map.Strict as Map
16 import qualified Data.Set as Set
17 import qualified Data.Text as T
18 import qualified Symantic.Document as Doc
20 import Language.Symantic.Grammar
21 import Language.Symantic.Typing.Kind
22 import Language.Symantic.Typing.Variable
23 import Language.Symantic.Typing.Module
24 import Language.Symantic.Typing.Type
26 -- * Type 'Config_Doc_Type'
29 { config_Doc_Type_vars_numbering :: Bool
30 -- ^ Style of /type variables/:
32 -- * if 'True': use name and position (as in @a0@ then @a1@)
33 -- * if 'False': use name, and a counter when names collide (as in @a@ then @a1@)
35 -- NOTE: if the name is empty, a 'freshName' is taken from 'poolNames'.
36 , config_Doc_Type_imports :: Imports NameTy
39 config_Doc_Type :: Config_Doc_Type
42 { config_Doc_Type_vars_numbering = True
43 , config_Doc_Type_imports = mempty
50 Doc.From (Doc.Word Char) d =>
51 Doc.From (Doc.Word Text) d =>
57 docType conf@Config_Doc_Type{config_Doc_Type_imports=imps} pr ty =
58 let (v2n, _) = var2Name conf mempty ty in
59 go v2n (infixB SideL pr, SideL) ty
63 Map IndexVar Name -> -- names of variables
67 go v2n _po (TyVar _src _n v) =
68 let iv = indexVar v in
69 case Map.lookup iv v2n of
70 Nothing -> error "[BUG] docType: variable name missing"
71 Just n -> Doc.from (Doc.Word n)
73 go _v2n _po (TyConst _src _vs c@Const{}) =
74 (if isNameTyOp c then docParen else id) $
77 go v2n _po (TyApp _ (TyConst _ _ f@Const{}) a)
78 | Just HRefl <- proj_ConstKi @(K []) @[] f =
79 Doc.between (Doc.from (Doc.Word '[')) (Doc.from (Doc.Word ']')) $
80 go v2n (infixB SideL 0, SideL) a
82 go v2n po (TyApp _ (TyApp _ (TyConst _ _ f@Const{}) a) b)
84 | Just HRefl <- proj_ConstKiTy @Constraint @(()::Constraint) a
85 , Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f =
87 | Just (Fixity2 op) <- fixityOf f =
88 (if isPairNeeded po op then docParen else id) $
89 go v2n (op, SideL) a <>
94 d_op = Doc.yellower . Doc.from . Doc.Word
95 prettyConst :: forall k c. Const src (c::k) -> d
96 prettyConst c | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c = Doc.space <> d_op "=>" <> Doc.space
97 prettyConst c | Just HRefl <- proj_ConstKi @(K (#)) @(#) c = d_op "," <> Doc.space
98 prettyConst c | Just HRefl <- proj_ConstKi @(K (,)) @(,) c = d_op "," <> Doc.space
100 | r <- typeRepTyCon (typeRep c)
102 , tyConModule r =="Language.Symantic.Typing.Type"
103 -- XXX: module name must be in sync with where (#~) is defined.
104 -- NOTE: cannot use 'proj_ConstKi' here
105 -- because (#~) has a polymorphic kind.
106 = Doc.space <> d_op "~" <> Doc.space
107 | otherwise = Doc.space <> d_op (docConst imps c) <> Doc.space
109 go v2n po (TyApp _src f a) =
110 let op = infixL 11 in
111 (if isPairNeeded po op then docParen else id) $
112 go v2n (op, SideL) f <>
116 go v2n po (TyFam _src _len fam args) =
117 let op = infixL 11 in
118 (if isPairNeeded po op then docParen else id) $
121 Doc.space <> go v2n (op, SideL) t <> acc
124 -- | Return a 'Map' associating a distinct 'Name'
125 -- for all the variables of the given 'Type'.
128 (Map IndexVar Name, Names) ->
130 (Map IndexVar Name, Names)
131 var2Name _cfg st TyConst{} = st
132 var2Name cfg st@(v2n, ns) (TyVar _src (NameVar n) v) =
133 let iv = indexVar v in
134 case Map.lookup iv v2n of
138 if config_Doc_Type_vars_numbering cfg && not (T.null n)
139 then n <> T.pack (show iv)
140 else freshifyName ns n in
141 let v2n' = Map.insert iv n' v2n in
142 let ns' = Set.insert n' ns in
144 var2Name cfg st (TyApp _src f a) = var2Name cfg (var2Name cfg st f) a
145 var2Name cfg st (TyFam _src _len _fam as) = foldlTys (flip $ var2Name cfg) as st
148 type Names = Set Name
150 -- | Return given 'Name' renamed a bit to avoid
151 -- conflicting with any given 'Names'.
152 freshifyName :: Names -> Name -> Name
153 freshifyName ns "" = freshName ns
155 let ints = [1..] :: [Int] in
158 | suffix <- "" : (show <$> ints)
159 , fresh <- [n <> T.pack suffix]
160 , fresh `Set.notMember` ns
163 freshName :: Names -> Name
164 freshName ns = L.head $ poolNames L.\\ Set.toList ns
166 -- | Infinite list of unique 'Name's:
167 -- @a, b, .., z, a1, b1 .., z1, a2, ..@
170 [ T.singleton n | n <- ['a'..'z'] ] <>
171 [ T.pack (n:show i) | n <- ['a'..'z']
172 , i <- [1 :: Int ..] ]
174 -- * Document 'Types'
178 Doc.From (Doc.Word Char) d =>
179 Doc.From (Doc.Word Text) d =>
185 d_op (Doc.from (Doc.Word '[')) <> go tys <> d_op (Doc.from (Doc.Word ']'))
188 go :: forall xs. Types src vs xs -> d
190 go (TypesS t0 (TypesS t1 ts)) =
191 docType conf 10 t0 <>
192 d_op (Doc.from (Doc.Word ',')) <> Doc.space <>
193 docType conf 10 t1 <>
195 go (TypesS t ts) = docType conf 10 t <> go ts
197 -- * Document 'Const'
200 Doc.From (Doc.Word Char) d =>
201 Doc.From (Doc.Word Text) d =>
202 Imports NameTy -> Const src c -> d
203 docConst imps c@Const{} =
206 revlookupImports f (m `Mod` n) imps
208 f = fixyOfFixity $ Fixity2 infixN5 `fromMaybe` fixityOf c
209 mn@(m `Mod` n) = nameTyOf c
211 -- * Document 'NameTy'
212 docNameTy :: Doc.From (Doc.Word Text) d => NameTy -> d
213 docNameTy (NameTy t) = Doc.from (Doc.Word t)
218 Doc.From (Doc.Word Char) d =>
219 Doc.From (Doc.Word Text) d =>
220 (a -> d) -> Mod a -> d
221 docMod a2d ([] `Mod` a) = a2d a
222 docMod a2d (m `Mod` a) = docPathMod m <> (Doc.from (Doc.Word '.')) <> a2d a
224 -- * Document 'PathMod'
227 Doc.From (Doc.Word Char) d =>
228 Doc.From (Doc.Word Text) d =>
230 docPathMod (p::PathMod) =
232 L.intersperse (Doc.from (Doc.Word '.')) $
233 (\(NameMod n) -> Doc.from (Doc.Word n)) <$> p
235 docParen :: Doc.Spaceable d => Doc.From (Doc.Word Char) d => d -> d
236 docParen = Doc.between (Doc.from (Doc.Word '(')) (Doc.from (Doc.Word ')'))
243 Doc.Decorationable d =>
245 Sym.Modules src ss -> d
246 docModules (Sym.Modules mods) =
248 (\p m doc -> docModule p m <> doc)
257 Doc.Decorationable d =>
259 Sym.PathMod -> Sym.Module src ss -> d
260 docModule m Sym.Module
265 go docFixityInfix module_infix <>
266 go docFixityPrefix module_prefix <>
267 go docFixityPostfix module_postfix
269 go :: (fixy -> d) -> ModuleFixy src ss fixy -> d
277 docFixy token_fixity <>
278 Doc.space <> Doc.bold (Doc.yellower "::") <> Doc.space <>
279 docTokenTerm (t Sym.noSource) <>
289 Sym.Token_Term src ss -> d
291 let n2t = name2typeInj @ss in
292 case Sym.readTerm n2t CtxTyZ (G.BinTree0 t) of
293 Left{} -> error "[BUG] docTokenTerm: readTerm failed"
294 Right (Sym.TermVT te) ->
295 Sym.docType Sym.config_doc_type
296 { config_Doc_Type_vars_numbering = False
297 } 0 $ Sym.typeOfTerm te
299 docFixityInfix :: (Doc.Decorationable t, Doc.Colorable16 t, Doc.Textable t) => Infix -> t
300 docFixityInfix = \case
301 Sym.Infix Nothing 5 -> mempty
306 Sym.AssocB Sym.SideL -> "l"
307 Sym.AssocB Sym.SideR -> "r" in
308 Doc.magenta $ " infix" <> maybe mempty docAssoc a <>
309 Doc.space <> Doc.bold (Doc.bluer (Doc.int p))
310 docFixityPrefix :: (Doc.Decorationable t, Doc.Colorable16 t, Doc.Textable t) => Unifix -> t
311 docFixityPrefix p = Doc.magenta $ " prefix " <> Doc.bold (Doc.bluer (Doc.int $ Sym.unifix_prece p))
312 docFixityPostfix :: (Doc.Decorationable t, Doc.Colorable16 t, Doc.Textable t) => Unifix -> t
313 docFixityPostfix p = Doc.magenta $ " postfix " <> Doc.bold (Doc.bluer (Doc.int $ Sym.unifix_prece p))
318 Sym.PathMod -> Sym.NameTe -> d
319 docPathTe (ms::Sym.PathMod) (Sym.NameTe n) =
321 L.intersperse (Doc.charH '.') $
322 ((\(Sym.NameMod m) -> Doc.textH m) <$> ms) <>
323 [(if isOp n then id else Doc.yellower) $ Doc.text n]
325 isOp = T.all $ \case '_' -> True; '\'' -> True; c -> Char.isAlphaNum c