1 {-# LANGUAGE PolyKinds #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Language.Symantic.Typing.Document where
5 import Data.Function (id)
6 import Data.Map.Strict (Map)
7 import Data.Maybe (fromMaybe)
8 import Data.Semigroup (Semigroup(..))
11 import qualified Data.List as L
12 import qualified Data.Map.Strict as Map
13 import qualified Data.Set as Set
14 import qualified Data.Text as T
16 import qualified Language.Symantic.Document as D
17 import Language.Symantic.Grammar
18 import Language.Symantic.Typing.Kind
19 import Language.Symantic.Typing.Variable
20 import Language.Symantic.Typing.Module
21 import Language.Symantic.Typing.Type
23 -- * Type 'Config_Doc_Type'
26 { config_Doc_Type_vars_numbering :: Bool
27 -- ^ Style of /type variables/:
29 -- * if 'True': use name and position (as in @a0@ then @a1@)
30 -- * if 'False': use name, and a counter when names collide (as in @a@ then @a1@)
32 -- NOTE: if the name is empty, a 'freshName' is taken from 'poolNames'.
33 , config_Doc_Type_imports :: Imports NameTy
36 config_Doc_Type :: Config_Doc_Type
39 { config_Doc_Type_vars_numbering = True
40 , config_Doc_Type_imports = mempty
52 docType conf@Config_Doc_Type{config_Doc_Type_imports=imps} pr ty =
53 let (v2n, _) = var2Name conf mempty ty in
54 go v2n (infixB SideL pr, SideL) ty
58 (Map IndexVar Name) -> -- names of variables
62 go v2n _po (TyVar _src _n v) =
63 let iv = indexVar v in
64 case Map.lookup iv v2n of
65 Nothing -> error "[BUG] docType: variable name missing"
68 go _v2n _po (TyConst _src _vs c@Const{}) =
69 (if isNameTyOp c then D.paren else id) $
72 go v2n _po (TyApp _ (TyConst _ _ f@Const{}) a)
73 | Just HRefl <- proj_ConstKi @(K []) @[] f =
74 "[" <> go v2n (infixB SideL 0, SideL) a <> "]"
76 go v2n po (TyApp _ (TyApp _ (TyConst _ _ f@Const{}) a) b)
78 | Just HRefl <- proj_ConstKiTy @Constraint @(()::Constraint) a
79 , Just HRefl <- proj_ConstKi @(K (#>)) @(#>) f =
81 | Just (Fixity2 op) <- fixityOf f =
82 (if needsParenInfix po op then D.paren else id) $
83 go v2n (op, SideL) a <>
88 prettyConst :: forall k c. Const src (c::k) -> d
89 prettyConst c | Just HRefl <- proj_ConstKi @(K (#>)) @(#>) c = D.space <> d_op "=>" <> D.space
90 prettyConst c | Just HRefl <- proj_ConstKi @(K (#)) @(#) c = d_op "," <> D.space
91 prettyConst c | Just HRefl <- proj_ConstKi @(K (,)) @(,) c = d_op "," <> D.space
93 | r <- typeRepTyCon (typeRep c)
95 , tyConModule r =="Language.Symantic.Typing.Type"
96 -- XXX: module name must be in sync with where (#~) is defined.
97 -- NOTE: cannot use 'proj_ConstKi' here
98 -- because (#~) has a polymorphic kind.
99 = D.space <> d_op "~" <> D.space
100 | otherwise = D.space <> d_op (docConst imps c) <> D.space
102 go v2n po (TyApp _src f a) =
103 let op = infixL 11 in
104 (if needsParenInfix po op then D.paren else id) $
105 go v2n (op, SideL) f <>
109 go v2n po (TyFam _src _len fam args) =
110 let op = infixL 11 in
111 (if needsParenInfix po op then D.paren else id) $
114 D.space <> go v2n (op, SideL) t <> acc
117 -- | Return a 'Map' associating a distinct 'Name'
118 -- for all the variables of the given 'Type'.
121 (Map IndexVar Name, Names) ->
123 (Map IndexVar Name, Names)
124 var2Name _cfg st TyConst{} = st
125 var2Name cfg st@(v2n, ns) (TyVar _src (NameVar n) v) =
126 let iv = indexVar v in
127 case Map.lookup iv v2n of
131 if config_Doc_Type_vars_numbering cfg && not (T.null n)
132 then n <> T.pack (show iv)
133 else freshifyName ns n in
134 let v2n' = Map.insert iv n' v2n in
135 let ns' = Set.insert n' ns in
137 var2Name cfg st (TyApp _src f a) = var2Name cfg (var2Name cfg st f) a
138 var2Name cfg st (TyFam _src _len _fam as) = foldlTys (flip $ var2Name cfg) as st
141 type Names = Set Name
143 -- | Return given 'Name' renamed a bit to avoid
144 -- conflicting with any given 'Names'.
145 freshifyName :: Names -> Name -> Name
146 freshifyName ns "" = freshName ns
148 let ints = [1..] :: [Int] in
151 | suffix <- "" : (show <$> ints)
152 , fresh <- [n <> T.pack suffix]
153 , fresh `Set.notMember` ns
156 freshName :: Names -> Name
157 freshName ns = L.head $ poolNames L.\\ Set.toList ns
159 -- | Infinite list of unique 'Name's:
160 -- @a, b, .., z, a1, b1 .., z1, a2, ..@
163 [ T.singleton n | n <- ['a'..'z'] ] <>
164 [ T.pack (n:show i) | n <- ['a'..'z']
165 , i <- [1 :: Int ..] ]
167 -- * Document 'Types'
176 d_op (D.charH '[') <> go tys <> d_op (D.charH ']')
179 go :: forall xs. Types src vs xs -> d
181 go (TypesS t0 (TypesS t1 ts)) =
182 docType conf 10 t0 <>
183 d_op (D.charH ',') <> D.space <>
184 docType conf 10 t1 <>
186 go (TypesS t ts) = docType conf 10 t <> go ts
188 -- * Document 'Const'
189 docConst :: D.Doc_Text d => Imports NameTy -> Const src c -> d
190 docConst imps c@Const{} =
193 revlookupImports f (m `Mod` n) imps
195 f = fixyOfFixity $ Fixity2 infixN5 `fromMaybe` fixityOf c
196 mn@(m `Mod` n) = nameTyOf c
198 -- * Document 'NameTy'
199 docNameTy :: D.Doc_Text d => NameTy -> d
200 docNameTy (NameTy t) = D.textH t
203 docMod :: D.Doc_Text d => (a -> d) -> Mod a -> d
204 docMod a2d ([] `Mod` a) = a2d a
205 docMod a2d (m `Mod` a) = docPathMod m <> (D.charH '.') <> a2d a
207 -- * Document 'PathMod'
208 docPathMod :: D.Doc_Text d => PathMod -> d
209 docPathMod (p::PathMod) =
211 L.intersperse (D.charH '.') $
212 (\(NameMod n) -> D.textH n) <$> p
220 D.Doc_Decoration d =>
222 Sym.Modules src ss -> d
223 docModules (Sym.Modules mods) =
225 (\p m doc -> docModule p m <> doc)
234 D.Doc_Decoration d =>
236 Sym.PathMod -> Sym.Module src ss -> d
237 docModule m Sym.Module
242 go docFixityInfix module_infix <>
243 go docFixityPrefix module_prefix <>
244 go docFixityPostfix module_postfix
246 go :: (fixy -> d) -> ModuleFixy src ss fixy -> d
254 docFixy token_fixity <>
255 D.space <> D.bold (D.yellower "::") <> D.space <>
256 docTokenTerm (t Sym.noSource) <>
266 Sym.Token_Term src ss -> d
268 let n2t = name2typeInj @ss in
269 case Sym.readTerm n2t CtxTyZ (G.BinTree0 t) of
270 Left{} -> error "[BUG] docTokenTerm: readTerm failed"
271 Right (Sym.TermVT te) ->
272 Sym.docType Sym.config_doc_type
273 { config_Doc_Type_vars_numbering = False
274 } 0 $ Sym.typeOfTerm te
276 docFixityInfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Infix -> t
277 docFixityInfix = \case
278 Sym.Infix Nothing 5 -> D.empty
283 Sym.AssocB Sym.SideL -> "l"
284 Sym.AssocB Sym.SideR -> "r" in
285 D.magenta $ " infix" <> maybe D.empty docAssoc a <>
286 D.space <> D.bold (D.bluer (D.int p))
287 docFixityPrefix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
288 docFixityPrefix p = D.magenta $ " prefix " <> D.bold (D.bluer (D.int $ Sym.unifix_prece p))
289 docFixityPostfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
290 docFixityPostfix p = D.magenta $ " postfix " <> D.bold (D.bluer (D.int $ Sym.unifix_prece p))
295 Sym.PathMod -> Sym.NameTe -> d
296 docPathTe (ms::Sym.PathMod) (Sym.NameTe n) =
298 L.intersperse (D.charH '.') $
299 ((\(Sym.NameMod m) -> D.textH m) <$> ms) <>
300 [(if isOp n then id else D.yellower) $ D.text n]
302 isOp = T.all $ \case '_' -> True; '\'' -> True; c -> Char.isAlphaNum c